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