Skip to content

MONGOID-5221/5222: Fix mongoization + demongoization of non-empty string as Integer/Float #5220

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/mongoid/extensions/boolean.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class << self
# @example Mongoize the object.
# Boolean.mongoize("123.11")
#
# @return [ String ] The object mongoized.
# @return [ true | false ] The object mongoized.
def mongoize(object)
evolve(object)
end
Expand Down
9 changes: 3 additions & 6 deletions lib/mongoid/extensions/float.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,10 @@ module ClassMethods
#
# @param [ Object ] object The object to mongoize.
#
# @return [ String ] The object mongoized.
# @return [ Float ] The object mongoized.
def mongoize(object)
unless object.blank?
__numeric__(object).to_f rescue 0.0
else
nil
end
return nil if object.blank?
Float(__numeric__(object)) rescue nil
end
alias :demongoize :mongoize
end
Expand Down
9 changes: 3 additions & 6 deletions lib/mongoid/extensions/integer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,10 @@ module ClassMethods
# @example Mongoize the object.
# BigDecimal.mongoize("123.11")
#
# @return [ String ] The object mongoized.
# @return [ Integer ] The object mongoized.
def mongoize(object)
unless object.blank?
__numeric__(object).to_i rescue 0
else
nil
end
return nil if object.blank?
Integer(__numeric__(object)) rescue nil
end
alias :demongoize :mongoize
end
Expand Down
9 changes: 9 additions & 0 deletions spec/mongoid/criteria/queryable/extensions/numeric_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,14 @@
expect { actual }.to raise_error(ArgumentError)
end
end

context "when the string is non-numeric" do

let(:str) { 'foo' }

it "returns the value as integer" do
expect { actual }.to raise_error(ArgumentError)
end
end
end
end
8 changes: 4 additions & 4 deletions spec/mongoid/extensions/float_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@

context "when the value is not a float string" do

it "returns a float" do
expect(Float.demongoize('asdf')).to eq(0)
it "returns nil" do
expect(Float.demongoize('asdf')).to eq(nil)
end
end
end
Expand Down Expand Up @@ -124,8 +124,8 @@

context "when the string is non numerical" do

it "returns 0" do
expect(Float.mongoize("foo")).to eq(0.0)
it "returns nil" do
expect(Float.mongoize("foo")).to eq(nil)
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/mongoid/extensions/integer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
context "when the string is non numerical" do

it "returns 0" do
expect(Integer.mongoize("foo")).to eq(0)
expect(Integer.mongoize("foo")).to eq(nil)
end
end

Expand Down