Skip to content

fetch model resources in a simple a concise fashion

License

Notifications You must be signed in to change notification settings

net-engine/retrieve_resource

 
 

Repository files navigation

Tired of repeatedly writing get methods for your resources? Then you need…

Retrieve Resource!

Same yourself from carpal tunnel and other programming ailments when writing methods to fetch your resources.

What use to be this:

class PeopleController < ApplicationController
def get_person
person = Person.find(params[:id])
end
end

is now this:
class PeopleController < ApplicationController
retrieve_resource :person
end

Have multiple resources? Then declare multiple retrievals:

class CommentsController < ApplicationController
  retrieve_resource :person, :param => :person_id
  retrieve_resource :comment, :only => [:show, :edit, :update, :destroy]
end

How does it work?

Tell retrieve_resource the name of the resource you want to load and it will load it. Based on the name specified it will get the name of your model and perform a find operation with the parameter from the request. But this baby allows you to have full control, just look at the options.

Options

  • class_name – The name of the class if it cannot be inflected from the name passed.
  • param – The parameter passed to load the model. This defaults to {name}_id or {id} if the name is inflected of the controller name.
  • find_method – Default is find but you can use another method. The argument passed is that from params

Along with the options above you can specify find options like conditions, joins, includes and all those other goodies.

The following options allow for retrieving resources which are associated with another resource.

  • through – The name of the parent object or the object associated to the object you want to retrieve.
  • as – If the association name is different then the pluralization of the class_name above. For instance, you have :subscriber but the association is call :subscriptions then you would specify that.

You can also filter when a retrieval will be triggered. Even when a particular action occurs or when it doesn’t occur, just like like Filters

  • only – Specify which actions to retrieve on
  • except – Specify which actions should not be retrieved on

You can also pass in a block to evaluate whether retrieval should occur.

Example

class ManagerController < ApplicationController
  retrieve_resource :person, :class_name => 'Person', :param => :id, :find_method => :find_if_manager
end

Versatility

Sometimes you want to retrieve an object that is associated with another object and with this plugin you can with a breeze.

class Person < ActiveRecord::Base
  has_many :comments
end

class Comment < ActiveRecord::Base; end

class CommentsController < ApplicationController
  retrieve_resource :person
  retrieve_resource :comment, :through => :person
end

Easy peasy ain’t it! And you can still use all the other options like normal, change the find_method or conditions etc. And best of all you don’t have to worry about the order. How about that!

TODO

Improve associated retrieval

Copyright

Copyright © 2009 Marty Zalega, released under the MIT license

About

fetch model resources in a simple a concise fashion

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Ruby 100.0%