Crudify
A dynamic resource controller for Rails 3 that keeps your controllers nice and skinny.
Crudify was shamlessly robbed from refinerycms's internals and customized for use in other projects. Much credit and many thanks to the guys at resolve digital for all their hard work on the refinery cms project. Keep it up!
Usage
In its most basic form, crudify is designed to be use like this:
class JelliesController < ApplicationController
crudify :jelly
end
Ok, so what does it do? The short answer; everything that you'd want it to. In more detail, crudify turns your controller into a full-fledged CRUD controller with index
, new
, create
, show
, edit
, update
, and destroy
. But wait, there's more! Inside each of these standard methods are several hook methods designed to make customizing your controllers even easier that over-riding crudify's methods. Over-riding; say what? ...
Say you want to customize an action that's being defined by crudify, simply over-ride it!
class JelliesController < ApplicationController
crudify :jelly
def create
@jelly = Jelly.new(params[:jelly])
# ... the rest of your custom action
end
end
Ok that seems easy enough, but what if my action is just a tiny bit different? That's where the hook methods come in...
Hook Methods
Laced into crudify's actions are a module full of methods designed to make customizing your controller quick and simple. Let's examine these methods in further detail starting with create.
Here's what lines #45-59 in lib/crudify/class_methods.rb
will produce in our Jellies controller:
def create
# if the position field exists, set this object as last object, given the conditions of this class.
if Jelly.column_names.include?("position")
params[:jelly].merge!({
:position => ((Jelly.maximum(:position, :conditions => "")||-1) + 1)
})
end
@instance = @jelly = Jelly.create(params[:jelly])
before_create
if @instance.valid? && @instance.save
successful_create
else
failed_create
end
end
Just before the calls to valid?
and save
, you'll see before_create
; the first hook method in the action. Looking further into the source, before_create
is nothing more than a blank action, waiting to be overwritten:
def before_create
# just a hook!
puts "> Crud::before_create" if @crud_options[:log]
before_action
end
Notice that before_create
calls a second hook; before_action
. This is a generic hook that fires before every crud method's call to save
, update
or destroy
. This means it might be helpful for you to call super
when over-riding this method so that the chain of hooks keeps firing. Inside the before_action
method we'll decide what to use as flash messages with set_what
. Here's the code for before_action
:
def before_action
# just a hook!
puts "> Crud::before_action" if @crud_options[:log]
set_what
true
end
Ok Ok, so we're gettin' kind of deep here. Let's get back to the basic concept; Skinny, sexy and easy on the eyes. (Are we still talking ruby here?)
class InquiriesController < ApplicationController
crudify :inquiry
def before_create
@inquiry.ip_address = request.remote_addr
super
end
end
Or an after-action hook:
class InquiriesController < ApplicationController
crudify :inquiry
def successful_create
InquiryMailer.(@inquiry).deliver!
super
end
end
To find out more about crudify, read the source! Here's some helpful links:
- For available options: Crudify::Base
- For available hooks: Crudify::HookMethods
- To see which hooks go where: Cruidfy::ClassMethods
Or check out the demo app in test/dummy
...
Testing
Shoulda and Capybara/Selenium tests can be run by cloning the repo and running rake
:
git clone git://github.com/citrus/crudify.git
cd crudify
bundle install
rake
To Do
- Documentation & Examples
- Nested set tests and demo (haven't even tried this yet :/)
- Generally more thorough tests
- Refactoring/Optimizing
License
Copyright (c) 2011 Spencer Steffen, released under the New BSD License All rights reserved.