| Reaction documentation | Contained in the Reaction distribution. |
Reaction::UI::Controller::Role::GetCollection
Provides a get_collection method, which fetches an Collection object
from a specified model.
package MyApp::Controller::Foo;
use base 'Reaction::Controller';
use Reaction::Class;
with 'Reaction::UI::Controller::Role::GetCollection';
__PACKAGE__->config( model_name => 'MyAppIM', collection_name => 'foos' );
sub bar :Local {
my($self, $c) = @_;
my $obj = $self->get_collection($c)->find( $some_key );
}
The name of the model this controller will use as it's data source. Should be a
name that can be passed to $C->model
The name of the collection whithin the model that this Controller will be utilizing.
Returns an instance of the collection this controller uses.
See Reaction::Class for authors.
See Reaction::Class for the license.
| Reaction documentation | Contained in the Reaction distribution. |
package Reaction::UI::Controller::Role::GetCollection; use Moose::Role -traits => 'MethodAttributes'; has model_name => (isa => 'Str', is => 'rw', required => 1); has collection_name => (isa => 'Str', is => 'rw', required => 1); sub get_collection { my ($self, $c) = @_; my $model = $c->model( $self->model_name ); confess "Failed to find Catalyst model named: " . $self->model_name unless $model; my $collection = $self->collection_name; if( my $meth = $model->can( $collection ) ){ return $model->$meth; } elsif ( my $meta = $model->can('meta') ){ if ( my $attr = $model->$meta->find_attribute_by_name($collection) ) { my $reader = $attr->get_read_method; return $model->$reader; } } confess "Failed to find collection $collection"; } 1; __END__;