aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Reaction/UI/Controller/Role/GetCollection.pm
blob: 8f40ce2160bbff76e2f71bb8eeed6065e2e86187 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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__;

=head1 NAME

Reaction::UI::Controller::Role::GetCollection

=head1 DESCRIPTION

Provides a C<get_collection> method, which fetches an C<Collection> object
from a specified model.

=head1 SYNOPSYS

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

=head1 ATTRIBUTES

=head2 model_name

The name of the model this controller will use as it's data source. Should be a
name that can be passed to C<$C-E<gt>model>

=head2 collection_name

The name of the collection whithin the model that this Controller will be
utilizing.

=head1 METHODS

=head2 get_collection $c

Returns an instance of the collection this controller uses.

=head1 SEE ALSO

=over4

=item L<Reaction::UI::Controller>

=item L<Reaction::UI::Controller::Role::Action::Simple>

=item L<Reaction::UI::Controller::Role::Action::Object>

=item L<Reaction::UI::Controller::Role::Action::List>

=item L<Reaction::UI::Controller::Role::Action::View>

=item L<Reaction::UI::Controller::Role::Action::Create>

=item L<Reaction::UI::Controller::Role::Action::Update>

=item L<Reaction::UI::Controller::Role::Action::Delete>

=item L<Reaction::UI::Controller::Role::Action::DeleteAll>

=back

=head1 AUTHORS

See L<Reaction::Class> for authors.

=head1 LICENSE

See L<Reaction::Class> for the license.

=cut