From 6860db61f516b813af624d38b47fef0bf983539c Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Thu, 16 Dec 2004 16:23:59 +0000 Subject: Renamed Mixins to Acts to resemble the change from include ActiveRecord::Mixins::List to acts_as_list and renamed @@default_error_messagess to @@default_error_messages git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@190 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/lib/active_record.rb | 8 +- activerecord/lib/active_record/acts/list.rb | 185 +++++++++++++++++++++ .../lib/active_record/acts/mixins/touch.rb | 34 ++++ activerecord/lib/active_record/acts/touch.rb | 34 ++++ activerecord/lib/active_record/acts/tree.rb | 40 +++++ activerecord/lib/active_record/mixins/list.rb | 185 --------------------- activerecord/lib/active_record/mixins/tree.rb | 40 ----- activerecord/lib/active_record/validations.rb | 24 +-- 8 files changed, 309 insertions(+), 241 deletions(-) create mode 100644 activerecord/lib/active_record/acts/list.rb create mode 100644 activerecord/lib/active_record/acts/mixins/touch.rb create mode 100644 activerecord/lib/active_record/acts/touch.rb create mode 100644 activerecord/lib/active_record/acts/tree.rb delete mode 100644 activerecord/lib/active_record/mixins/list.rb delete mode 100644 activerecord/lib/active_record/mixins/tree.rb (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record.rb b/activerecord/lib/active_record.rb index 229983503e..36c367b78f 100755 --- a/activerecord/lib/active_record.rb +++ b/activerecord/lib/active_record.rb @@ -36,8 +36,8 @@ require 'active_record/aggregations' require 'active_record/transactions' require 'active_record/reflection' require 'active_record/timestamp' -require 'active_record/mixins/list' -require 'active_record/mixins/tree' +require 'active_record/acts/list' +require 'active_record/acts/tree' ActiveRecord::Base.class_eval do include ActiveRecord::Validations @@ -47,8 +47,8 @@ ActiveRecord::Base.class_eval do include ActiveRecord::Transactions include ActiveRecord::Reflection include ActiveRecord::Timestamp - include ActiveRecord::Mixins::Tree - include ActiveRecord::Mixins::List + include ActiveRecord::Acts::Tree + include ActiveRecord::Acts::List end require 'active_record/connection_adapters/mysql_adapter' diff --git a/activerecord/lib/active_record/acts/list.rb b/activerecord/lib/active_record/acts/list.rb new file mode 100644 index 0000000000..5d16f9a0c9 --- /dev/null +++ b/activerecord/lib/active_record/acts/list.rb @@ -0,0 +1,185 @@ +module ActiveRecord + # Mixins are a way of decorating existing Active Record models with additional behavior. If you for example + # want to keep a number of Documents in order, you can include Mixins::List, and all of the sudden be able to + # call document.move_to_bottom. + module Acts + # This mixin provides the capabilities for sorting and reordering a number of objects in list. + # The class that has this mixin included needs to have a "position" column defined as an integer on + # the mapped database table. Further more, you need to implement the scope_condition if you want + # to separate one list from another. + # + # Todo list example: + # + # class TodoList < ActiveRecord::Base + # has_many :todo_items, :order => "position" + # end + # + # class TodoItem < ActiveRecord::Base + # include ActiveRecord::Mixins::List + # belongs_to :todo_list + # + # private + # def scope_condition + # "todo_list_id = #{todo_list_id}" + # end + # end + # + # todo_list.first.move_to_bottom + # todo_list.last.move_higher + module List + def self.append_features(base) + super + base.extend(ClassMethods) + end + + 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 + + def position_column + '#{configuration[:column]}' + end + + def scope_condition + "#{configuration[:scope].is_a?(Symbol) ? configuration[:scope].to_s + " = \#{" + configuration[:scope].to_s + "}" : configuration[:scope]}" + end + + before_destroy :remove_from_list + after_create :add_to_list_bottom + EOV + end + + module InstanceMethods + def move_lower + return unless lower_item + + self.class.transaction do + lower_item.decrement_position + increment_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_top + self.class.transaction do + increment_positions_on_higher_items + assume_top_position + end + end + + + # Entering or existing the list + + def add_to_list_top + increment_positions_on_all_items + end + + def add_to_list_bottom + assume_bottom_position + end + + def remove_from_list + decrement_positions_on_lower_items + end + + + # Changing the position + + 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 + + + # Querying the position + + def first? + self.send(position_column) == 1 + 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 + + 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 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 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 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_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/acts/mixins/touch.rb b/activerecord/lib/active_record/acts/mixins/touch.rb new file mode 100644 index 0000000000..baf217542a --- /dev/null +++ b/activerecord/lib/active_record/acts/mixins/touch.rb @@ -0,0 +1,34 @@ +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/acts/touch.rb b/activerecord/lib/active_record/acts/touch.rb new file mode 100644 index 0000000000..baf217542a --- /dev/null +++ b/activerecord/lib/active_record/acts/touch.rb @@ -0,0 +1,34 @@ +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/acts/tree.rb b/activerecord/lib/active_record/acts/tree.rb new file mode 100644 index 0000000000..f05bbe9a78 --- /dev/null +++ b/activerecord/lib/active_record/acts/tree.rb @@ -0,0 +1,40 @@ +module ActiveRecord + module Acts + # Including this mixin if you want to model a tree structure by providing a parent association and an children + # association. This mixin assumes that you have a column called parent_id + # + # class Category < ActiveRecord::Base + # include ActiveRecord::Mixins::Tree + # end + # + # Example : + # root + # \_ child1 + # \_ sub-child1 + # + # root = Category.create("name" => "root") + # child1 = root.children.create("name" => "child1") + # subchild1 = child1.children.create("name" => "subchild1") + # + # root.parent # => nil + # child1.parent # => root + # root.children # => [child1] + # root.children.first.children.first # => subchild1 + module Tree + def self.append_features(base) + super + 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 diff --git a/activerecord/lib/active_record/mixins/list.rb b/activerecord/lib/active_record/mixins/list.rb deleted file mode 100644 index f092347478..0000000000 --- a/activerecord/lib/active_record/mixins/list.rb +++ /dev/null @@ -1,185 +0,0 @@ -module ActiveRecord - # Mixins are a way of decorating existing Active Record models with additional behavior. If you for example - # want to keep a number of Documents in order, you can include Mixins::List, and all of the sudden be able to - # call document.move_to_bottom. - module Mixins - # This mixin provides the capabilities for sorting and reordering a number of objects in list. - # The class that has this mixin included needs to have a "position" column defined as an integer on - # the mapped database table. Further more, you need to implement the scope_condition if you want - # to separate one list from another. - # - # Todo list example: - # - # class TodoList < ActiveRecord::Base - # has_many :todo_items, :order => "position" - # end - # - # class TodoItem < ActiveRecord::Base - # include ActiveRecord::Mixins::List - # belongs_to :todo_list - # - # private - # def scope_condition - # "todo_list_id = #{todo_list_id}" - # end - # end - # - # todo_list.first.move_to_bottom - # todo_list.last.move_higher - module List - def self.append_features(base) - super - base.extend(ClassMethods) - end - - 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 - - def position_column - '#{configuration[:column]}' - end - - def scope_condition - "#{configuration[:scope].is_a?(Symbol) ? configuration[:scope].to_s + " = \#{" + configuration[:scope].to_s + "}" : configuration[:scope]}" - end - - before_destroy :remove_from_list - after_create :add_to_list_bottom - EOV - end - - module InstanceMethods - def move_lower - return unless lower_item - - self.class.transaction do - lower_item.decrement_position - increment_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_top - self.class.transaction do - increment_positions_on_higher_items - assume_top_position - end - end - - - # Entering or existing the list - - def add_to_list_top - increment_positions_on_all_items - end - - def add_to_list_bottom - assume_bottom_position - end - - def remove_from_list - decrement_positions_on_lower_items - end - - - # Changing the position - - 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 - - - # Querying the position - - def first? - self.send(position_column) == 1 - 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 - - 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 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 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 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_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/tree.rb b/activerecord/lib/active_record/mixins/tree.rb deleted file mode 100644 index 141bbb1806..0000000000 --- a/activerecord/lib/active_record/mixins/tree.rb +++ /dev/null @@ -1,40 +0,0 @@ -module ActiveRecord - module Mixins - # Including this mixin if you want to model a tree structure by providing a parent association and an children - # association. This mixin assumes that you have a column called parent_id - # - # class Category < ActiveRecord::Base - # include ActiveRecord::Mixins::Tree - # end - # - # Example : - # root - # \_ child1 - # \_ sub-child1 - # - # root = Category.create("name" => "root") - # child1 = root.children.create("name" => "child1") - # subchild1 = child1.children.create("name" => "subchild1") - # - # root.parent # => nil - # child1.parent # => root - # root.children # => [child1] - # root.children.first.children.first # => subchild1 - module Tree - def self.append_features(base) - super - 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 diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb index 544754eeb9..95e1cc4b1e 100755 --- a/activerecord/lib/active_record/validations.rb +++ b/activerecord/lib/active_record/validations.rb @@ -76,7 +76,7 @@ module ActiveRecord # ::message: A custom error message (default is: "doesn't match confirmation") # ::on: Specifies when this validation is active (default is :save, other options :create, :update) def validates_confirmation_of(*attr_names) - configuration = { :message => ActiveRecord::Errors.default_error_messagess[:confirmation], :on => :save } + configuration = { :message => ActiveRecord::Errors.default_error_messages[:confirmation], :on => :save } configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash) for attr_name in attr_names @@ -100,7 +100,7 @@ module ActiveRecord # # NOTE: The agreement is considered valid if it's set to the string "1". This makes it easy to relate it to an HTML checkbox. def validates_acceptance_of(*attr_names) - configuration = { :message => ActiveRecord::Errors.default_error_messagess[:accepted], :on => :save } + configuration = { :message => ActiveRecord::Errors.default_error_messages[:accepted], :on => :save } configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash) for attr_name in attr_names @@ -115,7 +115,7 @@ module ActiveRecord # ::message: A custom error message (default is: "has already been taken") # ::on: Specifies when this validation is active (default is :save, other options :create, :update) def validates_presence_of(*attr_names) - configuration = { :message => ActiveRecord::Errors.default_error_messagess[:empty], :on => :save } + configuration = { :message => ActiveRecord::Errors.default_error_messages[:empty], :on => :save } configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash) for attr_name in attr_names @@ -136,7 +136,7 @@ module ActiveRecord # ::too_short: The error message if the attributes go under the boundary (default is: "is too short (min is %d characters)") # ::on: Specifies when this validation is active (default is :save, other options :create, :update) def validates_boundaries_of(*attr_names) - configuration = { :within => 5..20, :too_long => ActiveRecord::Errors.default_error_messagess[:too_long], :too_short => ActiveRecord::Errors.default_error_messagess[:too_short], :on => :save } + configuration = { :within => 5..20, :too_long => ActiveRecord::Errors.default_error_messages[:too_long], :too_short => ActiveRecord::Errors.default_error_messages[:too_short], :on => :save } configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash) for attr_name in attr_names @@ -157,7 +157,7 @@ module ActiveRecord # Configuration options: # ::message: Specifies a custom error message (default is: "has already been taken") def validates_uniqueness_of(*attr_names) - configuration = { :message => ActiveRecord::Errors.default_error_messagess[:taken] } + configuration = { :message => ActiveRecord::Errors.default_error_messages[:taken] } configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash) for attr_name in attr_names @@ -179,7 +179,7 @@ module ActiveRecord # ::with: The regular expression used to validate the format with (note: must be supplied!) # ::on: Specifies when this validation is active (default is :save, other options :create, :update) def validates_format_of(*attr_names) - configuration = { :message => ActiveRecord::Errors.default_error_messagess[:invalid], :on => :save, :with => nil } + configuration = { :message => ActiveRecord::Errors.default_error_messages[:invalid], :on => :save, :with => nil } configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash) raise(ArgumentError, "A regular expression must be supplied as the :with option of the configuration hash") unless configuration[:with].is_a?(Regexp) @@ -200,7 +200,7 @@ module ActiveRecord # ::in: An enumerable object of available items # ::message: Specifieds a customer error message (default is: "is not included in the list") def validates_inclusion_of(*attr_names) - configuration = { :message => ActiveRecord::Errors.default_error_messagess[:inclusion], :on => :save } + configuration = { :message => ActiveRecord::Errors.default_error_messages[:inclusion], :on => :save } configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash) enum = configuration[:in] @@ -311,7 +311,7 @@ module ActiveRecord @base, @errors = base, {} end - @@default_error_messagess = { + @@default_error_messages = { :inclusion => "is not included in the list", :invalid => "is invalid", :confirmation => "doesn't match confirmation", @@ -321,7 +321,7 @@ module ActiveRecord :too_short => "is too short (min is %d characters)", :taken => "has already been taken", } - cattr_accessor :default_error_messagess + cattr_accessor :default_error_messages # Adds an error to the base object instead of any particular attribute. This is used @@ -336,19 +336,19 @@ module ActiveRecord # for the same attribute and ensure that this error object returns false when asked if +empty?+. More than one # error can be added to the same +attribute+ in which case an array will be returned on a call to on(attribute). # If no +msg+ is supplied, "invalid" is assumed. - def add(attribute, msg = @@default_error_messagess[:invalid]) + def add(attribute, msg = @@default_error_messages[:invalid]) @errors[attribute.to_s] = [] if @errors[attribute.to_s].nil? @errors[attribute.to_s] << msg end # Will add an error message to each of the attributes in +attributes+ that is empty (defined by attribute_present?). - def add_on_empty(attributes, msg = @@default_error_messagess[:empty]) + def add_on_empty(attributes, msg = @@default_error_messages[:empty]) [attributes].flatten.each { |attr| add(attr, msg) unless @base.attribute_present?(attr.to_s) } end # Will add an error message to each of the attributes in +attributes+ that has a length outside of the passed boundary +range+. # If the length is above the boundary, the too_long_msg message will be used. If below, the too_short_msg. - def add_on_boundary_breaking(attributes, range, too_long_msg = @@default_error_messagess[:too_long], too_short_msg = @@default_error_messagess[:too_short]) + def add_on_boundary_breaking(attributes, range, too_long_msg = @@default_error_messages[:too_long], too_short_msg = @@default_error_messages[:too_short]) for attr in [attributes].flatten add(attr, too_short_msg % range.begin) if @base.attribute_present?(attr.to_s) && @base.send(attr.to_s).length < range.begin add(attr, too_long_msg % range.end) if @base.attribute_present?(attr.to_s) && @base.send(attr.to_s).length > range.end -- cgit v1.2.3