We’ve released the first version of admin_view, a gem that adds a minimal admin interface generator for your models. It works with Rails 3 and gives you a controller, views and specs, with an assumption that you’re authenticating users with Devise.
For example, if you type:
bundle exec rails g admin_view User --search_by name
you’ll get something like this:
The search form is powered by meta_search, and you can easily tweak it to cover more columns. You also get sortable columns, and screens to CRUD individual records.
The markup is bare bones, and this is on purpose - our only guideline was that it should look good with the Railscasts stylesheet, which we use for admin interface in all our applications.
admin_view generates code (not much), because in our practice one size can’t fit all. We always need to be able to change something about the default, scaffold-like stuff: tweak which columns go in the listing table, how admin creates a new record (if at all), what editing means (if at all), how data in the show action is connected to other records etc.
But we need to have a starting point that works, and is covered by specs. So if we need something special, we can quickly write a Cucumber scenario, and make it all green. We’re dealing with basic Rails stuff there, so change is rapid, and there’s no additional API to memorize. For these reasons, something like active_admin is not what we need.
Packaging the generator as a Rails 3 gem was challenging. We had to examine the source code of various plugins and Rails itself in order to figure out the naming conventions, and how the tests can be set up (that’s one reason we’re releasing this now and not in January when the first commit went in). I’m still hoping to enhance the tests to include one temporary model as a basis for testing views.
The source is small and it can be a reference to anyone who wants to implement a Rails 3 generator plugin as a gem. Here are some links that we found useful:
- Yehuda’s notes on what to put in a gem’s Gemfile
- Testing generators in Rails 3
- Getting a bloody Rails 3 generator running and testing it
- source of rails3-generators test_helper.rb
- Rails guide to making a generator
- Rails guide to making a plugin
- Making generators with Thor