From 648b8fda54e5de742e4ffbb4bc8e6edf0b68a111 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 17 Dec 2004 21:36:13 +0000 Subject: [PATCH] Added Base.destroy and Base.delete to remove records without holding a reference to them first. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@206 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/CHANGELOG | 5 +++++ activerecord/lib/active_record/base.rb | 10 +++++++++ activerecord/test/base_test.rb | 30 ++++++++++++++++++++------ 3 files changed, 38 insertions(+), 7 deletions(-) diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 82f9d88992..7c032d1a9d 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,3 +1,8 @@ +*SVN* + +* Added Base.destroy and Base.delete to remove records without holding a reference to them first. + + *1.2.0* * Added Base.validates_inclusion_of that validates whether the value of the specified attribute is available in a particular enumerable diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index ca586aacad..2bcea9dd5d 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -333,6 +333,16 @@ module ActiveRecord #:nodoc: object end + # Deletes the record with the given +id+ without instantiating an object first. + def delete(id) + delete_all([ "#{primary_key} = ?", id ]) + end + + # Destroys the record with the given +id+ by instantiating the object and calling #destroy (all the callbacks are the triggered). + def destroy(id) + find(id).destroy + end + # Updates all records with the SET-part of an SQL update statement in +updates+. A subset of the records can be selected # by specifying +conditions+. Example: # Billing.update_all "category = 'authorized', approved = 1", "author = 'David'" diff --git a/activerecord/test/base_test.rb b/activerecord/test/base_test.rb index c362fd7139..b80f4ff6ef 100755 --- a/activerecord/test/base_test.rb +++ b/activerecord/test/base_test.rb @@ -26,9 +26,7 @@ end class Booleantest < ActiveRecord::Base; end class BasicsTest < Test::Unit::TestCase - def setup - @topic_fixtures, @companies = create_fixtures "topics", "companies" - end + fixtures :topics, :companies def test_set_attributes topic = Topic.find(1) @@ -36,7 +34,7 @@ class BasicsTest < Test::Unit::TestCase topic.save assert_equal("Budget", topic.title) assert_equal("Jason", topic.author_name) - assert_equal(@topic_fixtures["first"]["author_email_address"], Topic.find(1).author_email_address) + assert_equal(@topics["first"]["author_email_address"], Topic.find(1).author_email_address) end def test_integers_as_nil @@ -187,14 +185,14 @@ class BasicsTest < Test::Unit::TestCase def test_load topics = Topic.find_all nil, "id" assert_equal(2, topics.size) - assert_equal(@topic_fixtures["first"]["title"], topics.first.title) + assert_equal(@topics["first"]["title"], topics.first.title) end def test_load_with_condition topics = Topic.find_all "author_name = 'Mary'" assert_equal(1, topics.size) - assert_equal(@topic_fixtures["second"]["title"], topics.first.title) + assert_equal(@topics["second"]["title"], topics.first.title) end def test_table_name_guesses @@ -556,4 +554,22 @@ class BasicsTest < Test::Unit::TestCase topic = Topic.create('content' => content) assert_equal content, Topic.find(topic.id).content end -end + + def test_class_level_destroy + should_be_destroyed_reply = Reply.create("title" => "hello", "content" => "world") + @first.replies << should_be_destroyed_reply + + Topic.destroy(1) + assert_raises(ActiveRecord::RecordNotFound) { Topic.find(1) } + assert_raises(ActiveRecord::RecordNotFound) { Reply.find(should_be_destroyed_reply.id) } + end + + def test_class_level_delete + should_be_destroyed_reply = Reply.create("title" => "hello", "content" => "world") + @first.replies << should_be_destroyed_reply + + Topic.delete(1) + assert_raises(ActiveRecord::RecordNotFound) { Topic.find(1) } + assert_nothing_raised { Reply.find(should_be_destroyed_reply.id) } + end +end \ No newline at end of file