Permalink
Browse files

Added that Active Records will automatically record creation and/or u…

…pdate timestamps of database objects if fields of the names created_at/created_on or updated_at/updated_on are present. [Tobias Luetke] Added acts_as_tree that can decorates an existing class with a many to many relationship with itself. Added acts_as_list that can decorates an existing class with methods like move_higher/lower, move_to_top/bottom.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@176 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
dhh committed Dec 16, 2004
1 parent 8bc8227 commit 1f916a119ca5c61174cb58124ea989c1e8b00fd4
View
@@ -1,5 +1,18 @@
*CVS*
+* Added acts_as_list that can decorates an existing class with methods like move_higher/lower, move_to_top/bottom. Example:
+
+ class TodoItem < ActiveRecord::Base
+ acts_as_list :scope => :todo_list_id
+ belongs_to :todo_list
+ end
+
+* Added acts_as_tree that can decorates an existing class with a many to many relationship with itself. Perfect for categories in
+ categories and the likes.
+
+* Added that Active Records will automatically record creation and/or update timestamps of database objects if fields of the names
+ created_at/created_on or updated_at/updated_on are present. [Tobias Luetke]
+
* Added Base.default_error_messages as a hash of all the error messages used in the validates_*_of so they can be changed in one place [Tobias Luetke]
* Added automatic transaction block around AssociationCollection.<<, AssociationCollection.delete, and AssociationCollection.destroy_all
@@ -98,32 +111,6 @@
end
end
-* Added ActiveRecord::Mixins::Touch that will record creation and update times of objects [xal]. Example:
-
- 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
-
-* Added ActiveRecord::Mixins::List that can decorates an existing class with methods like move_higher/lower, move_to_top/bottom. Example:
-
- class TodoItem < ActiveRecord::Base
- include ActiveRecord::Mixins::List
- belongs_to :todo_list
-
- private
- def scope_condition
- "todo_list_id = #{todo_list_id}"
- end
- end
-
* Added the option for sanitizing find_by_sql and the offset parts in regular finds [Sam Stephenson]. Examples:
Project.find_all ["category = ?", category_name], "created ASC", ["? OFFSET ?", 15, 20]
@@ -244,6 +231,7 @@
* All associations will now attempt to require the classes that they associate to. Relieving the need for most explicit 'require' statements.
+
*1.1.0* (34)
* Added automatic fixture setup and instance variable availability. Fixtures can also be automatically
@@ -35,6 +35,9 @@
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'
ActiveRecord::Base.class_eval do
include ActiveRecord::Validations
@@ -43,11 +46,11 @@
include ActiveRecord::Aggregations
include ActiveRecord::Transactions
include ActiveRecord::Reflection
+ include ActiveRecord::Timestamp
+ include ActiveRecord::Mixins::Tree
+ include ActiveRecord::Mixins::List
end
-require 'active_record/mixins/list'
-require 'active_record/mixins/touch'
-
require 'active_record/connection_adapters/mysql_adapter'
require 'active_record/connection_adapters/postgresql_adapter'
require 'active_record/connection_adapters/sqlite_adapter'
@@ -28,141 +28,156 @@ module Mixins
# 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
@@ -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
Oops, something went wrong.

0 comments on commit 1f916a1

Please sign in to comment.