search spec components factored out of T365
[catagits/Reaction.git] / lib / Reaction / Manual / ActionPrototypes.pod
1 =head1 Changes to the Action Prototype Mechanism
2
3 =head1 DESCRIPTION
4
5 After Reaction 0.001001 the API used to create links for different actions in
6 the L<ViewPort::Collection::Grid|Reaction::UI::ViewPort::Collection::Grid>
7 changed significantly. The aim of the changes was to create a simpler API that
8 was more concise, flexible, and didn't tie unneccessary controller logic in the
9 ViewPort layer.
10
11 =head1 Major Changes
12
13 =head2 Controller Layer
14
15 =head3 L<Reaction::UI::Controller::Collection>
16
17 =over 4
18
19 =item The default display class for the C<list> action is now
20 L<Grid|Reaction::UI::ViewPort::Collection::Grid>.
21
22 =item Addition of the C<default_member_actions> and C<default_collection_actions>
23
24 =item Addition of the C<_build_member_action_prototype> and
25 C<_build_collection_action_prototype> methods. These are used by
26 C<_build_action_viewport_args> to create prototypes for collection and member
27 actions.
28
29 =back
30
31 =head3 L<Reaction::UI::Controller::Collection::CRUD>
32
33 By default, enable C<create>, C<update>, C<delete>, C<delete_all>, actions. 
34
35 =head2 ViewPort Layer
36
37 =head3 L<Reaction::UI::ViewPort::Collection::Grid>
38
39 =over 4
40
41 =item Add the C<member_action_count> attribute. It allows the controller to
42 know how many actions to expect to lay out the UI properly.
43
44 =item Default to member-class
45 L<Grid::Member::WithActions|Reaction::UI::ViewPort::Collection::Grid::Member::WithActions>
46
47 =back
48
49 =head2 L<Reaction::UI::ViewPort::Role::Actions>
50
51 Completely revamped the action-prototypes, added ordering support and moved to
52 using the new C<ViewPort::URI|Reaction::UI::ViewPort::URI>.
53
54 Most notably C<action_prototypes> is now a HASH ref.
55
56 =head1 Migration
57
58 In most cases, you shouldn't need to change much for migration, but if you had
59 custom actions in your controllers that were linked to by the CRUD system, or
60 you had excluded certain classes, you'll need to create some minor updates.
61
62 =head2 A custom collection action in your controller.
63
64     #old code
65     sub custom_action { ... }
66     sub _build_action_viewport_map {
67       my $map = shift->next::method(@_);
68       $map->{custom_action} = 'Reaction::UI::ViewPort::Action';
69       return $map;
70     }
71     sub _build_action_viewport_args {
72       my $args = shift->next::method(@_);
73       my $custom_proto = {
74         label => 'Create',
75         action => sub { [ '', 'create',    $_[1]->req->captures ] } 
76       };
77       my $protos = $args->{list}->{action_prototypes};
78       push(@$protos, $custom_proto);
79       return $args;
80     }
81
82     #new code:
83     sub custom_action { ... }
84     sub _build_action_viewport_map {
85       my $map = shift->next::method(@_);
86       $map->{custom_action} = 'Reaction::UI::ViewPort::Action';
87       return $map;
88     }
89     sub _build_default_collection_actions {
90       [ @{shift->next::method(@_)}, 'custom_action'];
91     }
92
93 =head2 A custom member action in your controller.
94
95     #old code
96     sub custom_action { ... }
97     sub _build_action_viewport_map {
98       my $map = shift->next::method(@_);
99       $map->{custom_action} = 'Reaction::UI::ViewPort::Action';
100       return $map;
101     }
102     sub _build_action_viewport_args {
103       my $args = shift->next::method(@_);
104       my $custom_proto = {
105         label => 'Create',
106         action => sub { [ '', 'create',    $_[1]->req->captures ] } 
107       };
108       my $protos = $args->{list}->{Member}->{action_prototypes};
109       push(@$protos, $custom_proto);
110       return $args;
111     }
112
113     #new code:
114     sub custom_action { ... }
115     sub _build_action_viewport_map {
116       my $map = shift->next::method(@_);
117       $map->{custom_action} = 'Reaction::UI::ViewPort::Action';
118       return $map;
119     }
120     sub _build_default_member_actions {
121       [ @{shift->next::method(@_)}, 'custom_action'];
122     }
123
124
125 =head2 Disabling a default collection action
126
127     #old code
128     sub delete_all {}
129     sub _build_action_viewport_args {
130       my $args = shift->next::method(@_);
131       #remove the delete all action
132       my $protos = $args->{list}->{action_prototypes};
133       @$protos = grep { $_->{label} !~ /Delete all/i } @$protos;
134       return $args;
135     }
136
137     #new code
138     sub delete_all {}
139     sub _build_default_collection_actions {
140       [ grep {$_ ne 'delete_all'} @{ shift->next::method(@_) } ];
141     }
142
143     #or ...
144     sub delete_all {}
145     sub _build_action_viewport_args {
146       my $args = shift->next::method(@_);
147       my $protos = $args->{list}->{action_prototypes};
148       delete $protos->{delete_all};
149       return $args;
150     }
151
152
153 =head2 Changing the label of a collection action
154
155     #old code
156     sub _build_action_viewport_args {
157       my $args = shift->next::method(@_);
158       my $protos = $args->{list}->{action_prototypes};
159       $proto = grep { $_->{label} eq 'Delete all' } @$protos;
160       $proto->{label} = 'New Label';
161       return $args;
162     }
163
164     #new code
165     sub delete_all {}
166     sub _build_action_viewport_args {
167       my $args = shift->next::method(@_);
168       my $protos = $args->{list}->{action_prototypes};
169       $proto->{delete_all}->{label} = 'New Label';
170       return $args;
171     }
172
173     #or ...
174     __PACKAGE__->config(action => { list => { ViewPort => {
175         action_prototypes => { delete_all => {label => 'New Label'} }
176       },
177     );
178
179 =cut