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/test | |
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/test')
-rw-r--r-- | activerecord/test/fixtures/mixin.rb | 16 | ||||
-rw-r--r-- | activerecord/test/mixin_test.rb | 111 |
2 files changed, 71 insertions, 56 deletions
diff --git a/activerecord/test/fixtures/mixin.rb b/activerecord/test/fixtures/mixin.rb index 589c1a8c23..8cae604140 100644 --- a/activerecord/test/fixtures/mixin.rb +++ b/activerecord/test/fixtures/mixin.rb @@ -1,21 +1,13 @@ class Mixin < ActiveRecord::Base - include ActiveRecord::Mixins::Touch - include ActiveRecord::Mixins::Tree + acts_as_tree :foreign_key => "parent_id", :order => "id" + end class ListMixin < ActiveRecord::Base - include ActiveRecord::Mixins::List - + acts_as_list :column => "pos", :scope => :parent_id + def self.table_name "mixins" end - def scope_condition - "parent_id = #{self.parent_id}" - end - - def position_column - "pos" - end - end
\ No newline at end of file diff --git a/activerecord/test/mixin_test.rb b/activerecord/test/mixin_test.rb index cc56bc7cec..31e71f5d6c 100644 --- a/activerecord/test/mixin_test.rb +++ b/activerecord/test/mixin_test.rb @@ -4,6 +4,73 @@ require 'active_record/mixins/list' require 'active_record/mixins/touch' require 'fixtures/mixin' + +class ListTest < Test::Unit::TestCase + fixtures :mixins + + def test_injection + l = ListMixin.new("parent_id"=>1) + assert_equal "parent_id = 1", l.scope_condition + assert_equal "pos", l.position_column + + end + + def test_reordering + + assert_equal [ListMixin.find(2), ListMixin.find(3)], ListMixin.find_all("parent_id=1", "pos") + + ListMixin.find(2).move_lower + + assert_equal [ListMixin.find(3), ListMixin.find(2)], ListMixin.find_all("parent_id=1", "pos") + + ListMixin.find(2).move_higher + + assert_equal [ListMixin.find(2), ListMixin.find(3)], ListMixin.find_all("parent_id=1", "pos") + + end + + def test_insert + new = ListMixin.create("parent_id"=>5) + assert_equal 1, new.pos + assert new.first? + assert new.last? + + new = ListMixin.create("parent_id"=>5) + assert_equal 2, new.pos + assert !new.first? + assert new.last? + + new = ListMixin.create("parent_id"=>5) + assert_equal 3, new.pos + assert !new.first? + assert new.last? + + new = ListMixin.create("parent_id"=>10) + assert_equal 1, new.pos + assert new.first? + assert new.last? + end + + + def test_delete_middle + first = ListMixin.create("parent_id"=>5) + assert_equal 1, first.pos + + second = ListMixin.create("parent_id"=>5) + assert_equal 2, second.pos + + third = ListMixin.create("parent_id"=>5) + assert_equal 3, third.pos + + second.destroy + + assert_equal 1, @mixins["first"].find.pos + assert_equal 2, @mixins["third"].find.pos + + end + +end + class TreeTest < Test::Unit::TestCase fixtures :mixins @@ -54,48 +121,4 @@ class TouchTest < Test::Unit::TestCase assert_not_nil @obj.updated_at assert_not_nil @obj.created_at end - - end - - -class ListTest < Test::Unit::TestCase - fixtures :mixins - - def test_reordering - - assert_equal [ListMixin.find(2), ListMixin.find(3)], ListMixin.find_all("parent_id=1", "pos") - - ListMixin.find(2).move_lower - - assert_equal [ListMixin.find(3), ListMixin.find(2)], ListMixin.find_all("parent_id=1", "pos") - - ListMixin.find(2).move_higher - - assert_equal [ListMixin.find(2), ListMixin.find(3)], ListMixin.find_all("parent_id=1", "pos") - - end - - def test_insert - new = ListMixin.create("parent_id"=>3) - assert_equal 1, new.pos - assert new.first? - assert new.last? - - new = ListMixin.create("parent_id"=>3) - assert_equal 2, new.pos - assert !new.first? - assert new.last? - - new = ListMixin.create("parent_id"=>3) - assert_equal 3, new.pos - assert !new.first? - assert new.last? - - new = ListMixin.create("parent_id"=>2) - assert_equal 1, new.pos - assert new.first? - assert new.last? - - end -end
\ No newline at end of file |