diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2004-12-16 02:49:18 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2004-12-16 02:49:18 +0000 |
commit | 1f916a119ca5c61174cb58124ea989c1e8b00fd4 (patch) | |
tree | a21bc3c8990c5fb6364ff04cc1b3bcd59a8ba27e /activerecord/lib/active_record/mixins/tree.rb | |
parent | 8bc82278dddfe23d84ac24ffe83f92c816028dd8 (diff) | |
download | rails-1f916a119ca5c61174cb58124ea989c1e8b00fd4.tar.gz rails-1f916a119ca5c61174cb58124ea989c1e8b00fd4.tar.bz2 rails-1f916a119ca5c61174cb58124ea989c1e8b00fd4.zip |
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 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
Diffstat (limited to 'activerecord/lib/active_record/mixins/tree.rb')
-rw-r--r-- | activerecord/lib/active_record/mixins/tree.rb | 18 |
1 files changed, 11 insertions, 7 deletions
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 |