Transactional and Authorized Methods in your Catalyst Model Class

| No Comments | No TrackBacks

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.

No TrackBacks

TrackBack URL:

Leave a comment

About this Entry

This page contains a single entry by Daniel Ruoso published on June 1, 2010 2:49 PM.

Writing Games in Perl - Part 7 - Game Map was the previous entry in this blog.

Producing ODF spreadsheets is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.