aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--activeresource/CHANGELOG2
-rw-r--r--activeresource/README5
-rw-r--r--activeresource/lib/active_resource/base.rb15
-rw-r--r--activeresource/test/base_test.rb15
4 files changed, 36 insertions, 1 deletions
diff --git a/activeresource/CHANGELOG b/activeresource/CHANGELOG
index 71d9067dc9..f62bce33a1 100644
--- a/activeresource/CHANGELOG
+++ b/activeresource/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Carry over the convenience of #create from ActiveRecord. Closes #7340. [Ryan Daigle]
+
* Increase ActiveResource::Base test coverage. Closes #7173, #7174 [Rich Collins]
* Interpret 422 Unprocessable Entity as ResourceInvalid. #7097 [dkubb]
diff --git a/activeresource/README b/activeresource/README
index 702ab7efed..696b5e4fed 100644
--- a/activeresource/README
+++ b/activeresource/README
@@ -36,6 +36,11 @@ lifecycle methods that operate against a persistent store.
Person.exists?(ryan.id) #=> true
ryan.exists? #=> true
+ # Resource creation can also use the convenience <tt>create</tt> method which
+ # will request a resource save after instantiation.
+ ryan = Person.create(:first => 'Ryan', :last => 'Daigle')
+ ryan.exists? #=> true
+
# Updating is done with 'save' as well
# PUT http://api.people.com:3000/people/1.xml
#
diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb
index f530923224..faf0f046d6 100644
--- a/activeresource/lib/active_resource/base.rb
+++ b/activeresource/lib/active_resource/base.rb
@@ -70,6 +70,21 @@ module ActiveResource
alias_method :set_primary_key, :primary_key= #:nodoc:
+ # Create a new resource instance and request to the remote service
+ # that it be saved. This is equivalent to the following simultaneous calls:
+ #
+ # ryan = Person.new(:first => 'ryan')
+ # ryan.save
+ #
+ # The newly created resource is returned. If a failure has occurred an
+ # exception will be raised (see save). If the resource is invalid and
+ # has not been saved then <tt>resource.valid?</tt> will return <tt>false</tt>,
+ # while <tt>resource.new?</tt> will still return <tt>true</tt>.
+ #
+ def create(attributes = {}, prefix_options = {})
+ returning(self.new(attributes, prefix_options)) { |res| res.save }
+ end
+
# Core method for finding resources. Used similarly to ActiveRecord's find method.
# Person.find(1) # => GET /people/1.xml
# StreetAddress.find(1, :person_id => 1) # => GET /people/1/street_addresses/1.xml
diff --git a/activeresource/test/base_test.rb b/activeresource/test/base_test.rb
index 040bd02b52..4d3bb7461e 100644
--- a/activeresource/test/base_test.rb
+++ b/activeresource/test/base_test.rb
@@ -196,7 +196,7 @@ class BaseTest < Test::Unit::TestCase
assert_raises(ActiveResource::ResourceNotFound) { StreetAddress.find(1) }
end
- def test_create
+ def test_save
rick = Person.new
assert_equal true, rick.save
assert_equal '5', rick.id
@@ -217,6 +217,19 @@ class BaseTest < Test::Unit::TestCase
assert_equal '5', matzs_house.id
end
+ def test_create
+ rick = Person.create(:name => 'Rick')
+ assert rick.valid?
+ assert !rick.new?
+ assert_equal '5', rick.id
+
+ # Test that save exceptions get bubbled up too
+ ActiveResource::HttpMock.respond_to do |mock|
+ mock.post "/people.xml", {}, nil, 409
+ end
+ assert_raises(ActiveResource::ResourceConflict) { Person.create(:name => 'Rick') }
+ end
+
def test_update
matz = Person.find(:first)
matz.name = "David"