Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

- Finishing off serialization refactor.

- Any type can now be localized. Closes #1558.
- Serialization now only needs a #mongoize instance method and
  .demongoize class method on any object to be able to be a custom type.
  Closes #1604.
  • Loading branch information...
commit 56151a25fff83eb09fcd4177f8ea8217b5974d1d 1 parent d1a770b
@durran durran authored
View
6 lib/mongoid/extensions/float.rb
@@ -21,7 +21,11 @@ module ClassMethods
#
# @since 3.0.0
def mongoize(object)
- object ? (__numeric__(object) rescue 0.0) : object
+ unless object.blank?
+ __numeric__(object) rescue 0.0
+ else
+ nil
+ end
end
end
end
View
6 lib/mongoid/extensions/integer.rb
@@ -31,7 +31,11 @@ module ClassMethods
#
# @since 3.0.0
def mongoize(object)
- object ? (__numeric__(object).to_i rescue 0) : object
+ unless object.blank?
+ __numeric__(object).to_i rescue 0
+ else
+ nil
+ end
end
end
end
View
40 lib/mongoid/fields/foreign_key.rb
@@ -3,6 +3,42 @@ module Mongoid
module Fields
class ForeignKey < Standard
+ # Adds the atomic changes for this type of resizable field.
+ #
+ # @example Add the atomic changes.
+ # field.add_atomic_changes(doc, "key", {}, [], [])
+ #
+ # @todo: Durran: Refactor, big time.
+ #
+ # @param [ Document ] document The document to add to.
+ # @param [ String ] name The name of the field.
+ # @param [ String ] key The atomic location of the field.
+ # @param [ Hash ] mods The current modifications.
+ # @param [ Array ] new The new elements to add.
+ # @param [ Array ] old The old elements getting removed.
+ #
+ # @since 2.4.0
+ def add_atomic_changes(document, name, key, mods, new_elements, old_elements)
+ old = (old_elements || [])
+ new = (new_elements || [])
+ if new.length > old.length
+ if new.first(old.length) == old
+ document.atomic_array_add_to_sets[key] = new.drop(old.length)
+ else
+ mods[key] = document.attributes[name]
+ end
+ elsif new.length < old.length
+ pulls = old - new
+ if new == old - pulls
+ document.atomic_array_pulls[key] = pulls
+ else
+ mods[key] = document.attributes[name]
+ end
+ elsif new != old
+ mods[key] = document.attributes[name]
+ end
+ end
+
# Is this field a foreign key?
#
# @example Is the field a foreign key?
@@ -15,6 +51,10 @@ def foreign_key?
true
end
+ def evolve(object)
+ object.is_a?(Document) ? object.id : mongoize(object)
+ end
+
def mongoize(object)
type.__mongoize_fk__(constraint, object, object_id_field?)
end
View
4 spec/mongoid/extensions/float_spec.rb
@@ -91,8 +91,8 @@
context "when the string is empty" do
- it "returns 0" do
- Float.mongoize("").should eq(0)
+ it "returns nil" do
+ Float.mongoize("").should be_nil
end
end
View
4 spec/mongoid/extensions/integer_spec.rb
@@ -91,8 +91,8 @@
context "when the string is empty" do
- it "returns 0" do
- Integer.mongoize("").should eq(0)
+ it "returns nil" do
+ Integer.mongoize("").should be_nil
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.