June 2010 Archives

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" });
};

1;

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.

About this Archive

This page is an archive of entries from June 2010 listed from newest to oldest.

May 2010 is the previous archive.

July 2010 is the next archive.

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