1 Jun 2010 ruoso   » (Journeyer)

Transactional and Authorized Methods in yout Catalyst Model Class

In a previous post I have showed how to implement a hack to have transactions and authorization around Catalyst model classes. I am now proud to say that after some uploads and a fix in MooseX::Method::Signatures (this post requires at least version 0.31 of that module), you can now write very elegant model classes.

The requirement is simple, you usually need your model methods to be enclosed in transactions and to be subject to some kind of authorization mechanism. Now the code looks like:

package MyApp::Model::MyModel;
use Moose;
use MooseX::Method::Signatures;
use aliased 'MooseX::Meta::Method::Transactional';
use aliased 'MooseX::Meta::Method::Authorized';
extends 'Catalyst::Model';
with 'Catalyst::Component::InstancePerContext';

has user => (is => 'ro');
has schema => (is => 'ro');

sub build_per_context_instance {
    my ($self, $c) = @_;
    return MyApp::Model::MyModel->new
        ({ user => $c->user, schema => $self->model('DBIC') });

method get_product_price($product) does Transactional does Authorized(requires => ['customer']) {
    return $product->prices->find({ 'me.listing' => "base" });

method get_product_minprice($product) does Transactional does Authorized(requires => ['seller']) {
    return $product->prices->find({ 'me.listing' => "minimum" });


Of course you also need MooseX::Meta::Method::Transactional, MooseX::Meta::Method::Authorized and Catalyst::Component::InstancePerContext for this code to work. But it certainly is very pretty.

Syndicated 2010-06-01 13:49:02 from Daniel Ruoso

Latest blog entries     Older blog entries

New Advogato Features

New HTML Parser: The long-awaited libxml2 based HTML parser code is live. It needs further work but already handles most markup better than the original parser.

Keep up with the latest Advogato features by reading the Advogato status blog.

If you're a C programmer with some spare time, take a look at the mod_virgule project page and help us with one of the tasks on the ToDo list!