Permalink
Browse files

Added assumption that a Symbol-based scope should end in _id unless i…

…t does so already and that you can pass vanilla string-based scopes as a parameter

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@183 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 4d6bfc7 commit 87297f1c4be22c10de138709ba2d6c3ace4b0117 @dhh dhh committed Dec 16, 2004
@@ -36,6 +36,8 @@ module ClassMethods
def acts_as_list(options = {})
configuration = { :column => "position", :scope => "1" }
configuration.update(options) if options.is_a?(Hash)
+
+ configuration[:scope] = "#{configuration[:scope]}_id".intern if configuration[:scope].is_a?(Symbol) && configuration[:scope].to_s !~ /_id$/
class_eval <<-EOV
include InstanceMethods
@@ -45,11 +47,7 @@ def position_column
end
def scope_condition
- if configuration[:scope].is_a?(Symbol)
- "#{configuration[:scope]} = \#{#{configuration[:scope]}}"
- else
- configuration[:scope]
- end
+ "#{configuration[:scope].is_a?(Symbol) ? configuration[:scope].to_s + " = \#{" + configuration[:scope].to_s + "}" : configuration[:scope]}"
end
before_destroy :remove_from_list
@@ -9,5 +9,13 @@ class ListMixin < ActiveRecord::Base
def self.table_name
"mixins"
end
-
+end
+
+
+class ListWithStringScopeMixin < ActiveRecord::Base
+ acts_as_list :column => "pos", :scope => 'parent_id = #{parent_id}'
+
+ def self.table_name
+ "mixins"
+ end
end
@@ -28,7 +28,7 @@ def test_reordering
end
- def test_insert
+ def test_insert
new = ListMixin.create("parent_id"=>5)
assert_equal 1, new.pos
assert new.first?
@@ -48,8 +48,7 @@ def test_insert
assert_equal 1, new.pos
assert new.first?
assert new.last?
- end
-
+ end
def test_delete_middle
first = ListMixin.create("parent_id"=>5)
@@ -68,6 +67,12 @@ def test_delete_middle
end
+ def test_with_string_based_scope
+ new = ListWithStringScopeMixin.create("parent_id"=>5)
+ assert_equal 1, new.pos
+ assert new.first?
+ assert new.last?
+ end
end
class TreeTest < Test::Unit::TestCase

0 comments on commit 87297f1

Please sign in to comment.