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