r31500@martha (orig r1229): purge | 2009-09-11 05:51:11 -0400
[catagits/Reaction.git] / lib / Reaction / UI / Controller / Role / Action / Create.pm
CommitLineData
931cbc8d 1package Reaction::UI::Controller::Role::Action::Create;
2
3use Moose::Role -traits => 'MethodAttributes';
4use Reaction::UI::ViewPort::Action;
5
6requires qw/get_collection make_context_closure setup_viewport/;
7
8sub create :Action :Args(0) {
9 my ($self, $c) = @_;
10 my $target = $c->stash->{collection} || $self->get_collection($c);
11 my %vp_args = ( model => $target->action_for('Create') );
12
13 if( $self->can('on_create_apply_callback') ){
14 my $apply = sub { $self->on_create_apply_callback( @_) };
15 $vp_args{on_apply_callback} = $self->make_context_closure( $apply );
16 }
17 if( $self->can('on_create_close_callback') ){
18 my $close = sub { $self->on_create_close_callback( @_) };
19 $vp_args{on_close_callback} = $self->make_context_closure( $close );
20 }
21
22 $self->setup_viewport( $c, \%vp_args );
23}
24
25around _build_action_viewport_map => sub {
26 my $orig = shift;
27 my $map = shift->$orig( @_ );
28 $map->{create} = 'Reaction::UI::ViewPort::Action';
29 return $map;
30};
31
321;
33
34__END__;
35
36=head1 NAME
37
38Reaction::UI::Controller::Role::Action::Create - Create action
39
40=head1 DESCRIPTION
41
42Provides a C<create> action, which sets up an L<Action Viewport|Reaction::UI::Viewport::Action>
43by calling C<action_for> on either the object located in the C<collection> slot
44of the C<stash> or on the object returned by the method C<get_collection>.
45
46=head1 SYNOPSYS
47
48 package MyApp::Controller::Foo;
49
50 use base 'Reaction::Controller';
51 use Reaction::Class;
52
53 with(
54 'Reaction::UI::Controller::Role::GetCollection',
55 'Reaction::UI::Controller::Role::Action::Simple',
56 'Reaction::UI::Controller::Role::Action::Create'
57 );
58
59 __PACKAGE__->config( action => {
60 create => { Chained => 'base' },
61 } );
62
63 sub base :Chained('/base') :CaptureArgs(0) {
64 ...
65 }
66
67 sub on_create_apply_callback{ #optional callback
68 my($self, $c, $vp, $result) = @_;
69 ...
70 }
71
72 sub on_create_close_callback{ #optional callback
73 my($self, $c, $vp) = @_;
74 ...
75 }
76
77=head1 ROLES CONSUMED
78
79This role also consumes the following roles:
80
81=over4
82
83=item L<Reaction::UI::Controller::Role::Action::Simple>
84
85=back
86
87=head1 REQUIRED METHODS
88
89The following methods must be provided by the consuming class:
90
91=over4
92
93=item C<get_collection>
94
95=item C<make_context_closure>
96
97=back
98
99=head1 ACTIONS
100
101=head2 create
102
103Chain endpoint with no args, sets up the viewport with the appropriate action.
104If the methods C<on_create_apply_callback> and C<on_create_close_callback> are
105present in the consuming class, they will be used as callbacks in the viewport.
106
107=head1 METHODS
108
109=head2 _build_action_viewport_map
110
111Extends to set the C<create> key in the map to L<Reaction::UI::ViewPort::Action>
112
113=head1 SEE ALSO
114
115=over4
116
117=item L<Reaction::UI::Controller>
118
119=item L<Reaction::UI::Controller::Role::GetCollection>
120
121=item L<Reaction::UI::Controller::Role::Action::Simple>
122
123=item L<Reaction::UI::Controller::Role::Action::List>
124
125=item L<Reaction::UI::Controller::Role::Action::View>
126
127=item L<Reaction::UI::Controller::Role::Action::Object>
128
129=item L<Reaction::UI::Controller::Role::Action::Update>
130
131=item L<Reaction::UI::Controller::Role::Action::Delete>
132
133=item L<Reaction::UI::Controller::Role::Action::DeleteAll>
134
135=back
136
137=head1 AUTHORS
138
139See L<Reaction::Class> for authors.
140
141=head1 LICENSE
142
143See L<Reaction::Class> for the license.
144
145=cut