Skip to content

Commit

Permalink
Only flatten 1 level in push and add to set:
Browse files Browse the repository at this point in the history
[ fix #3093 ]
  • Loading branch information
durran committed Jul 4, 2013
1 parent daa573d commit d52c33b
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ For instructions on upgrading to newer versions, visit

* \#3116 Relations instance variables are now all prefixed with `_`.

* \#3093 Only flatten 1 level when atomically pushing arrays.

* \#3063 `Document#becomes` now properly sets base object on errors.
(Adam Ross Cohen)

Expand Down
4 changes: 2 additions & 2 deletions lib/mongoid/persistable/pushable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def add_to_set(adds)
prepare_atomic_operation do |ops|
process_atomic_operations(adds) do |field, value|
existing = send(field) || (attributes[field] ||= [])
values = [ value ].flatten
values = [ value ].flatten(1)
values.each do |val|
existing.push(val) unless existing.include?(val)
end
Expand All @@ -50,7 +50,7 @@ def push(pushes)
prepare_atomic_operation do |ops|
process_atomic_operations(pushes) do |field, value|
existing = send(field) || (attributes[field] ||= [])
values = [ value ].flatten
values = [ value ].flatten(1)
values.each{ |val| existing.push(val) }
ops[atomic_attribute_name(field)] = { "$each" => values }
end
Expand Down
1 change: 1 addition & 0 deletions spec/app/models/person.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class Person
field :desc, localize: true
field :test_array, type: Array
field :overridden_setter, type: String
field :arrays, type: Array

index age: 1
index addresses: 1
Expand Down
16 changes: 12 additions & 4 deletions spec/mongoid/persistable/pushable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
it "persists absent values" do
expect(person.reload.test_array).to eq([ 1 ])
end

it "flattens only 1 level" do
expect(person.reload.arrays).to eq([[ 7, 8 ]])
end
end

let(:person) do
Expand All @@ -44,7 +48,7 @@
context "when provided string fields" do

let!(:add) do
person.add_to_set("aliases" => 4, "array" => [4, 5], "test_array" => 1)
person.add_to_set("aliases" => 4, "array" => [4, 5], "test_array" => 1, "arrays" => [[ 7, 8 ]])
end

it_behaves_like "a unique pushable root document"
Expand All @@ -53,7 +57,7 @@
context "when provided symbol fields" do

let!(:add) do
person.add_to_set(aliases: 4, array: [4, 5], test_array: 1)
person.add_to_set(aliases: 4, array: [4, 5], test_array: 1, arrays: [[ 7, 8 ]])
end

it_behaves_like "a unique pushable root document"
Expand Down Expand Up @@ -158,6 +162,10 @@
it "persists absent values" do
expect(person.reload.test_array).to eq([ 1 ])
end

it "flattens only 1 level" do
expect(person.reload.arrays).to eq([[ 7, 8 ]])
end
end

let(:person) do
Expand All @@ -167,7 +175,7 @@
context "when provided string fields" do

let!(:push) do
person.push("aliases" => 4, "array" => [ 7, 8 ], "test_array" => 1)
person.push("aliases" => 4, "array" => [ 7, 8 ], "test_array" => 1, "arrays" => [[ 7, 8 ]])
end

it_behaves_like "a pushable root document"
Expand All @@ -176,7 +184,7 @@
context "when provided symbol fields" do

let!(:push) do
person.push(aliases: 4, array: [ 7, 8 ], test_array: 1)
person.push(aliases: 4, array: [ 7, 8 ], test_array: 1, arrays: [[ 7, 8 ]])
end

it_behaves_like "a pushable root document"
Expand Down

0 comments on commit d52c33b

Please sign in to comment.