Skip to content

Commit

Permalink
Allow reordering of many to many foreign keys. Fixes #1705.
Browse files Browse the repository at this point in the history
  • Loading branch information
durran committed Feb 11, 2012
1 parent 16c3748 commit 46bee94
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -245,6 +245,8 @@ For instructions on upgrading to newer versions, visit

### Resolved Issues

* \#1705 Allow changing the order of many to many foreign keys.

* \#1703 Updated at is now versioned again. (Lucas Souza)

* \#1686 Set the base metadata on unbind as well as bind for belongs to
Expand Down
4 changes: 4 additions & 0 deletions lib/mongoid/fields/internal/foreign_keys/array.rb
Expand Up @@ -13,6 +13,8 @@ class Array
# @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.
Expand All @@ -32,6 +34,8 @@ def add_atomic_changes(document, name, key, mods, new, old)
document.atomic_array_add_to_sets[key] = pushes
elsif !pulls.empty?
document.atomic_array_pulls[key] = pulls
elsif new != old
mods[key] = document.attributes[name]
end
end

Expand Down
36 changes: 36 additions & 0 deletions spec/mongoid/relations/referenced/many_to_many_spec.rb
Expand Up @@ -2562,4 +2562,40 @@
end
end
end

context "when changing the order of existing ids" do

let(:person) do
Person.new
end

let(:preference_one) do
Preference.create(:name => "one")
end

let(:preference_two) do
Preference.create(:name => "two")
end

before do
person.preference_ids = [ preference_one.id, preference_two.id ]
person.save
end

context "and the order is changed" do

before do
person.preference_ids = [ preference_two.id, preference_one.id ]
person.save
end

let(:reloaded) do
Person.find(person.id)
end

it "persists the change in id order" do
reloaded.preference_ids.should eq([ preference_two.id, preference_one.id ])
end
end
end
end

0 comments on commit 46bee94

Please sign in to comment.