example of how to create forms that return users to the URI they came from
[catagits/Reaction.git] / lib / ComponentUI / Controller / TestModel / Bar.pm
1 package ComponentUI::Controller::TestModel::Bar;
2
3 use base 'Reaction::UI::Controller::Collection::CRUD';
4 use Reaction::Class;
5
6 __PACKAGE__->config(
7   model_name => 'TestModel',
8   collection_name => 'Bar',
9   action => {
10     base => { Chained => '/base', PathPart => 'testmodel/bar' },
11     create => { ViewPort => { layout => 'bar/create' } },
12     list => {
13       ViewPort => {
14         enable_order_by => [qw/name foo published_at/],
15         coerce_order_by => { foo => ['foo.last_name', 'foo.first_name'] },
16       }
17     }
18   },
19 );
20
21 sub get_collection {
22   my ($self, $c) = @_;
23   my $collection = $self->next::method($c);
24   return $collection->where({}, { prefetch => 'foo' });
25 }
26
27 sub create :Chained('base') {
28   my $self = shift;
29   my ($c) = @_;
30   my $action_vp = $self->next::method(@_);
31   my $self_uri = $c->uri_for($self->action_for('create'));
32   $action_vp->action($self_uri);
33
34   my $params = $c->request->parameters;
35   if ( defined $params->{return_to_uri} && $params->{return_to_uri} ){
36     if( $params->{return_to_uri} ne $c->request->uri ){
37       $action_vp->layout_args->{return_to_uri} = $params->{return_to_uri};
38     }
39   } elsif( $c->request->referer ne $c->request->uri) {
40     $action_vp->layout_args->{return_to_uri} = $c->request->referer;
41   }
42
43   return $action_vp;
44 }
45
46 sub on_create_close_callback {
47   my($self, $c, $vp) = @_;
48   if ( my $return_to_uri =  delete $c->request->parameters->{return_to_uri} ){
49     $c->response->redirect( $return_to_uri );
50   } else {
51     $self->redirect_to( $c, 'list' );
52   }
53   $c->detach;
54 }
55
56 1;