added action_filter
[catagits/Reaction.git] / lib / Reaction / UI / ViewPort / Action.pm
1 package Reaction::UI::ViewPort::Action;
2
3 use Reaction::Class;
4
5 use MooseX::Types::URI qw/Uri/;
6 use MooseX::Types::Moose qw/Int Str/;
7 use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
8
9 use namespace::clean -except => [ qw(meta) ];
10
11 extends 'Reaction::UI::ViewPort::Object::Mutable';
12 with 'Reaction::UI::ViewPort::Action::Role::OK';
13
14 has message => (is => 'rw', isa => Str);
15 has '+model' => (handles => [qw/error_message has_error_message/]);
16
17 #this has to fucking go. it BLOWS.
18 has method => (
19   is => 'rw',
20   isa => NonEmptySimpleStr,
21   default => sub { 'post' }
22 );
23
24 has action => ( is => 'rw', isa => Uri );
25
26 has changed => (
27   is => 'rw',
28   isa => Int,
29   reader => 'is_changed',
30   default => sub{0}
31 );
32
33 sub can_apply {
34   my ($self) = @_;
35   foreach my $field ( @{ $self->fields } ) {
36     return 0 if $field->needs_sync;
37     # if e.g. a datetime field has an invalid value that can't be re-assembled
38     # into a datetime object, the action may be in a consistent state but
39     # not synchronized from the fields; in this case, we must not apply
40   }
41   return $self->model->can_apply;
42 }
43
44 sub do_apply {
45   shift->model->do_apply;
46 }
47
48 after apply_child_events => sub {
49   # interrupt here because fields will have been updated
50   my ($self) = @_;
51   $self->sync_action_from_fields;
52 };
53
54 sub sync_action_from_fields {
55   my ($self) = @_;
56   foreach my $field (@{$self->fields}) {
57     $field->sync_to_action; # get the field to populate the $action if possible
58   }
59   $self->model->sync_all;
60   foreach my $field (@{$self->fields}) {
61     $field->sync_from_action; # get errors from $action if applicable
62   }
63 }
64
65
66 __PACKAGE__->meta->make_immutable;
67
68 1;
69
70 __END__;
71
72 =head1 NAME
73
74 Reaction::UI::ViewPort::Action - Provide user with a form with OK, Apply and Close.
75
76 =head1 SYNOPSIS
77
78   $controller->push_viewport('Reaction::UI::ViewPort::Action',
79     model           => $interface_model_action,
80     field_order     => [qw( firstname lastname )],
81     excluded_fields => [qw( password )],
82   );
83
84 =head1 DESCRIPTION
85
86 This subclass of L<Reaction::UI::ViewPort::Object::Mutable> is used for 
87 rendering a complete form supporting Apply, Close and OK.
88
89 =head1 ATTRIBUTES
90
91 =head2 message
92
93 =head2 model
94
95 Inherited from L<Reaction::UI::ViewPort::Object::Mutable>. Must be a
96 L<Reaction::InterfaceModel::Action>.
97
98 Also handles C<error_message> and C<has_error_message> methods.
99
100 =head2 method
101
102 post / get
103
104 =head2 changed
105
106 Returns true if a field has been edited.
107
108 =head1 METHODS
109
110 =head2 can_apply
111
112 Returns true if no field C<needs_sync> and the L</model> C<can_apply>.
113
114 =head2 do_apply
115
116 Delegates to C<do_apply> on the L</model>, which is a 
117 L<Reaction::InterfaceModel::Action>.
118
119 =head2 sync_action_from_fields
120
121 Firstly calls C<sync_to_action> on every L<Reaction::UI::ViewPort::Field::Mutable>
122 in L<fields|Reaction::UI::ViewPort::Object/fields>. Then it calls C<sync_all> on
123 the L<Reaction::InterfaceModel::Action> in L</model>. Next it will call
124 C<sync_from_action> on every field to repopulate them from the L</model>.
125
126 =head1 SUBCLASSING
127
128   package MyApp::UI::ViewPort::Action;
129   use Reaction::Class;
130   use MooseX::Types::Moose qw( Int );
131
132   use namespace::clean -except => 'meta';
133
134   extends 'Reaction::UI::ViewPort::Action';
135
136   has render_timestamp => (
137     is       => 'ro',
138     isa      => Int,
139     default  => sub { time },
140     required => 1,
141   );
142
143   has '+field_order' => (default => sub {[qw( firstname lastname )]});
144
145   1;
146
147 =head1 SEE ALSO
148
149 L<Reaction::UI::ViewPort>
150
151 L<Reaction::UI::ViewPort::Object>
152
153 L<Reaction::UI::ViewPort::Object::Mutable>
154
155 L<Reaction::InterfaceModel::Action::Role::Apply>
156
157 L<Reaction::InterfaceModel::Action::Role::Close>
158
159 L<Reaction::InterfaceModel::Action::Role::OK>
160
161 =head1 AUTHORS
162
163 See L<Reaction::Class> for authors.
164
165 =head1 LICENSE
166
167 See L<Reaction::Class> for the license.
168
169 =cut
170