Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Rails3 #5

Closed
wants to merge 2 commits into from

1 participant

@MissingHandle

Creates Rails3 Branch
Fixes default_scope issue

Never made a pull request before so lemme know if i screwed something up..as you probably will?

MissingHandle added some commits
@MissingHandle MissingHandle Created Rails 3 branch
Fixed so that acts_as_list works correctly when a default_scope of ordering on the position column is present on the model.
  modified:   lib/acts_as_list.rb
  modified:   test/list_test.rb
82d0999
@MissingHandle MissingHandle Added Comment to default_scope test
  modified:   test/list_test.rb
cb2c112
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 14, 2010
  1. @MissingHandle

    Created Rails 3 branch

    MissingHandle authored
    Fixed so that acts_as_list works correctly when a default_scope of ordering on the position column is present on the model.
      modified:   lib/acts_as_list.rb
      modified:   test/list_test.rb
  2. @MissingHandle

    Added Comment to default_scope test

    MissingHandle authored
      modified:   test/list_test.rb
This page is out of date. Refresh to see the latest.
Showing with 36 additions and 9 deletions.
  1. +13 −7 lib/acts_as_list.rb
  2. +23 −2 test/list_test.rb
View
20 lib/acts_as_list.rb
@@ -152,17 +152,21 @@ def last?
# Return the next higher item in the list.
def higher_item
return nil unless in_list?
- acts_as_list_class.find(:first, :conditions =>
- "#{scope_condition} AND #{position_column} < #{send(position_column).to_s}", :order => "#{position_column} DESC"
- )
+ acts_as_list_class.send(:with_exclusive_scope) do
+ acts_as_list_class.find(:first, :conditions =>
+ "#{scope_condition} AND #{position_column} < #{send(position_column).to_s}", :order => "#{position_column} DESC"
+ )
+ end
end
# Return the next lower item in the list.
def lower_item
return nil unless in_list?
- acts_as_list_class.find(:first, :conditions =>
- "#{scope_condition} AND #{position_column} > #{send(position_column).to_s}", :order => "#{position_column} ASC"
- )
+ acts_as_list_class.send(:with_exclusive_scope) do
+ acts_as_list_class.find(:first, :conditions =>
+ "#{scope_condition} AND #{position_column} > #{send(position_column).to_s}", :order => "#{position_column} ASC"
+ )
+ end
end
# Test if this record is in a list
@@ -193,7 +197,9 @@ 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.send(:with_exclusive_scope) do
+ acts_as_list_class.find(:first, :conditions => conditions, :order => "#{position_column} DESC")
+ end
end
# Forces item to assume the bottom position in the list.
View
25 test/list_test.rb
@@ -6,7 +6,10 @@
require "#{File.dirname(__FILE__)}/../lib/acts_as_list"
-ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :dbfile => ":memory:")
+# For ActiveRecord >= 3.0.0
+ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
+# For ActiveRecord < 3.0.0
+# ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :dbfile => ":memory:")
def setup_db
ActiveRecord::Schema.define(:version => 1) do
@@ -48,6 +51,12 @@ def self.table_name() "mixins" end
class ListTest < Test::Unit::TestCase
+
+ def add_troublesome_default_scope
+ ListMixin.class_eval do
+ default_scope order(:pos)
+ end
+ end
def setup
setup_db
@@ -258,6 +267,18 @@ def test_delete_middle_with_holes
assert_equal 2, ListMixin.find(4).pos
assert_equal 3, ListMixin.find(3).pos
end
+
+ def test_inserts_correctly_when_class_has_a_default_scope_ordering_by_position
+ #default_scope also added in add_troublesome_scope method so that it also covers all other tests
+ #duplicated code in case rest of code changes and this does not.
+ ListMixin.class_eval do
+ default_scope order(:pos)
+ end
+ 2.times { ListMixin.create! :parent_id => 5678 }
+ @three = ListMixin.create! :parent_id => 5678
+ assert_equal 3, @three.pos
+ end
+
end
class ListSubTest < Test::Unit::TestCase
@@ -366,4 +387,4 @@ def test_delete_middle
assert_equal 2, ListMixin.find(4).pos
end
-end
+end
Something went wrong with that request. Please try again.