Rails CRUD Actions Plugin 3

Posted by John Wulff Sun, 08 Apr 2007 09:06:00 GMT

For a while now I’ve been using a plugin I wrote that provides simple and versatile CRUD actions to Rails controllers.

About six months ago I was getting really fed up with writing the same actions over and over again: new, create, show, edit, update, list, destroy. So, I decided to abstract them out into a plugin.

All I have to do to get almost all the CRUD goodness I need is:
class NodeController < ApplicationController
  include CrudActions
end

Now the NodeController has new, create, show, edit, update, list, and destroy actions for the Node ActiveRecord Model. Likewise if CrudActions were included in ArticleController , ArticleController would have actions for the Article Model.

All of the actions work as you’d expect and assign variables for your views appropriately (@node, @nodes, @paginator, etc.).

For customization you can easily wrap the actions provided like so:
class NodeController < ApplicationController
  include CrudActions

  alias_method :original_list, :list
  def list
    Node.with_scope(:find => { :conditions => 'id != 1' } ) do
      original_list
    end
  end
end

The source is hosted at Google Code: http://code.google.com/p/rails-crud-actions.

The meat of the plugin is in crud_actions.rb.

There is still plenty of room for this concept to grow, implementing respond_to for example. However, I’m not sure that I’m going down a wise path here. Everything about this approach feels shaky… but it is so damned convenient.

Please have a look. I’d really like some feedback.

Trackbacks

Use the following link to trackback from your own site:
http://www.johnwulff.com/trackbacks?article_id=6

Comments

Leave a comment

  1. Avatar
    Giuseppe about 1 month later:
    Hi John, I too got fed up with writing the same actions over and over, and have been looking for a good approach to the problem. What I ended up doing was somewhat similar to your solution. I created a: class ContentController < ApplicationController where all the CRUD action "prototypes" can be found (BTW, not as nice code as yours), and then have all controllers that will use them inherit from ContentController instead of ApplicationController. This way, in case some of the actions in some of the controllers need some extra things done, compared to the prototype, I can do: class PeopleController < ContentsController def index super @extra = get_some_extra_data end So I agree with you, this type of mechanism *is* useful and feels shaky at the same time. Your CrudActions functions are different from mine, so if I had stumbled into your plugin before writing my own thingy I would have certainly used it, but I would have certainly changed crud_actions.rb to fit my own purposes. Which makes me think that CrudActions should perhaps be a generator, something that shows you a good coding practice, but by definition is meant to be messed with (unlike a plugin). One aspect that in my opinion deserves further work is how to customize actions (e.g., your use of with_scope and my putting extra statements before or after calling super) Cheers, Giuseppe Bertini
  2. Avatar
    Giuseppe about 1 month later:
    hey where did my paragraphs go! sorry for the difficult to discern blob of text. Cheers again. G
  3. Avatar
    John Wulff about 1 month later:
    Nice to know I'm not the only one Giuseppe. From what I heard at RailsConf07 there will be no more with_scope in 2.0. Just one more hint that this may not be a good idea...
Comments