From b8d4a629b0ca26f0b9b4288c92028d8fca6cb66d Mon Sep 17 00:00:00 2001 From: John Kisor Date: Sun, 17 Oct 2021 08:49:08 -0400 Subject: [PATCH 01/11] remove unused method --- helpers/account_helper.rb | 3 --- 1 file changed, 3 deletions(-) diff --git a/helpers/account_helper.rb b/helpers/account_helper.rb index 522982e4..df868235 100644 --- a/helpers/account_helper.rb +++ b/helpers/account_helper.rb @@ -775,7 +775,4 @@ def self.field_value(group, name) field.length == 1 ? field.first[:value] : Proc.new { "" } end - def self.parse_birth(birth) - DateTime.strptime("#{birth} EST", "%m/%d/%Y %H:%M:%S %p %Z") - end end From cf374c16cc98e7430912948803a82de9e85bb6dc Mon Sep 17 00:00:00 2001 From: John Kisor Date: Sun, 17 Oct 2021 10:54:13 -0400 Subject: [PATCH 02/11] method to ensure century for dates --- helpers/date_helper.rb | 8 ++++++++ spec/unit/date_helper_spec.rb | 27 +++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 helpers/date_helper.rb create mode 100644 spec/unit/date_helper_spec.rb diff --git a/helpers/date_helper.rb b/helpers/date_helper.rb new file mode 100644 index 00000000..5d9599b2 --- /dev/null +++ b/helpers/date_helper.rb @@ -0,0 +1,8 @@ +module DateHelper + + def self.ensure_century(date) + adjustment = (date.year < 1999) ? 2000 : 0 + date + adjustment.years + end + +end diff --git a/spec/unit/date_helper_spec.rb b/spec/unit/date_helper_spec.rb new file mode 100644 index 00000000..bb03c189 --- /dev/null +++ b/spec/unit/date_helper_spec.rb @@ -0,0 +1,27 @@ +require_relative '../spec_helper' +require './helpers/date_helper' + +describe DateHelper do + describe '.ensure_century' do + before do + date = Date.strptime(date_string, '%m/%d/%Y') + @result = DateHelper.ensure_century(date) + end + + describe 'when date string contains century' do + let(:date_string) { '10/07/2021' } + + it 'returns date with correct century' do + assert_equal(2021, @result.year) + end + end + + describe 'when date string does NOT contain century' do + let(:date_string) { '10/07/21' } + + it 'returns date with correct century' do + assert_equal(2021, @result.year) + end + end + end +end From ae4f717f8c8b0ed86fdee7479c8b302621815da3 Mon Sep 17 00:00:00 2001 From: John Kisor Date: Sun, 17 Oct 2021 11:13:56 -0400 Subject: [PATCH 03/11] ensure century for birth on character page --- views/_other_pane.haml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/views/_other_pane.haml b/views/_other_pane.haml index 55e58dc6..eb8fd082 100644 --- a/views/_other_pane.haml +++ b/views/_other_pane.haml @@ -4,7 +4,7 @@ %td.specialized{:colspan => 2} General %tr %td Birth - %td= @character.birth + %td= DateHelper::ensure_century(@character.birth) %tr %td Deaths %td= @character.deaths From 835b98408bee833559bb890c1ac008a5874fa93e Mon Sep 17 00:00:00 2001 From: John Kisor Date: Sun, 17 Oct 2021 11:15:36 -0400 Subject: [PATCH 04/11] ensure century for birth on account page --- helpers/account_helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helpers/account_helper.rb b/helpers/account_helper.rb index df868235..470c0692 100644 --- a/helpers/account_helper.rb +++ b/helpers/account_helper.rb @@ -39,7 +39,7 @@ module AccountHelper :name => "birth", :label => "Birth", :group => :general, - :value => Proc.new { |v| v[:birth] } + :value => Proc.new { |v| DateHelper::ensure_century(v[:birth]) } }, { :name => "aetheria", From 0cf84cf79d71fef28a351b51fbfe9320a29944a1 Mon Sep 17 00:00:00 2001 From: John Kisor Date: Sun, 17 Oct 2021 11:16:03 -0400 Subject: [PATCH 05/11] ensure century on upload --- helpers/character_helper.rb | 2 +- helpers/date_helper.rb | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/helpers/character_helper.rb b/helpers/character_helper.rb index 07129d3d..74c507f4 100644 --- a/helpers/character_helper.rb +++ b/helpers/character_helper.rb @@ -29,7 +29,7 @@ def self.parse_birth(birth) end end - parsed + DateHelper::ensure_century(parsed) end def self.tag_html(character) diff --git a/helpers/date_helper.rb b/helpers/date_helper.rb index 5d9599b2..21ae95c8 100644 --- a/helpers/date_helper.rb +++ b/helpers/date_helper.rb @@ -1,6 +1,8 @@ module DateHelper def self.ensure_century(date) + return if date.nil? + adjustment = (date.year < 1999) ? 2000 : 0 date + adjustment.years end From 6667820f87f7c47903adc8a9990ffe6f603f83d1 Mon Sep 17 00:00:00 2001 From: John Kisor Date: Sun, 17 Oct 2021 11:16:29 -0400 Subject: [PATCH 06/11] ensure century for birth on rankings page --- helpers/rankings_helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helpers/rankings_helper.rb b/helpers/rankings_helper.rb index 46506220..c8d014a2 100644 --- a/helpers/rankings_helper.rb +++ b/helpers/rankings_helper.rb @@ -463,7 +463,7 @@ def self.generate_aggregation_args(name, params) :match => { "a" => { "$exists" => true }, "b" => { "$ne" => nil } }, :project => { "_id" => 0, "n" => 1, "s" => 1, "b" => 1 }, :sort => { "b" => 1 }, - :accessor => Proc.new { |v| v["b"] } + :accessor => Proc.new { |v| DateHelper::ensure_century(v["b"]) } }, :deaths => { :display => "Deaths", From 78e872d6f6bb3a82d7aabaf60fe8a830cd11146f Mon Sep 17 00:00:00 2001 From: John Kisor Date: Sun, 17 Oct 2021 11:17:32 -0400 Subject: [PATCH 07/11] ensure century for birth from json character endpoint --- routes/server.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/routes/server.rb b/routes/server.rb index 57efe449..a883ffc9 100644 --- a/routes/server.rb +++ b/routes/server.rb @@ -62,7 +62,10 @@ def self.registered(app) end content_type 'application/json' - JSON.pretty_generate(@character.serializable_hash({}).tap {|h| h.delete("id")}) + JSON.pretty_generate(@character.serializable_hash({}).tap do |h| + h.delete("id") + h['birth'] = DateHelper::ensure_century(h['birth']) + end) end app.get '/:server/:name/?' do |s,n| From cbacb79e7a9a74c477a97dc440b08d4b79bfb866 Mon Sep 17 00:00:00 2001 From: John Kisor Date: Sun, 17 Oct 2021 11:47:06 -0400 Subject: [PATCH 08/11] rake task to cleanup birth dates in DB --- Rakefile | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/Rakefile b/Rakefile index 7a313e61..cb4e6c06 100644 --- a/Rakefile +++ b/Rakefile @@ -18,3 +18,21 @@ task "jobs:work" => "resque:work" task :test do Dir['./spec/**/*_spec.rb'].each { |f| load f } end + +desc "Adds missing centuries to birth dates (example: 0020 to 2020)" +task "cleanup_birth" do + characters = Character.where(({'birth' => {'$lt' => Date.parse('1999-01-01')}}) ) + + puts "Found #{characters.count}" + + puts "Updating..." if characters.count > 0 + + characters.each do |character| + with_century = DateHelper::ensure_century(character['birth']) + + character.update(birth: with_century) + end + + puts "Done" + +end From 4cd03039376f5fb0b7b5315cd6cf5e9bb2d60e12 Mon Sep 17 00:00:00 2001 From: John Kisor Date: Sun, 17 Oct 2021 13:08:02 -0400 Subject: [PATCH 09/11] move method --- helpers/character_helper.rb | 24 ------------------------ helpers/date_helper.rb | 24 ++++++++++++++++++++++++ routes/upload.rb | 2 +- 3 files changed, 25 insertions(+), 25 deletions(-) diff --git a/helpers/character_helper.rb b/helpers/character_helper.rb index 74c507f4..f62f3284 100644 --- a/helpers/character_helper.rb +++ b/helpers/character_helper.rb @@ -8,30 +8,6 @@ module CharacterHelper "self" => "Self" } - def self.parse_birth(birth) - parsed = nil - - # Try our first format - begin - parsed = DateTime.strptime("#{birth} EST", "%m/%d/%Y %H:%M:%S %p %Z") - rescue ArgumentError - puts "ArgumentError caught trying to parse '#{birth} EST' as a DateTime with format %m/%d/%Y %H:%M:%S %p %Z" - puts "Error was `#{$!}`" - end - - # Try our second one - if parsed.nil? - begin - parsed = DateTime.strptime("#{birth} EST", "%m/%d/%Y %H:%M:%S %Z") - rescue ArgumentError - puts "ArgumentError caught trying to parse '#{birth} EST' as a DateTime with format %m/%d/%Y %H:%M:%S %Z" - puts "Error was `#{$!}`" - end - end - - DateHelper::ensure_century(parsed) - end - def self.tag_html(character) html_strings = [] html_strings << "
" # Open up tag div diff --git a/helpers/date_helper.rb b/helpers/date_helper.rb index 21ae95c8..1f590483 100644 --- a/helpers/date_helper.rb +++ b/helpers/date_helper.rb @@ -7,4 +7,28 @@ def self.ensure_century(date) date + adjustment.years end + def self.parse(date_string) + parsed = nil + + # Try our first format + begin + parsed = DateTime.strptime("#{date_string} EST", "%m/%d/%Y %H:%M:%S %p %Z") + rescue ArgumentError + puts "ArgumentError caught trying to parse '#{date_string} EST' as a DateTime with format %m/%d/%Y %H:%M:%S %p %Z" + puts "Error was `#{$!}`" + end + + # Try our second one + if parsed.nil? + begin + parsed = DateTime.strptime("#{date_string} EST", "%m/%d/%Y %H:%M:%S %Z") + rescue ArgumentError + puts "ArgumentError caught trying to parse '#{date_string} EST' as a DateTime with format %m/%d/%Y %H:%M:%S %Z" + puts "Error was `#{$!}`" + end + end + + ensure_century(parsed) + end + end diff --git a/routes/upload.rb b/routes/upload.rb index ce7bb70e..0e4966c5 100644 --- a/routes/upload.rb +++ b/routes/upload.rb @@ -67,7 +67,7 @@ def self.registered(app) # Convert "birth" field so it's stored as DateTime with GMT-5 if(json_text.has_key?("birth")) - json_text["birth"] = CharacterHelper::parse_birth(json_text["birth"]) + json_text["birth"] = DateHelper::parse(json_text["birth"]) end # Log extra debug info if birth ends up being nil From 43aa186ead6fff2abc806b713cc8dae8ab0b825b Mon Sep 17 00:00:00 2001 From: John Kisor Date: Sun, 17 Oct 2021 15:54:54 -0400 Subject: [PATCH 10/11] remove duplication from date parsing --- helpers/date_helper.rb | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/helpers/date_helper.rb b/helpers/date_helper.rb index 1f590483..d5c08c87 100644 --- a/helpers/date_helper.rb +++ b/helpers/date_helper.rb @@ -1,5 +1,10 @@ module DateHelper + FORMATS = [ + "%m/%d/%Y %H:%M:%S %p %Z", + "%m/%d/%Y %H:%M:%S %Z" + ] + def self.ensure_century(date) return if date.nil? @@ -8,27 +13,17 @@ def self.ensure_century(date) end def self.parse(date_string) - parsed = nil - - # Try our first format - begin - parsed = DateTime.strptime("#{date_string} EST", "%m/%d/%Y %H:%M:%S %p %Z") - rescue ArgumentError - puts "ArgumentError caught trying to parse '#{date_string} EST' as a DateTime with format %m/%d/%Y %H:%M:%S %p %Z" - puts "Error was `#{$!}`" - end - - # Try our second one - if parsed.nil? + results = FORMATS.lazy.map do |format| begin - parsed = DateTime.strptime("#{date_string} EST", "%m/%d/%Y %H:%M:%S %Z") + parsed = DateTime.strptime("#{date_string} EST", format) + ensure_century(parsed) rescue ArgumentError - puts "ArgumentError caught trying to parse '#{date_string} EST' as a DateTime with format %m/%d/%Y %H:%M:%S %Z" + puts "ArgumentError caught trying to parse '#{date_string} EST' as a DateTime with format #{format}" puts "Error was `#{$!}`" end end - ensure_century(parsed) + results.detect { |date| !date.nil? } end end From a1872e00d7abf8745db5a7fa990b25fcd49633e8 Mon Sep 17 00:00:00 2001 From: John Kisor Date: Sun, 17 Oct 2021 16:06:25 -0400 Subject: [PATCH 11/11] remove nil check --- helpers/date_helper.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/helpers/date_helper.rb b/helpers/date_helper.rb index d5c08c87..decaaf7d 100644 --- a/helpers/date_helper.rb +++ b/helpers/date_helper.rb @@ -6,8 +6,6 @@ module DateHelper ] def self.ensure_century(date) - return if date.nil? - adjustment = (date.year < 1999) ? 2000 : 0 date + adjustment.years end