mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-01-01 13:13:22 +08:00
Merge pull request #2476 from biosistemika/SCI-4464-fix-date-time-imports
SCI-4464 fix the import of the dates, some tests
This commit is contained in:
commit
b3b9f578b8
7 changed files with 89 additions and 50 deletions
|
@ -12,11 +12,20 @@ class RepositoryDateTimeValue < RepositoryDateTimeValueBase
|
|||
super(:full_with_comma)
|
||||
end
|
||||
|
||||
def export_formatted
|
||||
I18n.l(data, format: :full)
|
||||
end
|
||||
|
||||
def self.new_with_payload(payload, attributes)
|
||||
value = new(attributes)
|
||||
value.data = Time.zone.parse(payload)
|
||||
value
|
||||
end
|
||||
|
||||
alias export_formatted formatted
|
||||
def self.import_from_text(text, attributes, options = {})
|
||||
date_format = (options.dig(:user, :settings, :date_format) || Constants::DEFAULT_DATE_FORMAT).gsub(/%-/, '%') + ' %H:%M'
|
||||
new(attributes.merge(data: DateTime.strptime(text, date_format)))
|
||||
rescue ArgumentError
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
|
|
@ -24,11 +24,4 @@ class RepositoryDateTimeValueBase < ApplicationRecord
|
|||
self.last_modified_by = user
|
||||
save!
|
||||
end
|
||||
|
||||
def self.import_from_text(text, attributes, options = {})
|
||||
date_format = options.dig(:user, :settings, :date_format) || Constants::DEFAULT_DATE_FORMAT
|
||||
new(attributes.merge(data: DateTime.strptime(text, date_format)))
|
||||
rescue ArgumentError
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
|
|
@ -18,5 +18,12 @@ class RepositoryDateValue < RepositoryDateTimeValueBase
|
|||
value
|
||||
end
|
||||
|
||||
def self.import_from_text(text, attributes, options = {})
|
||||
date_format = (options.dig(:user, :settings, :date_format) || Constants::DEFAULT_DATE_FORMAT).gsub(/%-/, '%')
|
||||
new(attributes.merge(data: DateTime.strptime(text, date_format)))
|
||||
rescue ArgumentError
|
||||
nil
|
||||
end
|
||||
|
||||
alias export_formatted formatted
|
||||
end
|
||||
|
|
|
@ -18,5 +18,12 @@ class RepositoryTimeValue < RepositoryDateTimeValueBase
|
|||
value
|
||||
end
|
||||
|
||||
def self.import_from_text(text, attributes, _options = {})
|
||||
time_format = '%H:%M'
|
||||
new(attributes.merge(data: DateTime.strptime(text, time_format).strftime(time_format)))
|
||||
rescue ArgumentError
|
||||
nil
|
||||
end
|
||||
|
||||
alias export_formatted formatted
|
||||
end
|
||||
|
|
|
@ -39,46 +39,4 @@ describe RepositoryDateTimeValueBase, type: :model do
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'self.import_from_text' do
|
||||
let(:user) { create :user }
|
||||
let(:date_string1) { '2020-01-22' }
|
||||
let(:date_string2) { '01-22-2020' }
|
||||
let(:date_string3) { '01/22/2020' }
|
||||
let(:test_date) { Date.parse('2020-01-22') }
|
||||
let(:attributes) { {} }
|
||||
|
||||
it 'takes date format from user settings 1' do
|
||||
user.settings[:date_format] = '%Y-%m-%d'
|
||||
user.save
|
||||
result = RepositoryDateTimeValueBase
|
||||
.import_from_text(date_string1,
|
||||
attributes,
|
||||
user.as_json(root: true, only: :settings).deep_symbolize_keys)
|
||||
expect(result).to respond_to(:data)
|
||||
expect(result.data).to eq(test_date)
|
||||
end
|
||||
|
||||
it 'takes date format from user settings 2' do
|
||||
user.settings[:date_format] = '%m-%d-%Y'
|
||||
user.save
|
||||
result = RepositoryDateTimeValueBase
|
||||
.import_from_text(date_string2,
|
||||
attributes,
|
||||
user.as_json(root: true, only: :settings).deep_symbolize_keys)
|
||||
expect(result).to respond_to(:data)
|
||||
expect(result.data).to eq(test_date)
|
||||
end
|
||||
|
||||
it 'takes default date format when there is no user settings' do
|
||||
user.settings[:date_format] = nil
|
||||
user.save
|
||||
result = RepositoryDateTimeValueBase
|
||||
.import_from_text(date_string3,
|
||||
attributes,
|
||||
user.as_json(root: true, only: :settings).deep_symbolize_keys)
|
||||
expect(result).to respond_to(:data)
|
||||
expect(result.data).to eq(test_date)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -49,4 +49,37 @@ describe RepositoryDateTimeValue, type: :model do
|
|||
.to be_an_instance_of RepositoryDateTimeValue
|
||||
end
|
||||
end
|
||||
|
||||
describe 'self.import_from_text' do
|
||||
let(:user) { create :user }
|
||||
let(:test_date) { DateTime.parse('2020-01-22 09:52') }
|
||||
let(:date_string) { '01/22/2020 09:52' }
|
||||
let(:attributes) { {} }
|
||||
|
||||
it 'correctly handles all available date formats' do
|
||||
Constants::SUPPORTED_DATE_FORMATS.each do |date_format|
|
||||
user.settings[:date_format] = date_format.gsub(/%-/, '%')
|
||||
user.save
|
||||
date_format += ' %H:%M'
|
||||
test_string = test_date.strftime(date_format)
|
||||
result = RepositoryDateTimeValue
|
||||
.import_from_text(test_string,
|
||||
attributes,
|
||||
user.as_json(root: true, only: :settings).deep_symbolize_keys)
|
||||
expect(result).to respond_to(:data)
|
||||
expect(result.data).to eq(test_date)
|
||||
end
|
||||
end
|
||||
|
||||
it 'takes default date format when there is no user settings' do
|
||||
user.settings[:date_format] = nil
|
||||
user.save
|
||||
result = RepositoryDateTimeValue
|
||||
.import_from_text(date_string,
|
||||
attributes,
|
||||
user.as_json(root: true, only: :settings).deep_symbolize_keys)
|
||||
expect(result).to respond_to(:data)
|
||||
expect(result.data).to eq(test_date)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -49,4 +49,36 @@ describe RepositoryDateValue, type: :model do
|
|||
.to be_an_instance_of RepositoryDateValue
|
||||
end
|
||||
end
|
||||
|
||||
describe 'self.import_from_text' do
|
||||
let(:user) { create :user }
|
||||
let(:test_date) { Date.parse('2020-01-22') }
|
||||
let(:date_string) { '01/22/2020' }
|
||||
let(:attributes) { {} }
|
||||
|
||||
it 'correctly handles all available date formats' do
|
||||
Constants::SUPPORTED_DATE_FORMATS.each do |date_format|
|
||||
user.settings[:date_format] = date_format.gsub(/%-/, '%')
|
||||
user.save
|
||||
test_string = test_date.strftime(date_format)
|
||||
result = RepositoryDateValue
|
||||
.import_from_text(test_string,
|
||||
attributes,
|
||||
user.as_json(root: true, only: :settings).deep_symbolize_keys)
|
||||
expect(result).to respond_to(:data)
|
||||
expect(result.data).to eq(test_date)
|
||||
end
|
||||
end
|
||||
|
||||
it 'takes default date format when there is no user settings' do
|
||||
user.settings[:date_format] = nil
|
||||
user.save
|
||||
result = RepositoryDateValue
|
||||
.import_from_text(date_string,
|
||||
attributes,
|
||||
user.as_json(root: true, only: :settings).deep_symbolize_keys)
|
||||
expect(result).to respond_to(:data)
|
||||
expect(result.data).to eq(test_date)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue