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