r31500@martha (orig r1229): purge | 2009-09-11 05:51:11 -0400
[catagits/Reaction.git] / lib / Reaction / UI / Controller / Role / Action / Simple.pm
1 package Reaction::UI::Controller::Role::Action::Simple;
2
3 use Moose::Role -traits => 'MethodAttributes';
4
5 requires 'push_viewport';
6 requires 'merge_config_hashes';
7
8 has action_viewport_map => (isa => 'HashRef', is => 'rw', lazy_build => 1);
9 has action_viewport_args => (isa => 'HashRef', is => 'rw', lazy_build => 1);
10
11 sub _build_action_viewport_map { {} }
12
13 sub _build_action_viewport_args { {} }
14
15 sub setup_viewport {
16   my ($self, $c, $vp_args) = @_;
17   my $action_name = $c->stack->[-1]->name;
18   my $vp = $self->action_viewport_map->{$action_name},
19   my $args = $self->merge_config_hashes(
20     $vp_args || {},
21     $self->action_viewport_args->{$action_name} || {} ,
22   );
23   return $self->push_viewport($vp, %$args);
24 }
25
26 1;
27
28 __END__;
29
30 =head1 NAME
31
32 Reaction::UI::Controller::Role::Action::Simple
33
34 =head1 DESCRIPTION
35
36 Provides a C<setup_viewport> method, which makes it easier to setup and
37 configure a viewport in controller actions.
38
39 =head1 SYNOPSYS
40
41     package MyApp::Controller::Foo;
42
43     use base 'Reaction::Controller';
44     use Reaction::Class;
45
46     with 'Reaction::UI::Controller::Role::Action::Simple';
47
48     __PACKAGE__->config(
49       action_viewport_map => { bar => 'Reaction::UI::Viewport::Object' },
50       action_viewport_args => { location => 'custom-location' },
51     );
52
53     sub bar :Local {
54       my($self, $c) = @_;
55       my $obj = $self->get_collection($c)->find( $some_key );
56       $self->setup_viewport($c, { model => $obj });
57     }
58
59 =head1 ATTRIBUTES
60
61 =head2 action_viewport_map
62
63 =over 4
64
65 =item B<_build_action_viewport_map> - Returns empty hashref by default.
66
67 =item B<has_action_viewport_map> - Auto generated predicate
68
69 =item B<clear_action_viewport_map>- Auto generated clearer method
70
71 =back
72
73 Read-write lazy building hashref. The keys should match action names in the
74 Controller and the value should be the ViewPort class that this action should
75 use.
76
77 =head2 action_viewport_args
78
79 Read-write lazy building hashref. Additional ViewPort arguments for the action
80 named as the key in the controller.
81
82 =over 4
83
84 =item B<_build_action_viewport_args> - Returns empty hashref by default.
85
86 =item B<has_action_viewport_args> - Auto generated predicate
87
88 =item B<clear_action_viewport_args>- Auto generated clearer method
89
90 =back
91
92 =head1 METHODS
93
94 =head2 setup_viewport $c, \%vp_args
95
96 Accepts two arguments, context, and a hashref of viewport arguments. It will
97 automatically determine the action name using the catalyst stack and call
98 C<push_viewport> with the ViewPort class name contained in the
99 C<action_viewport_map> with a set of options determined by merging C<$vp_args>
100 and the arguments contained in C<action_viewport_args>, if any.
101
102 =head1 SEE ALSO
103
104 =over4
105
106 =item L<Reaction::UI::Controller>
107
108 =item L<Reaction::UI::Controller::Role::Action::Simple>
109
110 =item L<Reaction::UI::Controller::Role::Action::Object>
111
112 =item L<Reaction::UI::Controller::Role::Action::List>
113
114 =item L<Reaction::UI::Controller::Role::Action::View>
115
116 =item L<Reaction::UI::Controller::Role::Action::Create>
117
118 =item L<Reaction::UI::Controller::Role::Action::Update>
119
120 =item L<Reaction::UI::Controller::Role::Action::Delete>
121
122 =item L<Reaction::UI::Controller::Role::Action::DeleteAll>
123
124 =back
125
126 =head1 AUTHORS
127
128 See L<Reaction::Class> for authors.
129
130 =head1 LICENSE
131
132 See L<Reaction::Class> for the license.
133
134 =cut