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

Production Branch #1

Draft
wants to merge 14 commits into
base: mongoid-4-w-patches
Choose a base branch
from
Draft
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
3 changes: 2 additions & 1 deletion lib/mongoid/attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@ def write_attribute(name, value)
attribute_will_change!(access)
end
if localized
(attributes[access] ||= {}).merge!(typed_value)
attributes[access] ||= {}
attributes[access].merge!(typed_value)
else
attributes[access] = typed_value
end
Expand Down
3 changes: 3 additions & 0 deletions lib/mongoid/attributes/processing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ def pending_nested
#
# @since 2.0.0.rc.7
def process_attribute(name, value)
if !respond_to?("#{name}=", true) && store_as = aliased_fields.invert[name.to_s]
name = store_as
end
responds = respond_to?("#{name}=")
raise Errors::UnknownAttribute.new(self.class, name) unless responds
send("#{name}=", value)
Expand Down
5 changes: 1 addition & 4 deletions lib/mongoid/contextual/memory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ class Memory
include Aggregable::Memory
include Relations::Eager
include Queryable
include Positional

# @attribute [r] root The root document.
# @attribute [r] path The atomic path.
Expand Down Expand Up @@ -47,9 +46,7 @@ def delete
doc.as_document
end
unless removed.empty?
collection.find(selector).update(
positionally(selector, "$pullAll" => { path => removed })
)
collection.find(selector).update("$pullAll" => { path => removed })
end
deleted
end
Expand Down
34 changes: 30 additions & 4 deletions lib/mongoid/copyable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,21 @@ def clone
# _id and id field in the document would cause problems with Mongoid
# elsewhere.
attrs = clone_document.except("_id", "id")
self.class.new(attrs)
dynamic_attrs = {}
attrs.reject! do |attr_name, value|
dynamic_attrs.merge!(attr_name => value) unless self.attribute_names.include?(attr_name)
end
self.class.new(attrs).tap do |object|
dynamic_attrs.each do |attr_name, value|
if object.respond_to?("#{attr_name}=")
object.send("#{attr_name}=", value)
elsif (store_as = aliased_fields.invert[attr_name.to_s])
object.send("#{store_as}=", value)
else
object.attributes[attr_name] = value
end
end
end
end
alias :dup :clone

Expand All @@ -40,7 +54,7 @@ def clone
# @since 3.0.22
def clone_document
attrs = as_document.__deep_copy__
process_localized_attributes(attrs)
process_localized_attributes(self, attrs)
attrs
end

Expand All @@ -55,12 +69,24 @@ def clone_document
# @param [ Hash ] attrs The attributes.
#
# @since 3.0.20
def process_localized_attributes(attrs)
localized_fields.keys.each do |name|
def process_localized_attributes(klass, attrs)
klass.localized_fields.keys.each do |name|
if value = attrs.delete(name)
attrs["#{name}_translations"] = value
end
end
klass.embedded_relations.each do |_, metadata|
next unless attrs.present? && attrs[metadata.key].present?

if metadata.macro == :embeds_many
attrs[metadata.key].each do |attr|
embedded_klass = attr.fetch('_type', metadata.class_name).constantize
process_localized_attributes(embedded_klass, attr)
end
else
process_localized_attributes(metadata.klass, attrs[metadata.key])
end
end
end
end
end
1 change: 0 additions & 1 deletion lib/mongoid/document.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# encoding: utf-8
require "mongoid/positional"
require "mongoid/evolvable"
require "mongoid/extensions"
require "mongoid/errors"
Expand Down
3 changes: 1 addition & 2 deletions lib/mongoid/persistable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ module Persistable
include Incrementable
include Logical
include Poppable
include Positional
include Pullable
include Pushable
include Renamable
Expand Down Expand Up @@ -209,7 +208,7 @@ def persist_or_delay_atomic_operation(operation)
def persist_atomic_operations(operations)
if persisted?
selector = atomic_selector
_root.collection.find(selector).update(positionally(selector, operations))
_root.collection.find(selector).update(operations)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/mongoid/persistable/creatable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def insert_as_embedded
_parent.insert
else
selector = _parent.atomic_selector
_root.collection.find(selector).update(positionally(selector, atomic_inserts))
_root.collection.find(selector).update(atomic_inserts)
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/mongoid/persistable/deletable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def delete_as_embedded(options = {})
_parent.remove_child(self) if notifying_parent?(options)
if _parent.persisted?
selector = _parent.atomic_selector
_root.collection.find(selector).update(positionally(selector, atomic_deletes))
_root.collection.find(selector).update(atomic_deletes)
end
true
end
Expand Down
4 changes: 2 additions & 2 deletions lib/mongoid/persistable/updatable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,9 @@ def update_document(options = {})
unless updates.empty?
coll = _root.collection
selector = atomic_selector
coll.find(selector).update(positionally(selector, updates))
coll.find(selector).update(updates)
conflicts.each_pair do |key, value|
coll.find(selector).update(positionally(selector, { key => value }))
coll.find(selector).update({ key => value })
end
end
end
Expand Down
71 changes: 0 additions & 71 deletions lib/mongoid/positional.rb

This file was deleted.

13 changes: 3 additions & 10 deletions lib/mongoid/relations/embedded/batchable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ module Embedded
# Contains behaviour for executing operations in batch on embedded
# documents.
module Batchable
include Positional

# Insert new documents as a batch push ($pushAll). This ensures that
# all callbacks are run at the appropriate time and only 1 request is
Expand Down Expand Up @@ -37,9 +36,7 @@ def batch_insert(docs)
def batch_clear(docs)
pre_process_batch_remove(docs, :delete)
unless docs.empty?
collection.find(selector).update(
positionally(selector, "$unset" => { path => true })
)
collection.find(selector).update("$unset" => { path => true })
post_process_batch_remove(docs, :delete)
end
_unscoped.clear
Expand All @@ -57,9 +54,7 @@ def batch_clear(docs)
def batch_remove(docs, method = :delete)
removals = pre_process_batch_remove(docs, method)
if !docs.empty?
collection.find(selector).update(
positionally(selector, "$pullAll" => { path => removals })
)
collection.find(selector).update("$pullAll" => { path => removals })
post_process_batch_remove(docs, method)
end
reindex
Expand Down Expand Up @@ -130,9 +125,7 @@ def execute_batch_insert(docs, operation)
self.inserts_valid = true
inserts = pre_process_batch_insert(docs)
if insertable?
collection.find(selector).update(
positionally(selector, operation => { path => inserts })
)
collection.find(selector).update(operation => { path => inserts })
post_process_batch_insert(docs)
end
inserts
Expand Down
2 changes: 1 addition & 1 deletion lib/mongoid/relations/touchable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def touch(field = nil)
touches = touch_atomic_updates(field)
unless touches.empty?
selector = atomic_selector
_root.collection.where(selector).update(positionally(selector, touches))
_root.collection.find(selector).update(touches)
end
run_callbacks(:touch)
true
Expand Down
3 changes: 3 additions & 0 deletions spec/app/models/address_customized.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class AddressCustomized < Address
field :alternative_name, localize: true
end
4 changes: 4 additions & 0 deletions spec/app/models/courier_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class CourierJob
include Mongoid::Document
embeds_one :drop_address, as: :addressable, autobuild: true, class_name: "ShipmentAddress"
end
2 changes: 2 additions & 0 deletions spec/app/models/shipment_address.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class ShipmentAddress < Address
end
5 changes: 5 additions & 0 deletions spec/app/models/store_as_dup_test1.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class StoreAsDupTest1
include Mongoid::Document
embeds_one :store_as_dup_test2, :store_as => :t
field :name
end
5 changes: 5 additions & 0 deletions spec/app/models/store_as_dup_test2.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class StoreAsDupTest2
include Mongoid::Document
embedded_in :store_as_dup_test1
field :name
end
7 changes: 7 additions & 0 deletions spec/app/models/store_as_dup_test3.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class StoreAsDupTest3
include Mongoid::Document
embeds_many :store_as_dup_test4s, :store_as => :t
field :name
end
7 changes: 7 additions & 0 deletions spec/app/models/store_as_dup_test4.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class StoreAsDupTest4
include Mongoid::Document
embedded_in :store_as_dup_test3
field :name
end
13 changes: 13 additions & 0 deletions spec/mongoid/attributes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1207,6 +1207,19 @@
}.to raise_error(Mongoid::Errors::InvalidValue)
end
end

context "when attribute is localized and #attributes is a BSON::Document" do
let(:dictionary) { Dictionary.new }

before do
allow(dictionary).to receive(:attributes).and_return(BSON::Document.new)
end

it "sets the value for the current locale" do
dictionary.write_attribute(:description, 'foo')
expect(dictionary.description).to eq('foo')
end
end
end

describe "#typed_value_for" do
Expand Down
Loading