Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Carry over the convenience of #create from ActiveRecord. Closes #7340.…

… [Ryan Daigle]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6025 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit f49e449ed5d140b63f30ac046826f81c04e8333d 1 parent 0eb8398
@technoweenie technoweenie authored
View
2  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]
View
5 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
#
View
15 activeresource/lib/active_resource/base.rb
@@ -70,6 +70,21 @@ def collection_path(options = {})
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
View
15 activeresource/test/base_test.rb
@@ -196,7 +196,7 @@ def test_find_by_id_not_found
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 @@ def test_create_with_custom_prefix
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"
Please sign in to comment.
Something went wrong with that request. Please try again.