[Rails] ActiveResource (was: [ANN] 1.1.3 available.)

Jean-François jf.web3 at gmail.com
Wed Jun 28 00:10:27 GMT 2006


2006/6/28, Paul Barry <mail at paulbarry.com>:
> Where can I find info about the "CRUD/resource-based features" that will be
> in 1.2.0?

In blogs. Especially about Railsconf and DHH's keynotes.

In trunk : http://dev.rubyonrails.org/browser/trunk/activeresource

In DHH slides :

Transcription of some slides from DHH's talk at RubyKaigi 2006
(I suppose it must be very similar to Railsconf ones)

---- slide A --------------------------------------------

--------------------------------------
| find   | create | update | destroy |
|-------------------------------------
| SELECT | INSERT | UPDATE | DELETE  |
--------------------------------------

---- slide B --------------------------------------------

--------------------------------------
|  GET   |  POST  |   PUT  |  DELETE |
|-------------------------------------
| find   | create | update | destroy |
|-------------------------------------
| SELECT | INSERT | UPDATE | DELETE  |
--------------------------------------

---- slide C --------------------------------------------

 POST /people/create
 GET /people/show/1
 POST /people/update/1
 POST /people/destroy/1

---- slide D --------------------------------------------

class PeopleController < ActionController::Base
  def index
    @people = Person.find :all

    respond_to do |format|
      format.html # renders index.rhtml
      format.js   # renders index.rjs
      format.xml  { render :xml => @people.to_xml }
      format.icl  { render_calendar(@people) }
      format.atom do
        render :action => "atom", :content_type => Mime::ATOM
      end
    end
  end
end

GET /people
=> returns HTML

Accept : text/javascript
GET /people
=> returns RJS

GET /people.xml
=> returns XML

Accept: text/html
GET /people.xml
=> returns XML

---- slide E --------------------------------------------

class PeopleController < ActionController::Base
  def create
    @people = Person.create(params[:person])

    respond_to do |format|
      format.html { redirect_to :action => 'index' }
      format.js   # renders create.rjs
      format.xml  do
        headers["Location"] = person_url(@person)
        render :nothing => true
      end
    end
  end
end

POST /people
person[name]=David
=> returns a redirect to index

Content-Type: application/xml
POST /people
<person>
<name>David</name>
</person>

POST /people.xml
person[name]=David
=> returns location

Accept: text/javascript
POST /people
person[name]=David
=> returns RJS

---- slide F --------------------------------------------

Person = ActiveResource::Struct.new do |p|
  p.uri "http://www.example.com/people"
  p.credentials "name" => "dhh", "password" => "secret"
end

# GET http://www.example.com/people/1
# => <person><name>Matz</name></person>
matz = Person.find(1)

matz.name
# => matz

---- slide G --------------------------------------------

Person = ActiveResource::Struct.new do
  uri 'http://www.example.com/people'
  credentials :name => 'dhh', :password => 'secret'
end

david = Person.new(:name => 'David')

david.save
# POST http://www.example.com/people
# <person><name>David</name></person>
# => Location: http://www.example.com/people/2 (201 Created)

david.id
# => 2

david.name = "David Heinemeier Hansson"

david.save
# PUT http://www.example.com/people
# <person><name>David Heinemeier Hansson</name></person>

# => (200 OK)

---- slide H --------------------------------------------

class SunriseResource < ActiveResource::Base
  uri 'http://www.example.com'
  credentials :name => 'dhh', :password => 'secret'
end

class  Person < SunriseResource
  def first_name
    name.split(' ').first
  end
end

david = Person.find(5)

david.name
# => "David Heinemeier Hansson"

david.first_name
# => "David"




-- 
À la renverse.


More information about the Rails mailing list