Skip to content
This repository
Browse code

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
risk danger olson authored January 24, 2007
2  activeresource/CHANGELOG
... ...
@@ -1,5 +1,7 @@
1 1
 *SVN*
2 2
 
  3
+* Carry over the convenience of #create from ActiveRecord.  Closes #7340.  [Ryan Daigle]
  4
+
3 5
 * Increase ActiveResource::Base test coverage.  Closes #7173, #7174 [Rich Collins]
4 6
 
5 7
 * Interpret 422 Unprocessable Entity as ResourceInvalid.  #7097 [dkubb]
5  activeresource/README
@@ -36,6 +36,11 @@ lifecycle methods that operate against a persistent store.
36 36
    Person.exists?(ryan.id)  #=> true
37 37
    ryan.exists?  #=> true
38 38
 
  39
+   # Resource creation can also use the convenience <tt>create</tt> method which
  40
+   # will request a resource save after instantiation.
  41
+   ryan = Person.create(:first => 'Ryan', :last => 'Daigle')
  42
+   ryan.exists?  #=> true
  43
+
39 44
    # Updating is done with 'save' as well
40 45
    # PUT http://api.people.com:3000/people/1.xml
41 46
    #
15  activeresource/lib/active_resource/base.rb
@@ -70,6 +70,21 @@ def collection_path(options = {})
70 70
 
71 71
       alias_method :set_primary_key, :primary_key=  #:nodoc:
72 72
 
  73
+      # Create a new resource instance and request to the remote service
  74
+      # that it be saved.  This is equivalent to the following simultaneous calls:
  75
+      #
  76
+      #   ryan = Person.new(:first => 'ryan')
  77
+      #   ryan.save
  78
+      #
  79
+      # The newly created resource is returned.  If a failure has occurred an
  80
+      # exception will be raised (see save).  If the resource is invalid and
  81
+      # has not been saved then <tt>resource.valid?</tt> will return <tt>false</tt>,
  82
+      # while <tt>resource.new?</tt> will still return <tt>true</tt>.
  83
+      #      
  84
+      def create(attributes = {}, prefix_options = {})
  85
+        returning(self.new(attributes, prefix_options)) { |res| res.save }        
  86
+      end
  87
+
73 88
       # Core method for finding resources.  Used similarly to ActiveRecord's find method.
74 89
       #  Person.find(1) # => GET /people/1.xml
75 90
       #  StreetAddress.find(1, :person_id => 1) # => GET /people/1/street_addresses/1.xml
15  activeresource/test/base_test.rb
@@ -196,7 +196,7 @@ def test_find_by_id_not_found
196 196
     assert_raises(ActiveResource::ResourceNotFound) { StreetAddress.find(1) }
197 197
   end
198 198
 
199  
-  def test_create
  199
+  def test_save
200 200
     rick = Person.new
201 201
     assert_equal true, rick.save
202 202
     assert_equal '5', rick.id
@@ -217,6 +217,19 @@ def test_create_with_custom_prefix
217 217
     assert_equal '5', matzs_house.id
218 218
   end
219 219
 
  220
+  def test_create
  221
+    rick = Person.create(:name => 'Rick')
  222
+    assert rick.valid?
  223
+    assert !rick.new?
  224
+    assert_equal '5', rick.id
  225
+    
  226
+    # Test that save exceptions get bubbled up too
  227
+    ActiveResource::HttpMock.respond_to do |mock|
  228
+      mock.post   "/people.xml", {}, nil, 409
  229
+    end    
  230
+    assert_raises(ActiveResource::ResourceConflict) { Person.create(:name => 'Rick') }
  231
+  end
  232
+
220 233
   def test_update
221 234
     matz = Person.find(:first)
222 235
     matz.name = "David"

0 notes on commit f49e449

Please sign in to comment.
Something went wrong with that request. Please try again.