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