Skip to content

Commit e0f726e

Browse files
committed
[STORE] Refactor the Rails' forms date conversions into a module method
Also, added tests. Note, this doesn't solve the problem with updating the model, added a pending test.
1 parent 92ff8c8 commit e0f726e

File tree

3 files changed

+60
-19
lines changed

3 files changed

+60
-19
lines changed

elasticsearch-persistence/lib/elasticsearch/persistence/model/rails.rb

+27-19
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,38 @@ module Rails
88
def self.included(base)
99
base.class_eval do
1010

11-
# Decorates the passed in `attributes` so they extract the date & time values from Rails forms
12-
#
13-
# @example Correctly combine the date and time to a datetime string
14-
#
15-
# params = { "published_on(1i)"=>"2014",
16-
# "published_on(2i)"=>"1",
17-
# "published_on(3i)"=>"1",
18-
# "published_on(4i)"=>"12",
19-
# "published_on(5i)"=>"00"
20-
# }
21-
# MyRailsModel.new(params).published_on.iso8601
22-
# # => "2014-01-01T12:00:00+00:00"
23-
#
2411
def initialize(attributes={})
25-
day = attributes.select { |p| p =~ /\([1-3]/ }.reduce({}) { |sum, item| (sum[item.first.gsub(/\(.+\)/, '')] ||= '' )<< item.last+'-'; sum }
26-
time = attributes.select { |p| p =~ /\([4-6]/ }.reduce({}) { |sum, item| (sum[item.first.gsub(/\(.+\)/, '')] ||= '' )<< item.last+':'; sum }
27-
unless day.empty?
28-
attributes.update day.reduce({}) { |sum, item| sum[item.first] = item.last; sum[item.first] += ' ' + time[item.first] unless time.empty?; sum }
29-
end
12+
super(__convert_rails_dates(attributes))
13+
end
3014

31-
super(attributes)
15+
def update(attributes={}, options={})
16+
super(__convert_rails_dates(attributes))
3217
end
3318
end
3419
end
20+
21+
# Decorates the passed in `attributes` so they extract the date & time values from Rails forms
22+
#
23+
# @example Correctly combine the date and time to a datetime string
24+
#
25+
# params = { "published_on(1i)"=>"2014",
26+
# "published_on(2i)"=>"1",
27+
# "published_on(3i)"=>"1",
28+
# "published_on(4i)"=>"12",
29+
# "published_on(5i)"=>"00"
30+
# }
31+
# MyRailsModel.new(params).published_on.iso8601
32+
# # => "2014-01-01T12:00:00+00:00"
33+
#
34+
def __convert_rails_dates(attributes={})
35+
day = attributes.select { |p| p =~ /\([1-3]/ }.reduce({}) { |sum, item| (sum[item.first.gsub(/\(.+\)/, '')] ||= '' )<< item.last+'-'; sum }
36+
time = attributes.select { |p| p =~ /\([4-6]/ }.reduce({}) { |sum, item| (sum[item.first.gsub(/\(.+\)/, '')] ||= '' )<< item.last+':'; sum }
37+
unless day.empty?
38+
attributes.update day.reduce({}) { |sum, item| sum[item.first] = item.last; sum[item.first] += ' ' + time[item.first] unless time.empty?; sum }
39+
end
40+
41+
return attributes
42+
end; module_function :__convert_rails_dates
3543
end
3644

3745
end

elasticsearch-persistence/test/integration/model/model_basic_test.rb

+26
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
require 'test_helper'
22

33
require 'elasticsearch/persistence/model'
4+
require 'elasticsearch/persistence/model/rails'
45

56
module Elasticsearch
67
module Persistence
78
class PersistenceModelBasicIntegrationTest < Elasticsearch::Test::IntegrationTestCase
89

910
class ::Person
1011
include Elasticsearch::Persistence::Model
12+
include Elasticsearch::Persistence::Model::Rails
1113

1214
settings index: { number_of_shards: 1 }
1315

@@ -81,6 +83,30 @@ class ::Person
8183
assert_equal 'UPDATED', Person.find(person.id).name
8284
end
8385

86+
should "create the model with correct Date form Rails' form attributes" do
87+
params = { "birthday(1i)"=>"2014",
88+
"birthday(2i)"=>"1",
89+
"birthday(3i)"=>"1"
90+
}
91+
person = Person.create params.merge(name: 'TEST')
92+
93+
assert_equal Date.parse('2014-01-01'), person.birthday
94+
assert_equal Date.parse('2014-01-01'), Person.find(person.id).birthday
95+
end
96+
97+
should_eventually "update the model with correct Date form Rails' form attributes" do
98+
params = { "birthday(1i)"=>"2014",
99+
"birthday(2i)"=>"1",
100+
"birthday(3i)"=>"1"
101+
}
102+
person = Person.create params.merge(name: 'TEST')
103+
104+
person.update params.merge('birthday(1i)' => '2015')
105+
106+
assert_equal Date.parse('2015-01-01'), person.birthday
107+
assert_equal Date.parse('2015-01-01'), Person.find(person.id).birthday
108+
end
109+
84110
should "increment an object attribute" do
85111
person = Person.create name: 'John Smith', salary: 1_000
86112

elasticsearch-persistence/test/unit/model_rails_test.rb

+7
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ class MyView; include ActionView::Helpers::UrlHelper; end
7070
"published_at(5i)"=>"00"
7171
}
7272

73+
assert_equal '2014-1-1- 12:00:',
74+
Elasticsearch::Persistence::Model::Rails.__convert_rails_dates(params)['published_at']
75+
7376
m = MyRailsModel.new params
7477
assert_equal "2014-01-01T12:00:00+00:00", m.published_at.iso8601
7578
end
@@ -80,6 +83,10 @@ class MyView; include ActionView::Helpers::UrlHelper; end
8083
"published_on(3i)"=>"1"
8184
}
8285

86+
assert_equal '2014-1-1-',
87+
Elasticsearch::Persistence::Model::Rails.__convert_rails_dates(params)['published_on']
88+
89+
8390
m = MyRailsModel.new params
8491
assert_equal "2014-01-01", m.published_on.iso8601
8592
end

0 commit comments

Comments
 (0)