diff options
-rw-r--r-- | activerecord/lib/active_record/mixins/list.rb | 8 | ||||
-rw-r--r-- | activerecord/test/fixtures/mixin.rb | 10 | ||||
-rw-r--r-- | activerecord/test/mixin_test.rb | 11 |
3 files changed, 20 insertions, 9 deletions
diff --git a/activerecord/lib/active_record/mixins/list.rb b/activerecord/lib/active_record/mixins/list.rb index a1d914d73c..f092347478 100644 --- a/activerecord/lib/active_record/mixins/list.rb +++ b/activerecord/lib/active_record/mixins/list.rb @@ -36,6 +36,8 @@ module ActiveRecord 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 @@ module ActiveRecord 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 diff --git a/activerecord/test/fixtures/mixin.rb b/activerecord/test/fixtures/mixin.rb index 6da85e55e7..0c3bf5000a 100644 --- a/activerecord/test/fixtures/mixin.rb +++ b/activerecord/test/fixtures/mixin.rb @@ -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
\ No newline at end of file diff --git a/activerecord/test/mixin_test.rb b/activerecord/test/mixin_test.rb index 107d6d2f0a..58e7ff1807 100644 --- a/activerecord/test/mixin_test.rb +++ b/activerecord/test/mixin_test.rb @@ -28,7 +28,7 @@ class ListTest < Test::Unit::TestCase 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 @@ class ListTest < Test::Unit::TestCase 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 @@ class ListTest < Test::Unit::TestCase 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 |