Skip to content

Commit b11e424

Browse files
committed
MONGOID-5222 fix tests
1 parent 7e869d0 commit b11e424

File tree

3 files changed

+20
-17
lines changed

3 files changed

+20
-17
lines changed

lib/mongoid/extensions/date.rb

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,21 @@ def demongoize(object)
5252
#
5353
# @return [ Time ] The object mongoized.
5454
def mongoize(object)
55-
return if object.nil? || object.blank?
56-
begin
57-
if object.is_a?(String)
58-
# https://jira.mongodb.org/browse/MONGOID-4460
59-
time = ::Time.parse(object)
60-
else
61-
time = object.__mongoize_time__
55+
return if object.nil?
56+
unless object.blank?
57+
begin
58+
if object.is_a?(String)
59+
# https://jira.mongodb.org/browse/MONGOID-4460
60+
time = ::Time.parse(object)
61+
else
62+
time = object.__mongoize_time__
63+
end
64+
if time.acts_like?(:time)
65+
::Time.utc(time.year, time.month, time.day)
66+
end
67+
rescue ArgumentError
68+
nil
6269
end
63-
if time.acts_like?(:time)
64-
::Time.utc(time.year, time.month, time.day)
65-
end
66-
rescue ArgumentError
67-
nil
6870
end.tap do |res|
6971
if res.nil?
7072
raise Errors::InvalidValue.new(self, object)

lib/mongoid/extensions/range.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ def mongoize(object)
6767
case object
6868
when Hash then __mongoize_hash__(object)
6969
when Range then __mongoize_range__(object)
70-
else raise Errors::InvalidValue.new(self, object)
70+
end.tap do |res|
71+
raise Errors::InvalidValue.new(self, object) unless res
7172
end
7273
end
7374

spec/mongoid/attributes_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1586,13 +1586,13 @@
15861586
it "raises an error when trying to set a value of invalid type - array" do
15871587
expect do
15881588
person.map = []
1589-
end.to raise_error(Mongoid::Errors::InvalidValue, /Value of type Array cannot be written to a field of type Hash/)
1589+
end.to raise_error(Mongoid::Errors::InvalidValue, /The value \[\] cannot be written to a field of type Hash/)
15901590
end
15911591

15921592
it "raises an error when trying to set a value of invalid type - boolean" do
15931593
expect do
15941594
person.map = false
1595-
end.to raise_error(Mongoid::Errors::InvalidValue, /Value of type FalseClass cannot be written to a field of type Hash/)
1595+
end.to raise_error(Mongoid::Errors::InvalidValue, /The value false cannot be written to a field of type Hash/)
15961596
end
15971597

15981598
it "can set a Hash value" do
@@ -1610,13 +1610,13 @@
16101610
it "raises an error when trying to set a value of invalid type - hash" do
16111611
expect do
16121612
person.aliases = {}
1613-
end.to raise_error(Mongoid::Errors::InvalidValue, /Value of type Hash cannot be written to a field of type Array/)
1613+
end.to raise_error(Mongoid::Errors::InvalidValue, /The value {} cannot be written to a field of type Array/)
16141614
end
16151615

16161616
it "raises an error when trying to set a value of invalid type - boolean" do
16171617
expect do
16181618
person.aliases = false
1619-
end.to raise_error(Mongoid::Errors::InvalidValue, /Value of type FalseClass cannot be written to a field of type Array/)
1619+
end.to raise_error(Mongoid::Errors::InvalidValue, /The value false cannot be written to a field of type Array/)
16201620
end
16211621
end
16221622

0 commit comments

Comments
 (0)