Acts As Resource

Acts As Resource

ActsAsResource combines ActiveRecord and ActiveResource features in one class.

Example

   1  class Carrot
   2    acts_as_resource
   3    self.site = 'http://localhost:3000'
   4    
   5    belongs_to :bunny
   6  
   7    validates_presence_of :color
   8    validates_uniqueness_of :color
   9    validates_length_of :color, :within => 2..23,
  10                        :if => lambda { |c| c.color && !c.color.empty? }
  11    validates_format_of :color,
  12                        :with => /[\w\s]+$/,
  13                        :if => lambda { |c| c.color && !c.color.empty? }
  14  
  15    before_create :please_call_me_before_create
  16    def self.validate
  17      logger.debug("VALIDATE #{color}")
  18    end
  19    
  20    def please_call_me_before_create
  21      logger.debug("Ohhh, so you called me..")
  22    end  
  23  end

Usage

For convention each operation is local unless specified with :remote flag.

   1  carrot = Carrot.find(23)                   # => local request
   2  carrot = Carrot.find(23, :remote => false) # => local request
   3  carrot = Carrot.find(23, :remote => true)  # => remote request
   4  
   5  Carrot.create(:color => 'orange')                  # => create local
   6  Carrot.create(:color => 'orange', :remote => true) # => create remote

Prerequisites

For 2.0 railists: no problem guys!
For old 1.2.x people you should live in the edge, cause ActiveResource is needed.

Install

Rails 2.1.x

   1  $ ./script/plugin install git://github.com/jodosha/acts-as-resource.git

Rails <= 2.0.x

   1  $ ./script/plugin install http://dev.23labs.net/svn/rails/plugins/acts_as_resource

Uninstall

   1  $ ./script/plugin remove acts_as_resource

Common Issues

  • Actually ActsAsResource doesn't support Single Table Inheritance.
  • Sometimes, when class relations are used, ActiveRecord generates a warning like:
    warning: toplevel constant Bunny referenced by Carrot::Bunny

Contribute

  • Check out the code and test it: $ git clone git://github.com/jodosha/acts-as-resource.git $ rake resource
  • Create a ticket on: http://dev.23labs.net/rails/trac.cgi
  • Create a patch and add as attachement to the ticket.
  • The SVN repo has been deprecated in favor of the Git one.

Repository

git://github.com/jodosha/acts-as-resource.git

Vote

If you find it useful feel free to add to your favs on agilewebdevelopment.com.

Copyright

Copyright © 2007 Luca Guidi - 23 Labs, released under the MIT license

advertising