Catalyst::Plugin::AutoCRUD

2009-09-02 − 🏷 catalyst 🏷 crud 🏷 extensions 🏷 perl

One major factor in Catalyst's success has been extensibility. I plan to do a series on extensions that I've found useful recently. To kick it off, I'll feature a model which gives me a simple and useful admin interface to my DBIx::Class models.

When I first got involved with MVC Web Frameworks like Maypole and Ruby on Rails, a big selling point was the ability to generate CRUD (Create/Read/ Update/Delete) interfaces to your data model. Ruby on Rails does this through a mechanism called scaffolding, where it adds a set of actions to your controller. It soon became apparent that these CRUD frameworks did not live up to their promise as a base for creating your own custom actions. Usually, you spend more time customizing these controllers than you would just implementing the features you require.

However, there is still a good use for these for giving your admins a direct interface to your data model. There has been several iterations of these tools for the Catalyst framework, but with Catalyst::Plugin::AutoCRUD, I've finally found something easy to plug into your existing app, which integrates well with most setups.

autocrud

For most apps, all you need to do is plug it into the main application class by adding AutoCRUD to your list of plugins. This will plug a complete CRUD application, including controllers and view and templates into your app. By default it will attach itself at '/autocrud', but you can easily change that in config. Just add this to your config file:


      basepath admin

and it will respond to /admin/* instead. Another common requirement is to add authentication for the admin interface. One way to accomplish that is by using Catalyst's auto handler functionality. Add a method like this in your Root controller

sub auto :Private {
    my ( $self, $c ) = @_;
    if ($c->action->reverse =~ /^autocrud\//) {
        $c->authenticate({},'users');
    }
    return 1;
}

Note that my example is using HTTP Basic auth. The actual authenticate call needs to be customized for your realm.

AutoCRUD supports multiple DBIC Schemas, and if will automatically provide you with a list to let you pick which one to work with. After that, you choose a Result class, and you have access to an extensive AJAX-enhanced database admin tool. You can search and browse data, as well as edit it and add new rows easily. AutoCRUD also understands your DBIC relationships, so you can easily see data related to the current rows.

Like it's predecessors, I do not recommend trying to make this tool into a generic 'everything'-application. However, if you use it for what it is, you can save countless of development hours making trivial admin tools. Since it works again your DBIC Schema, you'll also get the advantage of keeping your business logic in the data model. Things like DBIC timestamps will Just Work.

There still might be some polishing left to do on AutoCRUD, but I already find it a hugely useful tool. You can install it just like you would any other CPAN module using

$ cpan Catalyst::Plugin::AutoCRUD