Skip to content
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

Improve BigDecimal mongoize behavior #4164

Merged
merged 1 commit into from Apr 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 17 additions & 8 deletions lib/mongoid/extensions/big_decimal.rb
Expand Up @@ -28,6 +28,18 @@ def mongoize
to_s
end

# Is the BigDecimal a number?
#
# @example Is the object a number?.
# object.numeric?
#
# @return [ true ] Always true.
#
# @since 6.0.0
def numeric?
true
end

module ClassMethods

# Convert the object from its mongo friendly ruby type to this type.
Expand All @@ -37,28 +49,25 @@ module ClassMethods
#
# @param [ Object ] object The object to demongoize.
#
# @return [ Object ] The object.
# @return [ BigDecimal, nil ] A BigDecimal derived from the object or nil.
#
# @since 3.0.0
def demongoize(object)
if object
object.numeric? ? ::BigDecimal.new(object.to_s) : object
end
object && object.numeric? ? ::BigDecimal.new(object.to_s) : nil
end

# Mongoize an object of any type to how it's stored in the db as a big
# decimal.
# Mongoize an object of any type to how it's stored in the db as a String.
#
# @example Mongoize the object.
# BigDecimal.mongoize(123)
#
# @param [ Object ] object The object to Mongoize
#
# @return [ String ] The mongoized object.
# @return [ String, nil ] A String representing the object or nil.
#
# @since 3.0.7
def mongoize(object)
object ? object.to_s : object
object && object.numeric? ? object.to_s : nil
end
end
end
Expand Down
5 changes: 3 additions & 2 deletions lib/mongoid/extensions/string.rb
Expand Up @@ -72,7 +72,8 @@ def mongoid_id?
self =~ /\A(|_)id$/
end

# Is the string a number?
# Is the string a number? The literals "NaN", "Infinity", and "-Infinity"
# are counted as numbers.
#
# @example Is the string a number.
# "1234.23".numeric?
Expand All @@ -81,7 +82,7 @@ def mongoid_id?
#
# @since 3.0.0
def numeric?
true if Float(self) rescue (self == "NaN")
true if Float(self) rescue (self =~ /^NaN|\-?Infinity$/)
end

# Get the string as a getter string.
Expand Down
2 changes: 1 addition & 1 deletion lib/mongoid/findable.rb
Expand Up @@ -11,7 +11,7 @@ module Findable
select_with :with_default_scope

# These are methods defined on the criteria that should also be accessible
# directly from the the class level.
# directly from the class level.
delegate \
:aggregates,
:avg,
Expand Down
18 changes: 9 additions & 9 deletions spec/mongoid/attributes/nested_spec.rb
Expand Up @@ -1970,7 +1970,7 @@ class BandWithAllowDestroyedRecords < Band
}
end

it "ignores the the marked document" do
it "ignores the marked document" do
expect(person.addresses.size).to eq(1)
end

Expand Down Expand Up @@ -2031,7 +2031,7 @@ class BandWithAllowDestroyedRecords < Band
}
end

it "adds the the marked document" do
it "adds the marked document" do
expect(person.addresses.first.street).to eq("Maybachufer")
end

Expand Down Expand Up @@ -2091,7 +2091,7 @@ class BandWithAllowDestroyedRecords < Band
}
end

it "adds the the marked document" do
it "adds the marked document" do
expect(person.addresses.first.street).to eq("Maybachufer")
end

Expand Down Expand Up @@ -3431,7 +3431,7 @@ class BandWithAllowDestroyedRecords < Band
}
end

it "ignores the the marked document" do
it "ignores the marked document" do
expect(person.posts.size).to eq(1)
end

Expand Down Expand Up @@ -3492,7 +3492,7 @@ class BandWithAllowDestroyedRecords < Band
}
end

it "adds the the marked document" do
it "adds the marked document" do
expect(person.posts.first.title).to eq("New Blog")
end

Expand Down Expand Up @@ -3552,7 +3552,7 @@ class BandWithAllowDestroyedRecords < Band
}
end

it "adds the the marked document" do
it "adds the marked document" do
expect(person.posts.first.title).to eq("New Blog")
end

Expand Down Expand Up @@ -4097,7 +4097,7 @@ class BandWithAllowDestroyedRecords < Band
}
end

it "ignores the the marked document" do
it "ignores the marked document" do
expect(person.preferences.size).to eq(1)
end

Expand Down Expand Up @@ -4158,7 +4158,7 @@ class BandWithAllowDestroyedRecords < Band
}
end

it "adds the the marked document" do
it "adds the marked document" do
expect(person.preferences.first.name).to eq("New Blog")
end

Expand Down Expand Up @@ -4218,7 +4218,7 @@ class BandWithAllowDestroyedRecords < Band
}
end

it "adds the the marked document" do
it "adds the marked document" do
expect(person.preferences.first.name).to eq("New Blog")
end

Expand Down