added action_filter
[catagits/Reaction.git] / lib / Reaction / UI / ViewPort / Role / Actions.pm
1 package Reaction::UI::ViewPort::Role::Actions;
2
3 use Reaction::Role;
4 use Reaction::UI::ViewPort::URI;
5
6 use namespace::clean -except => [ qw(meta) ];
7
8 has actions => (
9   is => 'ro',
10   isa => 'ArrayRef',
11   lazy_build => 1
12 );
13
14 has action_order => (
15   is => 'ro',
16   isa => 'ArrayRef'
17 );
18
19 has action_filter => (
20   isa => 'CodeRef', is => 'ro',
21   required => '1', lazy => '1',
22   default => sub {
23       sub { return [keys %{ shift->action_prototypes }] }
24   }
25 );
26
27 has action_prototypes => (
28   is => 'ro',
29   isa => 'HashRef',
30   required => 1,
31   default => sub{ {} }
32 );
33
34 has computed_action_order => (
35   is => 'ro',
36   isa => 'ArrayRef',
37   lazy_build => 1
38 );
39
40 sub _build_computed_action_order {
41   my $self = shift;
42   my $ordered = $self->sort_by_spec(
43     ($self->has_action_order ? $self->action_order : []),
44     [ keys %{ $self->action_prototypes } ]
45   );
46   return $self->action_filter->($ordered, $self->model);
47 }
48
49 sub _build_actions {
50   my ($self) = @_;
51   my (@act, $i);
52   my $ctx = $self->ctx;
53   my $loc = $self->location;
54   my $target = $self->model;
55
56   foreach my $proto_name ( @{ $self->computed_action_order } ) {
57     my $proto = $self->action_prototypes->{$proto_name};
58     my $uri = $proto->{uri} or confess('uri is required in prototype action');
59     my $label = exists $proto->{label} ? $proto->{label} : $proto_name;
60     my $layout = exists $proto->{layout} ? $proto->{layout} : 'uri';
61
62     my $action = Reaction::UI::ViewPort::URI->new(
63       location => join ('-', $loc, 'action', $i++),
64       uri => ( ref($uri) eq 'CODE' ? $uri->($target, $ctx) : $uri ),
65       display => ( ref($label) eq 'CODE' ? $label->($target, $ctx) : $label ),
66       layout => ( ref($layout) eq 'CODE' ? $layout->($target, $ctx) : $layout ),
67     );
68     push(@act, $action);
69   }
70   return \@act;
71 }
72
73 1;
74
75 __END__;
76
77 =head1 NAME
78
79 Reaction::UI::ViewPort::Role::Actions
80
81 =head1 DESCRIPTION
82
83 A role to ease attaching actions to L<Reaction::InterfaceModel::Object>s
84
85 =head1 ATTRIBUTES
86
87 =head2 actions
88
89 Read-only, lazy-building ArrayRef of URI objects pointing to actions.
90
91 =head2 action_prototypes
92
93 A HashRef of prototypes for building the Action links. The prototypes should be
94 composed like these:
95
96     my %action_prototypes = (
97       example_action => { label => 'Example Action', uri => $uri_obj },
98     );
99
100     #or you can get fancy and do something like what is below:
101     sub make_label{
102       my($im, $ctx) =  @_; #InterfaceModel::Object/Collection, Catalyst Context
103       return 'label_text';
104     }
105     sub make_uri{
106       my($im, $ctx) =  @_; #InterfaceModel::Object/Collection, Catalyst Context
107       return return $ctx->uri_for('some_action');
108     }
109     my %action_prototypes = (
110       example_action => { label => \&make_label, uri => \&make_uri },
111     );
112
113 =head2 action_order
114
115 User-provided ArrayRef with how the actions should be ordered eg
116
117      action_order => [qw/view edit delete/]
118
119 =head2 computed_action_order
120
121 Read-only lazy-building ARRAY ref. The final computed action order. This may
122 differ from the C<action_order> provided if you any actions were not included
123 in that list.
124
125 =head1 METHODS
126
127 =head2 _build_actions
128
129 Cycle through the C<computed_action_order> and create a new
130 L<ViewPort::URI|Reaction::UI::ViewPort::URI> object for each action using the
131 provided prototypes.
132
133 =head2 _build_computed_action_order
134
135 Compute the final action ordering by using the provided C<action_order> as a
136 spec to order all the present actions (the keys of C<action_prototypes>)
137
138 =head1 ACTION PROTOTYPES
139
140 Action prototypes are simply hashrefs that must contain a C<uri> key and may
141 contain a C<label> key. The label can be anything that the display attribute of
142 L<ViewPort::URI|Reaction::UI::ViewPort::URI> will accept, usually a scalar or a
143 ViewPort. The value for C<uri> may be either a scalar, a L<URI> object (or
144 anything that C<ISA URI>).
145
146 Additionally, both C<label> and C<uri> can be CODE refs. In this case, the code
147 will be executed at C<_build_actions> time and will recieve two arguments, the
148 value returned by C<model> and the value returned by C<ctx> in that order. Both
149 of these methods should be implemented in the consuming class. By convention,
150 model refers to the target of the action, an C<InterfaceModel::Object> in the
151 case of a member action and an C<InterfaceModel::Collection> in the case of a
152 Collection action. C<ctx> should be the current Catalyst context.
153
154 =head1 AUTHORS
155
156 See L<Reaction::Class> for authors.
157
158 =head1 LICENSE
159
160 See L<Reaction::Class> for the license.
161
162 =cut