[Dbix-class] Class methods on result source classes

Matt S Trout dbix-class at trout.me.uk
Mon Sep 3 20:24:25 GMT 2007


On Mon, Sep 03, 2007 at 01:22:49PM +0100, Pedro Melo wrote:
> Hi,
> 
> I'm using a "fat model" approach in a small project and one thing  
> that keeps popping up is class methods.
> 
> For example, I usually have a can_edit() method in a base class that  
> all my sources use. This allows me to do something like this:
> 
>   $schema->resultset('Books')->find($id)->can_edit($current_user);
> 
> The can_edit() method will receive as "self" the Book with the $id,  
> and it will just work.
> 
> But if I want to write a can_create(), I do something like:
> 
> $schema->resultset('Books')->->can_create($current_user);
> 
> or like this
> 
> $schema->resultset('Books')->result_class->can_create($current_user);
> 
> The second one is longer but easier to do because it doesn't require  
> me to create a ResultSet class for each source.
> 
> Any common, more elegant, solution to this problems?

Creating the resultset class is both correct and elegant.

See load_namespaces for a way to save a bit of typing.

-- 
      Matt S Trout       Need help with your Catalyst or DBIx::Class project?
   Technical Director    Want a managed development or deployment platform?
 Shadowcat Systems Ltd.  Contact mst (at) shadowcatsystems.co.uk for a quote
http://chainsawblues.vox.com/                    http://www.shadowcat.co.uk/ 



More information about the DBIx-Class mailing list