Skip to content

Commit

Permalink
Carry over the convenience of #create from ActiveRecord. Closes #7340.…
Browse files Browse the repository at this point in the history
… [Ryan Daigle]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6025 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
technoweenie committed Jan 24, 2007
1 parent 0eb8398 commit f49e449
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
2 changes: 2 additions & 0 deletions activeresource/CHANGELOG
Original file line number Diff line number Diff line change
@@ -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]
Expand Down
5 changes: 5 additions & 0 deletions activeresource/README
Original file line number Diff line number Diff line change
Expand Up @@ -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
#
Expand Down
15 changes: 15 additions & 0 deletions activeresource/lib/active_resource/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 14 additions & 1 deletion activeresource/test/base_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand Down

0 comments on commit f49e449

Please sign in to comment.