Skip to content

Minimalistic abstract resource controller for rails

Notifications You must be signed in to change notification settings

jbutkowski/tiny_resource

 
 

Repository files navigation

tiny_resource

Minimalistic abstract resource controllers to use as a starting point for your standard rails resource. Requires Rails 3.0+ (works with the beta).

WHY

I know about inherited_resources, make_resourceful etc. I’ve used them a lot.

With tiny_resource you get less flash. You get 1 easy-to-read controller and 1 helper file which provides just about everything. With tiny_resource you can easily see and modify the code that serves as base controller code for your rails resources. It’s not as configurable a resource plugin. If you need to change it use normal class overloading. Or just modify tiny_resource.rb. It’s one file to muck around with. You can do it :).

INSTALL

1) Copy the 3 files to whatever directory

2) Configure Rails:

config.autoload_paths += %W( /path/to/that/directory )

or

1) Copy tiny_resource_controller.rb and tiny_resource_helpers.rb to your controllers directory. Bam, ready to use.

USAGE

class PagesController < TinyResourceController
  #
  # You now have the standard index, new, edit, create, update, destroy etc..
  # If you need to change some behaivor just modify tiny_resource_controller.rb (or a copy of it)
  #
  # If you need to overload any method, you have 3 controller-methods to help you:
  #
  # * resource        # the current resource ( model.find(params[:id] )
  # * collection      # the current collection ( model.all )
  # * model           # the activerecord model, for example Project
  #
end

There’s also TinyUserResourceController that creates, updates and destroys data through current_user.

OVERLOADING

You can overload any method you want. If you want the tiny resource helpers to work as normal use self.collection= and self.resource= See example bellow:

class PagesController < TinyResourceController
  # Add pagination to the index-listing
  def index
    self.collection = Page.paginate(:per_page => 10, :page => params[:page])
  end

  # This overloading doesn't change any behaivor but it uses the tiny resource helper "model" instead of "Page"
  det show
    self.resource = model.find(params[:id]) # model in this case == Page.
  end
end

IN THE VIEW

The default TinyResourceController adds some view-helpers with the normal "helper_method"-call

* collection_path         # /projects
* resource_path(5)        # /project/5
* edit_resource_path(5)   # /projects/5/edit
* new_resource_path       # /projects/new
* resource                # the resource-object, available in show, edit, update)
* collection              # the collection-array, available in index
* model                   # the activerecord-model, for example Project

You also have collection_url, resource_url(5), edit_resource_url(5) and new_resource_url.
They work as their *_path equivalents but with full URLs instead.

About

Minimalistic abstract resource controller for rails

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published