Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Support default_scope ordering by calling .unscoped when retrieving t…

…he highest current position value for a model in the list
  • Loading branch information...
commit f233643018ad9a2791bba0cf6dd48ff90633230e 1 parent 819c37d
@tanordheim tanordheim authored
Showing with 93 additions and 1 deletion.
  1. +1 −1  lib/acts_as_list/active_record/acts/list.rb
  2. +92 −0 test/test_list.rb
View
2  lib/acts_as_list/active_record/acts/list.rb
@@ -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.
View
92 test/test_list.rb
@@ -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
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.