Skip to content

Commit

Permalink
Merge pull request rails#13 from tanordheim/default_scope_fix
Browse files Browse the repository at this point in the history
Support default_scope ordering by calling .unscoped
  • Loading branch information
swanandp committed Aug 25, 2011
2 parents 819c37d + f233643 commit 42f73a6
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/acts_as_list/active_record/acts/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ def bottom_position_in_list(except = nil)
def bottom_item(except = nil)
conditions = scope_condition
conditions = "#{conditions} AND #{self.class.primary_key} != #{except.id}" if except
acts_as_list_class.find(:first, :conditions => conditions, :order => "#{position_column} DESC")
acts_as_list_class.unscoped.find(:first, :conditions => conditions, :order => "#{position_column} DESC")
end

# Forces item to assume the bottom position in the list.
Expand Down
92 changes: 92 additions & 0 deletions test/test_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ class ZeroBasedMixin < ActiveRecord::Base
def self.table_name() "mixins" end
end

class DefaultScopedMixin < ActiveRecord::Base
set_table_name 'mixins'
acts_as_list :column => "pos"
default_scope order('pos ASC')
end

class ZeroBasedTest < Test::Unit::TestCase
def setup
setup_db
Expand Down Expand Up @@ -1248,3 +1254,89 @@ def test_remove_before_destroy_does_not_shift_lower_items_twice
end

end

class DefaultScopedTest < Test::Unit::TestCase
def setup
setup_db
(1..4).each { |counter| DefaultScopedMixin.create! :pos => counter }
end

def teardown
teardown_db
end

def test_insert
new = DefaultScopedMixin.create
assert_equal 5, new.pos
assert !new.first?
assert new.last?

new = DefaultScopedMixin.create
assert_equal 6, new.pos
assert !new.first?
assert new.last?

new = DefaultScopedMixin.create
assert_equal 7, new.pos
assert !new.first?
assert new.last?
end

def test_reordering
assert_equal [1, 2, 3, 4], DefaultScopedMixin.find(:all).map(&:id)

DefaultScopedMixin.find(2).move_lower
assert_equal [1, 3, 2, 4], DefaultScopedMixin.find(:all).map(&:id)

DefaultScopedMixin.find(2).move_higher
assert_equal [1, 2, 3, 4], DefaultScopedMixin.find(:all).map(&:id)

DefaultScopedMixin.find(1).move_to_bottom
assert_equal [2, 3, 4, 1], DefaultScopedMixin.find(:all).map(&:id)

DefaultScopedMixin.find(1).move_to_top
assert_equal [1, 2, 3, 4], DefaultScopedMixin.find(:all).map(&:id)

DefaultScopedMixin.find(2).move_to_bottom
assert_equal [1, 3, 4, 2], DefaultScopedMixin.find(:all).map(&:id)

DefaultScopedMixin.find(4).move_to_top
assert_equal [4, 1, 3, 2], DefaultScopedMixin.find(:all).map(&:id)
end

def test_insert_at
new = DefaultScopedMixin.create
assert_equal 5, new.pos

new = DefaultScopedMixin.create
assert_equal 6, new.pos

new = DefaultScopedMixin.create
assert_equal 7, new.pos

new4 = DefaultScopedMixin.create
assert_equal 8, new4.pos

new4.insert_at(2)
assert_equal 2, new4.pos

new.reload
assert_equal 8, new.pos

new.insert_at(2)
assert_equal 2, new.pos

new4.reload
assert_equal 3, new4.pos

new5 = DefaultScopedMixin.create
assert_equal 9, new5.pos

new5.insert_at(1)
assert_equal 1, new5.pos

new4.reload
assert_equal 4, new4.pos
end

end

0 comments on commit 42f73a6

Please sign in to comment.