diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 8408b44672..5ec2e389e0 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,9 @@ *SVN* +* Remove acts_as_list. To use it, install the plugin. [josh, nzkoz] + + http://dev.rubyonrails.org/svn/rails/plugins/acts_as_list/ + * Explicitly require active_record/query_cache before using it. [Jeremy Kemper] * Fix bug where unserializing an attribute attempts to modify a frozen @attributes hash for a deleted record. [Rick, marclove] diff --git a/activerecord/lib/active_record.rb b/activerecord/lib/active_record.rb index 671a8a7ed8..6586b55fe9 100755 --- a/activerecord/lib/active_record.rb +++ b/activerecord/lib/active_record.rb @@ -66,7 +66,6 @@ ActiveRecord::Base.class_eval do include ActiveRecord::Transactions include ActiveRecord::Reflection include ActiveRecord::Acts::Tree - include ActiveRecord::Acts::List include ActiveRecord::Acts::NestedSet include ActiveRecord::Calculations include ActiveRecord::XmlSerialization diff --git a/activerecord/lib/active_record/acts/list.rb b/activerecord/lib/active_record/acts/list.rb deleted file mode 100644 index 22ac28b624..0000000000 --- a/activerecord/lib/active_record/acts/list.rb +++ /dev/null @@ -1,253 +0,0 @@ -module ActiveRecord - module Acts #:nodoc: - module List #:nodoc: - def self.included(base) - base.extend(ClassMethods) - end - - # This +acts_as+ extension provides the capabilities for sorting and reordering a number of objects in a list. - # The class that has this specified needs to have a +position+ column defined as an integer on - # the mapped database table. - # - # Todo list example: - # - # class TodoList < ActiveRecord::Base - # has_many :todo_items, :order => "position" - # end - # - # class TodoItem < ActiveRecord::Base - # belongs_to :todo_list - # acts_as_list :scope => :todo_list - # end - # - # todo_list.first.move_to_bottom - # todo_list.last.move_higher - module ClassMethods - # Configuration options are: - # - # * +column+ - specifies the column name to use for keeping the position integer (default: +position+) - # * +scope+ - restricts what is to be considered a list. Given a symbol, it'll attach _id - # (if it hasn't already been added) and use that as the foreign key restriction. It's also possible - # to give it an entire string that is interpolated if you need a tighter scope than just a foreign key. - # Example: acts_as_list :scope => 'todo_list_id = #{todo_list_id} AND completed = 0' - def acts_as_list(options = {}) - configuration = { :column => "position", :scope => "1 = 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$/ - - if configuration[:scope].is_a?(Symbol) - scope_condition_method = %( - def scope_condition - if #{configuration[:scope].to_s}.nil? - "#{configuration[:scope].to_s} IS NULL" - else - "#{configuration[:scope].to_s} = \#{#{configuration[:scope].to_s}}" - end - end - ) - else - scope_condition_method = "def scope_condition() \"#{configuration[:scope]}\" end" - end - - class_eval <<-EOV - include ActiveRecord::Acts::List::InstanceMethods - - def acts_as_list_class - ::#{self.name} - end - - def position_column - '#{configuration[:column]}' - end - - #{scope_condition_method} - - after_destroy :remove_from_list - before_create :add_to_list_bottom - EOV - end - end - - # All the methods available to a record that has had acts_as_list specified. Each method works - # by assuming the object to be the item in the list, so chapter.move_lower would move that chapter - # lower in the list of all chapters. Likewise, chapter.first? would return +true+ if that chapter is - # the first in the list of all chapters. - module InstanceMethods - # Insert the item at the given position (defaults to the top position of 1). - def insert_at(position = 1) - insert_at_position(position) - end - - # Swap positions with the next lower item, if one exists. - def move_lower - return unless lower_item - - acts_as_list_class.transaction do - lower_item.decrement_position - increment_position - end - end - - # Swap positions with the next higher item, if one exists. - def move_higher - return unless higher_item - - acts_as_list_class.transaction do - higher_item.increment_position - decrement_position - end - end - - # Move to the bottom of the list. If the item is already in the list, the items below it have their - # position adjusted accordingly. - def move_to_bottom - return unless in_list? - acts_as_list_class.transaction do - decrement_positions_on_lower_items - assume_bottom_position - end - end - - # Move to the top of the list. If the item is already in the list, the items above it have their - # position adjusted accordingly. - def move_to_top - return unless in_list? - acts_as_list_class.transaction do - increment_positions_on_higher_items - assume_top_position - end - end - - # Removes the item from the list. - def remove_from_list - decrement_positions_on_lower_items if in_list? - end - - # Increase the position of this item without adjusting the rest of the list. - def increment_position - return unless in_list? - update_attribute position_column, self.send(position_column).to_i + 1 - end - - # Decrease the position of this item without adjusting the rest of the list. - def decrement_position - return unless in_list? - update_attribute position_column, self.send(position_column).to_i - 1 - end - - # Return +true+ if this object is the first in the list. - def first? - return false unless in_list? - self.send(position_column) == 1 - end - - # Return +true+ if this object is the last in the list. - def last? - return false unless in_list? - self.send(position_column) == bottom_position_in_list - end - - # 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_i - 1).to_s}" - ) - 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_i + 1).to_s}" - ) - end - - # Test if this record is in a list - def in_list? - !send(position_column).nil? - end - - private - def add_to_list_top - increment_positions_on_all_items - end - - def add_to_list_bottom - self[position_column] = bottom_position_in_list.to_i + 1 - end - - # Overwrite this method to define the scope of the list changes - def scope_condition() "1" end - - # Returns the bottom position number in the list. - # bottom_position_in_list # => 2 - def bottom_position_in_list(except = nil) - item = bottom_item(except) - item ? item.send(position_column) : 0 - end - - # Returns the bottom item - 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") - end - - # Forces item to assume the bottom position in the list. - def assume_bottom_position - update_attribute(position_column, bottom_position_in_list(self).to_i + 1) - end - - # Forces item to assume the top position in the list. - def assume_top_position - update_attribute(position_column, 1) - end - - # This has the effect of moving all the higher items up one. - def decrement_positions_on_higher_items(position) - acts_as_list_class.update_all( - "#{position_column} = (#{position_column} - 1)", "#{scope_condition} AND #{position_column} <= #{position}" - ) - end - - # This has the effect of moving all the lower items up one. - def decrement_positions_on_lower_items - return unless in_list? - acts_as_list_class.update_all( - "#{position_column} = (#{position_column} - 1)", "#{scope_condition} AND #{position_column} > #{send(position_column).to_i}" - ) - end - - # This has the effect of moving all the higher items down one. - def increment_positions_on_higher_items - return unless in_list? - acts_as_list_class.update_all( - "#{position_column} = (#{position_column} + 1)", "#{scope_condition} AND #{position_column} < #{send(position_column).to_i}" - ) - end - - # This has the effect of moving all the lower items down one. - def increment_positions_on_lower_items(position) - acts_as_list_class.update_all( - "#{position_column} = (#{position_column} + 1)", "#{scope_condition} AND #{position_column} >= #{position}" - ) - end - - # Increments position (position_column) of all items in the list. - def increment_positions_on_all_items - acts_as_list_class.update_all( - "#{position_column} = (#{position_column} + 1)", "#{scope_condition}" - ) - end - - def insert_at_position(position) - remove_from_list - increment_positions_on_lower_items(position) - self.update_attribute(position_column, position) - end - end - end - end -end diff --git a/activerecord/test/fixtures/mixin.rb b/activerecord/test/fixtures/mixin.rb index 7f877a8ce8..5e40c397c0 100644 --- a/activerecord/test/fixtures/mixin.rb +++ b/activerecord/test/fixtures/mixin.rb @@ -15,24 +15,6 @@ class RecursivelyCascadedTreeMixin < Mixin has_one :first_child, :class_name => 'RecursivelyCascadedTreeMixin', :foreign_key => :parent_id end -class ListMixin < Mixin - acts_as_list :column => "pos", :scope => :parent - - def self.table_name() "mixins" end -end - -class ListMixinSub1 < ListMixin -end - -class ListMixinSub2 < ListMixin -end - - -class ListWithStringScopeMixin < ActiveRecord::Base - acts_as_list :column => "pos", :scope => 'parent_id = #{parent_id}' - - def self.table_name() "mixins" end -end class NestedSet < Mixin acts_as_nested_set :scope => "root_id IS NULL" diff --git a/activerecord/test/fixtures/mixins.yml b/activerecord/test/fixtures/mixins.yml index 1f5d3ba60b..6c66eb559c 100644 --- a/activerecord/test/fixtures/mixins.yml +++ b/activerecord/test/fixtures/mixins.yml @@ -59,15 +59,6 @@ recursively_cascaded_tree_4: type: RecursivelyCascadedTreeMixin parent_id: 5007 -# List mixins - -<% (1..4).each do |counter| %> -list_<%= counter %>: - id: <%= counter+1006 %> - pos: <%= counter %> - type: ListMixin - parent_id: 5 -<% end %> # Nested set mixins @@ -117,11 +108,3 @@ tree_<%= set[0] %>: <% end %> -# subclasses of list items -<% (1..4).each do |i| %> -list_sub_<%= i %>: - id: <%= i + 5000 %> - pos: <%= i %> - parent_id: 5000 - type: <%= (i % 2 == 1) ? ListMixinSub1 : ListMixinSub2 %> -<% end %> diff --git a/activerecord/test/mixin_test.rb b/activerecord/test/mixin_test.rb index 44a84f62c8..41310e2c83 100644 --- a/activerecord/test/mixin_test.rb +++ b/activerecord/test/mixin_test.rb @@ -1,6 +1,5 @@ require 'abstract_unit' require 'active_record/acts/tree' -require 'active_record/acts/list' require 'active_record/acts/nested_set' require 'fixtures/mixin' @@ -21,164 +20,6 @@ class Time end end -class ListTest < Test::Unit::TestCase - fixtures :mixins - - def test_reordering - assert_equal mixins(:list_1, :list_2, :list_3, :list_4), - ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos') - - mixins(:list_2).move_lower - - assert_equal mixins(:list_1, :list_3, :list_2, :list_4), - ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos') - - mixins(:list_2).move_higher - - assert_equal mixins(:list_1, :list_2, :list_3, :list_4), - ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos') - - mixins(:list_1).move_to_bottom - - assert_equal mixins(:list_2, :list_3, :list_4, :list_1), - ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos') - - mixins(:list_1).move_to_top - - assert_equal mixins(:list_1, :list_2, :list_3, :list_4), - ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos') - - - mixins(:list_2).move_to_bottom - - assert_equal mixins(:list_1, :list_3, :list_4, :list_2), - ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos') - - mixins(:list_4).move_to_top - - assert_equal mixins(:list_4, :list_1, :list_3, :list_2), - ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos') - - end - - def test_move_to_bottom_with_next_to_last_item - assert_equal mixins(:list_1, :list_2, :list_3, :list_4), - ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos') - - mixins(:list_3).move_to_bottom - - assert_equal mixins(:list_1, :list_2, :list_4, :list_3), - ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos') - end - - def test_next_prev - assert_equal mixins(:list_2), mixins(:list_1).lower_item - assert_nil mixins(:list_1).higher_item - assert_equal mixins(:list_3), mixins(:list_4).higher_item - assert_nil mixins(:list_4).lower_item - end - - - def test_injection - item = ListMixin.new("parent_id"=>1) - assert_equal "parent_id = 1", item.scope_condition - assert_equal "pos", item.position_column - end - - def test_insert - new = ListMixin.create("parent_id"=>20) - assert_equal 1, new.pos - assert new.first? - assert new.last? - - new = ListMixin.create("parent_id"=>20) - assert_equal 2, new.pos - assert !new.first? - assert new.last? - - new = ListMixin.create("parent_id"=>20) - assert_equal 3, new.pos - assert !new.first? - assert new.last? - - new = ListMixin.create("parent_id"=>0) - assert_equal 1, new.pos - assert new.first? - assert new.last? - end - - def test_insert_at - new = ListMixin.create("parent_id" => 20) - assert_equal 1, new.pos - - new = ListMixin.create("parent_id" => 20) - assert_equal 2, new.pos - - new = ListMixin.create("parent_id" => 20) - assert_equal 3, new.pos - - new4 = ListMixin.create("parent_id" => 20) - assert_equal 4, new4.pos - - new4.insert_at(3) - assert_equal 3, new4.pos - - new.reload - assert_equal 4, new.pos - - new.insert_at(2) - assert_equal 2, new.pos - - new4.reload - assert_equal 4, new4.pos - - new5 = ListMixin.create("parent_id" => 20) - assert_equal 5, new5.pos - - new5.insert_at(1) - assert_equal 1, new5.pos - - new4.reload - assert_equal 5, new4.pos - end - - def test_delete_middle - assert_equal mixins(:list_1, :list_2, :list_3, :list_4), - ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos') - - mixins(:list_2).destroy - - assert_equal mixins(:list_1, :list_3, :list_4, :reload), - ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos') - - assert_equal 1, mixins(:list_1).pos - assert_equal 2, mixins(:list_3).pos - assert_equal 3, mixins(:list_4).pos - - mixins(:list_1).destroy - - assert_equal mixins(:list_3, :list_4, :reload), - ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos') - - assert_equal 1, mixins(:list_3).pos - assert_equal 2, mixins(:list_4).pos - - end - - def test_with_string_based_scope - new = ListWithStringScopeMixin.create("parent_id"=>500) - assert_equal 1, new.pos - assert new.first? - assert new.last? - end - - def test_nil_scope - new1, new2, new3 = ListMixin.create, ListMixin.create, ListMixin.create - new2.move_higher - assert_equal [new2, new1, new3], ListMixin.find(:all, :conditions => 'parent_id IS NULL', :order => 'pos') - end - -end class TreeTest < Test::Unit::TestCase fixtures :mixins @@ -339,128 +180,5 @@ class TouchTest < Test::Unit::TestCase end -class ListSubTest < Test::Unit::TestCase - fixtures :mixins - - def test_reordering - assert_equal mixins(:list_sub_1, :list_sub_2, :list_sub_3, :list_sub_4), - ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos') - - mixins(:list_sub_2).move_lower - - assert_equal mixins(:list_sub_1, :list_sub_3, :list_sub_2, :list_sub_4), - ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos') - - mixins(:list_sub_2).move_higher - - assert_equal mixins(:list_sub_1, :list_sub_2, :list_sub_3, :list_sub_4), - ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos') - - mixins(:list_sub_1).move_to_bottom - - assert_equal mixins(:list_sub_2, :list_sub_3, :list_sub_4, :list_sub_1), - ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos') - - mixins(:list_sub_1).move_to_top - - assert_equal mixins(:list_sub_1, :list_sub_2, :list_sub_3, :list_sub_4), - ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos') - mixins(:list_sub_2).move_to_bottom - - assert_equal mixins(:list_sub_1, :list_sub_3, :list_sub_4, :list_sub_2), - ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos') - - mixins(:list_sub_4).move_to_top - - assert_equal mixins(:list_sub_4, :list_sub_1, :list_sub_3, :list_sub_2), - ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos') - - end - - def test_move_to_bottom_with_next_to_last_item - assert_equal mixins(:list_sub_1, :list_sub_2, :list_sub_3, :list_sub_4), - ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos') - - mixins(:list_sub_3).move_to_bottom - - assert_equal mixins(:list_sub_1, :list_sub_2, :list_sub_4, :list_sub_3), - ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos') - end - - def test_next_prev - assert_equal mixins(:list_sub_2), mixins(:list_sub_1).lower_item - assert_nil mixins(:list_sub_1).higher_item - assert_equal mixins(:list_sub_3), mixins(:list_sub_4).higher_item - assert_nil mixins(:list_sub_4).lower_item - end - - - def test_injection - item = ListMixin.new("parent_id"=>1) - assert_equal "parent_id = 1", item.scope_condition - assert_equal "pos", item.position_column - end - - - def test_insert_at - new = ListMixin.create("parent_id" => 20) - assert_equal 1, new.pos - - new = ListMixinSub1.create("parent_id" => 20) - assert_equal 2, new.pos - - new = ListMixinSub2.create("parent_id" => 20) - assert_equal 3, new.pos - - new4 = ListMixin.create("parent_id" => 20) - assert_equal 4, new4.pos - - new4.insert_at(3) - assert_equal 3, new4.pos - - new.reload - assert_equal 4, new.pos - - new.insert_at(2) - assert_equal 2, new.pos - - new4.reload - assert_equal 4, new4.pos - - new5 = ListMixinSub1.create("parent_id" => 20) - assert_equal 5, new5.pos - - new5.insert_at(1) - assert_equal 1, new5.pos - - new4.reload - assert_equal 5, new4.pos - end - - def test_delete_middle - assert_equal mixins(:list_sub_1, :list_sub_2, :list_sub_3, :list_sub_4), - ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos') - - mixins(:list_sub_2).destroy - - assert_equal mixins(:list_sub_1, :list_sub_3, :list_sub_4, :reload), - ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos') - - assert_equal 1, mixins(:list_sub_1).pos - assert_equal 2, mixins(:list_sub_3).pos - assert_equal 3, mixins(:list_sub_4).pos - - mixins(:list_sub_1).destroy - - assert_equal mixins(:list_sub_3, :list_sub_4, :reload), - ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos') - - assert_equal 1, mixins(:list_sub_3).pos - assert_equal 2, mixins(:list_sub_4).pos - - end - -end -