diff options
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r-- | activerecord/lib/active_record/mixins/list.rb | 221 | ||||
-rw-r--r-- | activerecord/lib/active_record/mixins/touch.rb | 34 | ||||
-rw-r--r-- | activerecord/lib/active_record/mixins/tree.rb | 18 |
3 files changed, 129 insertions, 144 deletions
diff --git a/activerecord/lib/active_record/mixins/list.rb b/activerecord/lib/active_record/mixins/list.rb index bec4b33651..16a60d7b6c 100644 --- a/activerecord/lib/active_record/mixins/list.rb +++ b/activerecord/lib/active_record/mixins/list.rb @@ -28,141 +28,156 @@ module ActiveRecord # todo_list.last.move_higher module List def self.append_features(base) - super - base.before_destroy :remove_from_list - base.after_create :add_to_list_bottom + super + base.extend(ClassMethods) end - # can be overriden - - def position_column - "position" - end + module ClassMethods + def acts_as_list(options = {}) + configuration = { :column => "position", :scope => "1" } + configuration.update(options) if options.is_a?(Hash) + + class_eval <<-EOV + include InstanceMethods - # Moving around on the list + def position_column + '#{configuration[:column]}' + end - def move_lower - return unless lower_item - - self.class.transaction do - lower_item.decrement_position - increment_position + def scope_condition + "#{configuration[:scope]} = \#{#{configuration[:scope]}}" + end + + before_destroy :remove_from_list + after_create :add_to_list_bottom + EOV end - end + + module InstanceMethods + def move_lower + return unless lower_item - def move_higher - return unless higher_item + self.class.transaction do + lower_item.decrement_position + increment_position + end + end - self.class.transaction do - higher_item.increment_position - decrement_position - end - end + def move_higher + return unless higher_item + + self.class.transaction do + higher_item.increment_position + decrement_position + end + end - def move_to_bottom - self.class.transaction do - decrement_positions_on_lower_items - assume_bottom_position - end - end + def move_to_bottom + self.class.transaction do + decrement_positions_on_lower_items + assume_bottom_position + end + end - def move_to_top - self.class.transaction do - increment_positions_on_higher_items - assume_top_position - end - end + def move_to_top + self.class.transaction do + increment_positions_on_higher_items + assume_top_position + end + end - # Entering or existing the list + # Entering or existing the list - def add_to_list_top - increment_positions_on_all_items - end + def add_to_list_top + increment_positions_on_all_items + end - def add_to_list_bottom - assume_bottom_position - end + def add_to_list_bottom + assume_bottom_position + end - def remove_from_list - decrement_positions_on_lower_items - end + def remove_from_list + decrement_positions_on_lower_items + end - # Changing the position + # Changing the position - def increment_position - update_attribute position_column, self.send(position_column).to_i + 1 - end + def increment_position + update_attribute position_column, self.send(position_column).to_i + 1 + end - def decrement_position - update_attribute position_column, self.send(position_column).to_i - 1 - end + def decrement_position + update_attribute position_column, self.send(position_column).to_i - 1 + end - # Querying the position + # Querying the position - def first? - self.send(position_column) == 1 - end + def first? + self.send(position_column) == 1 + end - def last? - self.send(position_column) == bottom_position_in_list - end + def last? + self.send(position_column) == bottom_position_in_list + end - private - # Overwrite this method to define the scope of the list changes - def scope_condition() "1" end + private + # Overwrite this method to define the scope of the list changes + def scope_condition() "1" end - def higher_item - self.class.find_first( - "#{scope_condition} AND #{position_column} = #{(send(position_column).to_i - 1).to_s}" - ) - end + def higher_item + self.class.find_first( + "#{scope_condition} AND #{position_column} = #{(send(position_column).to_i - 1).to_s}" + ) + end - def lower_item - self.class.find_first( - "#{scope_condition} AND #{position_column} = #{(send(position_column).to_i + 1).to_s}" - ) - end + def lower_item + self.class.find_first( + "#{scope_condition} AND #{position_column} = #{(send(position_column).to_i + 1).to_s}" + ) + end - def bottom_position_in_list - item = bottom_item - item ? item.send(position_column) : 0 - end + def bottom_position_in_list + item = bottom_item + item ? item.send(position_column) : 0 + end - def bottom_item - self.class.find_first( - "#{scope_condition} ", - "#{position_column} DESC" - ) - end + def bottom_item + self.class.find_first( + "#{scope_condition} ", + "#{position_column} DESC" + ) + end - def assume_bottom_position - update_attribute position_column, bottom_position_in_list.to_i + 1 - end + def assume_bottom_position + update_attribute position_column, bottom_position_in_list.to_i + 1 + end - def assume_top_position - update_attribute position_column, 1 - end + def assume_top_position + update_attribute position_column, 1 + end - def decrement_positions_on_lower_items - self.class.update_all( - "#{position_column} = (#{position_column} - 1)", "#{scope_condition} AND #{position_column} > #{send(position_column).to_i}" - ) - end + def decrement_positions_on_lower_items + self.class.update_all( + "#{position_column} = (#{position_column} - 1)", "#{scope_condition} AND #{position_column} > #{send(position_column).to_i}" + ) + end - def increment_positions_on_higher_items - self.class.update_all( - "#{position_column} = (#{position_column} + 1)", "#{scope_condition} AND #{position_column} < #{send(position_column)}" - ) - end + def increment_positions_on_higher_items + self.class.update_all( + "#{position_column} = (#{position_column} + 1)", "#{scope_condition} AND #{position_column} < #{send(position_column)}" + ) + end - def increment_positions_on_all_items - self.class.update_all( - "#{position_column} = (#{position_column} + 1)", "#{scope_condition}" - ) - end + def increment_positions_on_all_items + self.class.update_all( + "#{position_column} = (#{position_column} + 1)", "#{scope_condition}" + ) + end + end + end end end end
\ No newline at end of file diff --git a/activerecord/lib/active_record/mixins/touch.rb b/activerecord/lib/active_record/mixins/touch.rb deleted file mode 100644 index baf217542a..0000000000 --- a/activerecord/lib/active_record/mixins/touch.rb +++ /dev/null @@ -1,34 +0,0 @@ -module ActiveRecord - module Mixins - # Including this mixins will record when objects of the class are created in a datetime column called "created_at" - # and when its updated in another datetime column called "updated_at". - # - # class Bill < ActiveRecord::Base - # include ActiveRecord::Mixins::Touch - # end - # - # bill = Bill.create("amount" => 100) - # bill.created_at # => Time.now at the moment of Bill.create - # bill.updated_at # => Time.now at the moment of Bill.create - # - # bill.update_attribute("amount", 150) - # bill.created_at # => Time.now at the moment of Bill.create - # bill.updated_at # => Time.now at the moment of bill.update_attribute - module Touch - def self.append_features(base) - super - - base.before_create :touch_on_create - base.before_update :touch_on_update - end - - def touch_on_create - self.updated_at = (self.created_at ||= Time.now) - end - - def touch_on_update - self.updated_at = Time.now - end - end - end -end
\ No newline at end of file diff --git a/activerecord/lib/active_record/mixins/tree.rb b/activerecord/lib/active_record/mixins/tree.rb index 582aa09b4b..141bbb1806 100644 --- a/activerecord/lib/active_record/mixins/tree.rb +++ b/activerecord/lib/active_record/mixins/tree.rb @@ -21,16 +21,20 @@ module ActiveRecord # root.children # => [child1] # root.children.first.children.first # => subchild1 module Tree - def self.append_features(base) super - - base.module_eval <<-associations - belongs_to :parent, :class_name => name, :foreign_key => "parent_id" - has_many :children, :class_name => name, :foreign_key => "parent_id", :order => "id", :dependent => true - associations - + base.extend(ClassMethods) end end + + module ClassMethods + def acts_as_tree(options = {}) + configuration = { :foreign_key => "parent_id", :order => nil } + configuration.update(options) if options.is_a?(Hash) + + belongs_to :parent, :class_name => name, :foreign_key => configuration[:foreign_key] + has_many :children, :class_name => name, :foreign_key => configuration[:foreign_key], :order => configuration[:order], :dependent => true + end + end end end
\ No newline at end of file |