Commit | Line | Data |
ddccc6a2 |
1 | package Reaction::UI::ViewPort::Action; |
2 | |
3 | use Reaction::Class; |
4 | |
7b5e71ad |
5 | use MooseX::Types::URI qw/Uri/; |
e29819c4 |
6 | use MooseX::Types::Moose qw/Int Str/; |
7b5e71ad |
7 | use MooseX::Types::Common::String qw/NonEmptySimpleStr/; |
08451970 |
8 | |
9 | use namespace::clean -except => [ qw(meta) ]; |
b343a983 |
10 | |
11 | extends 'Reaction::UI::ViewPort::Object::Mutable'; |
08451970 |
12 | with 'Reaction::UI::ViewPort::Action::Role::OK'; |
13 | |
e29819c4 |
14 | has message => (is => 'rw', isa => Str); |
eb52e595 |
15 | has '+model' => (handles => [qw/error_message has_error_message/]); |
e29819c4 |
16 | |
b343a983 |
17 | #this has to fucking go. it BLOWS. |
18 | has method => ( |
19 | is => 'rw', |
20 | isa => NonEmptySimpleStr, |
21 | default => sub { 'post' } |
22 | ); |
08451970 |
23 | |
7b5e71ad |
24 | has action => ( is => 'rw', isa => Uri ); |
25 | |
08451970 |
26 | has changed => ( |
27 | is => 'rw', |
28 | isa => Int, |
29 | reader => 'is_changed', |
30 | default => sub{0} |
b343a983 |
31 | ); |
08451970 |
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 | |
81393881 |
65 | __PACKAGE__->meta->make_immutable; |
66 | |
114916fc |
67 | 1; |
ddccc6a2 |
68 | |
114916fc |
69 | __END__; |
ddccc6a2 |
70 | |
08451970 |
71 | =head1 NAME |
72 | |
63bb30b4 |
73 | Reaction::UI::ViewPort::Action - Provide user with a form with OK, Apply and Close. |
08451970 |
74 | |
75 | =head1 SYNOPSIS |
76 | |
63bb30b4 |
77 | $controller->push_viewport('Reaction::UI::ViewPort::Action', |
78 | model => $interface_model_action, |
79 | field_order => [qw( firstname lastname )], |
80 | excluded_fields => [qw( password )], |
81 | ); |
82 | |
08451970 |
83 | =head1 DESCRIPTION |
84 | |
599c1172 |
85 | This subclass of L<Reaction::UI::ViewPort::Object::Mutable> is used for |
86 | rendering a complete form supporting Apply, Close and OK. |
08451970 |
87 | |
88 | =head1 ATTRIBUTES |
89 | |
63bb30b4 |
90 | =head2 message |
91 | |
92 | =head2 model |
93 | |
94 | Inherited from L<Reaction::UI::ViewPort::Object::Mutable>. Must be a |
95 | L<Reaction::InterfaceModel::Action>. |
96 | |
97 | Also handles C<error_message> and C<has_error_message> methods. |
98 | |
599c1172 |
99 | =head2 method |
08451970 |
100 | |
599c1172 |
101 | post / get |
08451970 |
102 | |
103 | =head2 changed |
104 | |
105 | Returns true if a field has been edited. |
106 | |
08451970 |
107 | =head1 METHODS |
108 | |
599c1172 |
109 | =head2 can_apply |
08451970 |
110 | |
63bb30b4 |
111 | Returns true if no field C<needs_sync> and the L</model> C<can_apply>. |
112 | |
599c1172 |
113 | =head2 do_apply |
08451970 |
114 | |
63bb30b4 |
115 | Delegates to C<do_apply> on the L</model>, which is a |
116 | L<Reaction::InterfaceModel::Action>. |
117 | |
599c1172 |
118 | =head2 sync_action_from_fields |
08451970 |
119 | |
63bb30b4 |
120 | Firstly calls C<sync_to_action> on every L<Reaction::UI::ViewPort::Field::Mutable> |
121 | in L<fields|Reaction::UI::ViewPort::Object/fields>. Then it calls C<sync_all> on |
122 | the L<Reaction::InterfaceModel::Action> in L</model>. Next it will call |
123 | C<sync_from_action> on every field to repopulate them from the L</model>. |
124 | |
125 | =head1 SUBCLASSING |
126 | |
127 | package MyApp::UI::ViewPort::Action; |
128 | use Reaction::Class; |
129 | use MooseX::Types::Moose qw( Int ); |
130 | |
131 | use namespace::clean -except => 'meta'; |
132 | |
133 | extends 'Reaction::UI::ViewPort::Action'; |
134 | |
135 | has render_timestamp => ( |
136 | is => 'ro', |
137 | isa => Int, |
138 | default => sub { time }, |
139 | required => 1, |
140 | ); |
141 | |
142 | has '+field_order' => (default => sub {[qw( firstname lastname )]}); |
143 | |
144 | 1; |
145 | |
599c1172 |
146 | =head1 SEE ALSO |
08451970 |
147 | |
599c1172 |
148 | L<Reaction::UI::ViewPort> |
08451970 |
149 | |
599c1172 |
150 | L<Reaction::UI::ViewPort::Object> |
08451970 |
151 | |
599c1172 |
152 | L<Reaction::UI::ViewPort::Object::Mutable> |
08451970 |
153 | |
599c1172 |
154 | L<Reaction::InterfaceModel::Action::Role::Apply> |
08451970 |
155 | |
599c1172 |
156 | L<Reaction::InterfaceModel::Action::Role::Close> |
08451970 |
157 | |
599c1172 |
158 | L<Reaction::InterfaceModel::Action::Role::OK> |
08451970 |
159 | |
160 | =head1 AUTHORS |
161 | |
162 | See L<Reaction::Class> for authors. |
163 | |
164 | =head1 LICENSE |
165 | |
166 | See L<Reaction::Class> for the license. |
167 | |
168 | =cut |
169 | |