ActiveResource
From WhyNotWiki
ActiveResource edit (Category edit)
Rails / REST edit (Category edit)
Contents |
[edit]
http://dev.rubyonrails.org/svn/rails/trunk/activeresource/README

[edit]
Ryan Daigle (2007-03-14). REST & ActiveResource (http://ryandaigle.com/assets/2007/3/14/REST_ARes.pdf).
[edit] Nested Resources
- Nested != Inherited
- has_many relationships often represented w/ nesting
- Nesting specified via URI: http://addressbook/users/1/contacts.xml
Containing model:
class User < ActiveResource::Base self.site = "http://addressbook" endNested model:
class Contact < ActiveResource::Base self.site = "http://addressbook/users/:user_id" # This is the URI *prefix* used for all requests through this ARes model # An actual request URI might look something like this: # "http://addressbook/users/2/contacts.xml" end[edit] Nested Model Initialization
contact = Contact.new(:name => 'Ryan', :last_name => 'Daigle', {:user_id => 2} # This hash is used to populate nesting entities in URI (:user_id in "http://addressbook/users/:user_id") )...
[edit] Token-Based Authentication
class User < ActiveResource::Base self.site = "http://addressbook/2kal893jl" end[edit] ARes Caveats
- No ActiveRecord-like DSL [I assume they mean shortcuts like find_by_name and stuff? Or what exactly is ARes missing in comparison to AR?]
[edit]
http://www.ryandaigle.com/articles/2006/06/30/whats-new-in-edge-rails-activeresource-is-here :
CRUD is great because it’s consistent, simple, expressive and foundational. Every application should be built solely on CRUD operations – and Rails is going to help you construct such applications...
If you haven’t noticed, it appears that the next version of Rails will whole heartedly embrace the RESTful model of application development...
ActiveResource provides a large piece of the REST puzzle by basically implementing the client side of a RESTful system – the parts of a decoupled system that consume RESTful services. At its essence, ActiveResource provides a way to utilize model objects as REST-based client proxies to remote services.
Let’s look at an example of a typical find call on a Person model object:
Person.find(1).name #=> "Ryan"Doesn’t appear to be anything out of the ordinary going on here – except that under the covers it’s not a database SELECT that’s occuring. Instead, if the Person model is an Active Resource, this find call is actually sending an HTTP GET request across the wire to a RESTful service:
GET http://api.myremote.com/people/1 <= <person><name>Ryan</name></person>
[edit] How to get it installed/working
This apparently doesn't export activeresource for you:
> rake rails:freeze:edge
(I'm not sure why not.) See?:
> ffind activeresource; ffind active_resource (nothing)
In fact, this will throw an error until you make activeresource available:
> ./script/about
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require': no such file to load -- active_resource (MissingSourceFile)
from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require'
from /home/tyler/code/examples/restful_resource/vendor/rails/activerecord/lib/../../activesupport/lib/active_support/dependencies.rb:495:in `require'
from /home/tyler/code/examples/restful_resource/vendor/rails/activerecord/lib/../../activesupport/lib/active_support/dependencies.rb:342:in `new_constants_in'
from /home/tyler/code/examples/restful_resource/vendor/rails/activerecord/lib/../../activesupport/lib/active_support/dependencies.rb:495:in `require'
from ./script/../config/../vendor/rails/railties/lib/initializer.rb:160:in `require_frameworks'
from ./script/../config/../vendor/rails/railties/lib/initializer.rb:160:in `each'
from ./script/../config/../vendor/rails/railties/lib/initializer.rb:160:in `require_frameworks'
from ./script/../config/../vendor/rails/railties/lib/initializer.rb:88:in `process'
from ./script/../config/../vendor/rails/railties/lib/initializer.rb:49:in `send'
from ./script/../config/../vendor/rails/railties/lib/initializer.rb:49:in `run'
from /home/tyler/code/examples/restful_resource/config/environment.rb:13
from /home/tyler/code/examples/restful_resource/vendor/rails/railties/lib/commands/about.rb:1:in `require'
from /home/tyler/code/examples/restful_resource/vendor/rails/railties/lib/commands/about.rb:1
from ./script/about:3:in `require'
from ./script/about:3
Wow! So Rails (edge version 7200) expects to find activeresource but yet Rails doesn't provide activeresource out of the box?
> svn export http://dev.rubyonrails.org/svn/rails/trunk/activeresource vendor/rails/activeresource > ffind activeresource; ffind active_resource ./vendor/rails/activeresource ./vendor/rails/activeresource/lib/active_resource.rb ./vendor/rails/activeresource/lib/active_resource
[edit] Problems
[edit] Dates are transmitted as UTC times (not local times) and the time part of the date is lost/truncated
Here was my schema (sqlite3):
CREATE TABLE tasks ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255) DEFAULT NULL, "description" text DEFAULT NULL, "due_date" date DEFAULT NULL);
restful_resource_consumer > ./script/console
...
irb -> task.due_date = Time.now
=> Fri Jul 20 17:59:49 -0700 2007
irb -> task.save
=> true
irb -> task
=> #<Task:0xb7ce33a0 @prefix_options={}, @attributes={"name"=>"Mow the lawn", "id"=>2, "description"=>nil, "due_date"=>Sat, 21 Jul 2007}>
irb -> task = Task.find(2)
=> #<Task:0xb7c04984 @prefix_options={}, @attributes={"name"=>"Mow the lawn", "id"=>2, "description"=>nil, "due_date"=>Sat, 21 Jul 2007}>
I would say, "maybe it's a problem with date types in sqlite3 with ActiveRecord", except that I can't reproduce the problem when I access the model via ActiveRecord directly as opposed to through ActiveResource:
restful_resource > ./script/console
irb -> Task.create(:due_date => Time.now)
=> #<Task id: 3, name: nil, description: nil, due_date: "2007-07-20 18:24:34">
irb -> Task.find(3)
=> #<Task id: 3, name: nil, description: nil, due_date: "2007-07-20">
Obviously I need to do a bit more digging to find out what's going on... I'd like to look at the actual XML data that gets posted by ActiveResource...
