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

Add tests for moving within scope and add method: move_within_scope #79

Merged
merged 1 commit into from
Apr 9, 2013
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/acts_as_list/active_record/acts/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,12 @@ def remove_from_list
end
end

# Move the item within scope
def move_within_scope(scope_id)
send("#{scope_name}=", scope_id)
save!
end

# Increase the position of this item without adjusting the rest of the list.
def increment_position
return unless in_list?
Expand Down Expand Up @@ -409,6 +415,7 @@ def check_scope
self["#{scope_name}"] = old_scope_id
send("decrement_positions_on_lower_items")
self["#{scope_name}"] = new_scope_id
send("#{position_column}=", nil)
send("add_to_list_#{add_new_at}")
end
end
Expand Down
20 changes: 20 additions & 0 deletions test/shared_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ def test_insert_at
def test_delete_middle
assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)



ListMixin.find(2).destroy

assert_equal [1, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
Expand Down Expand Up @@ -138,6 +140,24 @@ def test_nil_scope
assert_equal [new2, new1, new3], ListMixin.find(:all, :conditions => 'parent_id IS NULL', :order => 'pos')
end

def test_update_position_when_scope_changes
assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
parent = ListMixin.create(:id => 6)

ListMixin.find(2).move_within_scope(6)

assert_equal 1, ListMixin.find(2).pos

assert_equal [1, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)

assert_equal 1, ListMixin.find(1).pos
assert_equal 2, ListMixin.find(3).pos
assert_equal 3, ListMixin.find(4).pos

ListMixin.find(2).move_within_scope(5)
assert_equal [1, 3, 4, 2], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
end

def test_remove_from_list_should_then_fail_in_list?
assert_equal true, ListMixin.find(1).in_list?
ListMixin.find(1).remove_from_list
Expand Down