From c89503f69113cdb3c11d6dbd4a50065d57d2d6bc Mon Sep 17 00:00:00 2001 From: Miha Mencin Date: Tue, 17 Mar 2020 21:20:50 +0100 Subject: [PATCH 1/4] SCI-4464 fix the import of the dates, some tests --- app/models/repository_date_time_value.rb | 7 ++++ app/models/repository_date_time_value_base.rb | 7 ---- app/models/repository_date_value.rb | 7 ++++ app/models/repository_time_value.rb | 7 ++++ .../repository_date_time_value_base_spec.rb | 42 ------------------- .../models/repository_date_time_value_spec.rb | 34 +++++++++++++++ spec/models/repository_date_value_spec.rb | 33 +++++++++++++++ 7 files changed, 88 insertions(+), 49 deletions(-) diff --git a/app/models/repository_date_time_value.rb b/app/models/repository_date_time_value.rb index cf8e24e8a..bcb0e0998 100644 --- a/app/models/repository_date_time_value.rb +++ b/app/models/repository_date_time_value.rb @@ -18,5 +18,12 @@ class RepositoryDateTimeValue < RepositoryDateTimeValueBase value end + 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 + alias export_formatted formatted end diff --git a/app/models/repository_date_time_value_base.rb b/app/models/repository_date_time_value_base.rb index 941cf1564..d16b0fc31 100644 --- a/app/models/repository_date_time_value_base.rb +++ b/app/models/repository_date_time_value_base.rb @@ -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 diff --git a/app/models/repository_date_value.rb b/app/models/repository_date_value.rb index 318f4dbfe..a56b42553 100644 --- a/app/models/repository_date_value.rb +++ b/app/models/repository_date_value.rb @@ -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 diff --git a/app/models/repository_time_value.rb b/app/models/repository_time_value.rb index e9e0eb774..4fa5d0ff0 100644 --- a/app/models/repository_time_value.rb +++ b/app/models/repository_time_value.rb @@ -18,5 +18,12 @@ class RepositoryTimeValue < RepositoryDateTimeValueBase value end + def self.import_from_text(text, attributes, options = {}) + date_format = "%H:%M" + new(attributes.merge(data: DateTime.strptime(text, date_format).strftime(date_format))) + rescue ArgumentError + nil + end + alias export_formatted formatted end diff --git a/spec/models/repository_date_time_value_base_spec.rb b/spec/models/repository_date_time_value_base_spec.rb index 4761dbb1e..a178cfaf0 100644 --- a/spec/models/repository_date_time_value_base_spec.rb +++ b/spec/models/repository_date_time_value_base_spec.rb @@ -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 diff --git a/spec/models/repository_date_time_value_spec.rb b/spec/models/repository_date_time_value_spec.rb index 3f73e251c..48ec35890 100644 --- a/spec/models/repository_date_time_value_spec.rb +++ b/spec/models/repository_date_time_value_spec.rb @@ -49,4 +49,38 @@ 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 diff --git a/spec/models/repository_date_value_spec.rb b/spec/models/repository_date_value_spec.rb index 118dd8738..d82052c2c 100644 --- a/spec/models/repository_date_value_spec.rb +++ b/spec/models/repository_date_value_spec.rb @@ -49,4 +49,37 @@ 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 From 24b979aa6f5709b3dad53a659634cf999bf6c557 Mon Sep 17 00:00:00 2001 From: Miha Mencin Date: Wed, 18 Mar 2020 09:53:47 +0100 Subject: [PATCH 2/4] fix CR comments --- app/models/repository_date_time_value.rb | 5 ++--- app/models/repository_date_value.rb | 2 +- app/models/repository_time_value.rb | 4 ++-- spec/models/repository_date_time_value_spec.rb | 7 +++---- spec/models/repository_date_value_spec.rb | 1 - 5 files changed, 8 insertions(+), 11 deletions(-) diff --git a/app/models/repository_date_time_value.rb b/app/models/repository_date_time_value.rb index bcb0e0998..66810818f 100644 --- a/app/models/repository_date_time_value.rb +++ b/app/models/repository_date_time_value.rb @@ -9,7 +9,7 @@ class RepositoryDateTimeValue < RepositoryDateTimeValueBase end def formatted - super(:full_with_comma) + I18n.l(data, format: :full ) end def self.new_with_payload(payload, attributes) @@ -19,11 +19,10 @@ class RepositoryDateTimeValue < RepositoryDateTimeValueBase end def self.import_from_text(text, attributes, options = {}) - date_format = (options.dig(:user, :settings, :date_format) || Constants::DEFAULT_DATE_FORMAT).gsub(/%-/, '%') + ", %H:%M" + 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 - alias export_formatted formatted end diff --git a/app/models/repository_date_value.rb b/app/models/repository_date_value.rb index a56b42553..680ec67ce 100644 --- a/app/models/repository_date_value.rb +++ b/app/models/repository_date_value.rb @@ -19,7 +19,7 @@ class RepositoryDateValue < RepositoryDateTimeValueBase end def self.import_from_text(text, attributes, options = {}) - date_format = (options.dig(:user, :settings, :date_format) || Constants::DEFAULT_DATE_FORMAT).gsub(/%-/, '%') + 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 diff --git a/app/models/repository_time_value.rb b/app/models/repository_time_value.rb index 4fa5d0ff0..ec2ba25cd 100644 --- a/app/models/repository_time_value.rb +++ b/app/models/repository_time_value.rb @@ -19,8 +19,8 @@ class RepositoryTimeValue < RepositoryDateTimeValueBase end def self.import_from_text(text, attributes, options = {}) - date_format = "%H:%M" - new(attributes.merge(data: DateTime.strptime(text, date_format).strftime(date_format))) + time_format = '%H:%M' + new(attributes.merge(data: DateTime.strptime(text, time_format).strftime(time_format))) rescue ArgumentError nil end diff --git a/spec/models/repository_date_time_value_spec.rb b/spec/models/repository_date_time_value_spec.rb index 48ec35890..d5a6ee738 100644 --- a/spec/models/repository_date_time_value_spec.rb +++ b/spec/models/repository_date_time_value_spec.rb @@ -9,7 +9,7 @@ describe RepositoryDateTimeValue, type: :model do describe '.formatted' do it 'prints date format with date' do - str = '10/11/2000, 01:04' + str = '10/11/2000 01:04' expect(date_time_value.formatted).to eq(str) end end @@ -53,15 +53,14 @@ describe RepositoryDateTimeValue, type: :model do 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(: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" + date_format += ' %H:%M' test_string = test_date.strftime(date_format) result = RepositoryDateTimeValue .import_from_text(test_string, diff --git a/spec/models/repository_date_value_spec.rb b/spec/models/repository_date_value_spec.rb index d82052c2c..42c3bd889 100644 --- a/spec/models/repository_date_value_spec.rb +++ b/spec/models/repository_date_value_spec.rb @@ -56,7 +56,6 @@ describe RepositoryDateValue, type: :model do 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(/%-/, '%') From ecd49cb1e73881187d517a412b31a7af30e2d4da Mon Sep 17 00:00:00 2001 From: Miha Mencin Date: Wed, 18 Mar 2020 09:57:57 +0100 Subject: [PATCH 3/4] fix format and export_format mess --- app/models/repository_date_time_value.rb | 4 ++++ spec/models/repository_date_time_value_spec.rb | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/app/models/repository_date_time_value.rb b/app/models/repository_date_time_value.rb index 66810818f..dbacffe8f 100644 --- a/app/models/repository_date_time_value.rb +++ b/app/models/repository_date_time_value.rb @@ -9,6 +9,10 @@ class RepositoryDateTimeValue < RepositoryDateTimeValueBase end def formatted + super(:full_with_comma) + end + + def export_formatted I18n.l(data, format: :full ) end diff --git a/spec/models/repository_date_time_value_spec.rb b/spec/models/repository_date_time_value_spec.rb index d5a6ee738..d44826287 100644 --- a/spec/models/repository_date_time_value_spec.rb +++ b/spec/models/repository_date_time_value_spec.rb @@ -9,7 +9,7 @@ describe RepositoryDateTimeValue, type: :model do describe '.formatted' do it 'prints date format with date' do - str = '10/11/2000 01:04' + str = '10/11/2000, 01:04' expect(date_time_value.formatted).to eq(str) end end From fd8462b008becb9fe89aa7c40cf88bf6da865668 Mon Sep 17 00:00:00 2001 From: Miha Mencin Date: Wed, 18 Mar 2020 10:35:31 +0100 Subject: [PATCH 4/4] codstyle --- app/models/repository_date_time_value.rb | 3 +-- app/models/repository_time_value.rb | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/app/models/repository_date_time_value.rb b/app/models/repository_date_time_value.rb index dbacffe8f..9a7402190 100644 --- a/app/models/repository_date_time_value.rb +++ b/app/models/repository_date_time_value.rb @@ -13,7 +13,7 @@ class RepositoryDateTimeValue < RepositoryDateTimeValueBase end def export_formatted - I18n.l(data, format: :full ) + I18n.l(data, format: :full) end def self.new_with_payload(payload, attributes) @@ -28,5 +28,4 @@ class RepositoryDateTimeValue < RepositoryDateTimeValueBase rescue ArgumentError nil end - end diff --git a/app/models/repository_time_value.rb b/app/models/repository_time_value.rb index ec2ba25cd..bcd7e62ce 100644 --- a/app/models/repository_time_value.rb +++ b/app/models/repository_time_value.rb @@ -18,7 +18,7 @@ class RepositoryTimeValue < RepositoryDateTimeValueBase value end - def self.import_from_text(text, attributes, options = {}) + 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