Commit | Line | Data |
---|---|---|
ddccc6a2 | 1 | package Reaction::UI::ViewPort::Action; |
2 | ||
3 | use Reaction::Class; | |
4 | ||
e29819c4 | 5 | use MooseX::Types::Moose qw/Int Str/; |
08451970 | 6 | use Reaction::Types::Core qw/NonEmptySimpleStr/; |
7 | ||
8 | use namespace::clean -except => [ qw(meta) ]; | |
b343a983 | 9 | |
10 | extends 'Reaction::UI::ViewPort::Object::Mutable'; | |
08451970 | 11 | with 'Reaction::UI::ViewPort::Action::Role::OK'; |
12 | ||
e29819c4 | 13 | has message => (is => 'rw', isa => Str); |
eb52e595 | 14 | has '+model' => (handles => [qw/error_message has_error_message/]); |
e29819c4 | 15 | |
b343a983 | 16 | #this has to fucking go. it BLOWS. |
17 | has method => ( | |
18 | is => 'rw', | |
19 | isa => NonEmptySimpleStr, | |
20 | default => sub { 'post' } | |
21 | ); | |
08451970 | 22 | |
23 | has changed => ( | |
24 | is => 'rw', | |
25 | isa => Int, | |
26 | reader => 'is_changed', | |
27 | default => sub{0} | |
b343a983 | 28 | ); |
08451970 | 29 | |
30 | sub can_apply { | |
31 | my ($self) = @_; | |
32 | foreach my $field ( @{ $self->fields } ) { | |
33 | return 0 if $field->needs_sync; | |
34 | # if e.g. a datetime field has an invalid value that can't be re-assembled | |
35 | # into a datetime object, the action may be in a consistent state but | |
36 | # not synchronized from the fields; in this case, we must not apply | |
37 | } | |
38 | return $self->model->can_apply; | |
39 | } | |
40 | ||
41 | sub do_apply { | |
42 | shift->model->do_apply; | |
43 | } | |
44 | ||
45 | after apply_child_events => sub { | |
46 | # interrupt here because fields will have been updated | |
47 | my ($self) = @_; | |
48 | $self->sync_action_from_fields; | |
49 | }; | |
50 | ||
51 | sub sync_action_from_fields { | |
52 | my ($self) = @_; | |
53 | foreach my $field (@{$self->fields}) { | |
54 | $field->sync_to_action; # get the field to populate the $action if possible | |
55 | } | |
56 | $self->model->sync_all; | |
57 | foreach my $field (@{$self->fields}) { | |
58 | $field->sync_from_action; # get errors from $action if applicable | |
59 | } | |
60 | } | |
61 | ||
81393881 | 62 | |
63 | __PACKAGE__->meta->make_immutable; | |
64 | ||
114916fc | 65 | 1; |
ddccc6a2 | 66 | |
114916fc | 67 | __END__; |
ddccc6a2 | 68 | |
08451970 | 69 | =head1 NAME |
70 | ||
b343a983 | 71 | Reaction::UI::ViewPort::Action |
08451970 | 72 | |
73 | =head1 SYNOPSIS | |
74 | ||
08451970 | 75 | =head1 DESCRIPTION |
76 | ||
599c1172 | 77 | This subclass of L<Reaction::UI::ViewPort::Object::Mutable> is used for |
78 | rendering a complete form supporting Apply, Close and OK. | |
08451970 | 79 | |
80 | =head1 ATTRIBUTES | |
81 | ||
599c1172 | 82 | =head2 method |
08451970 | 83 | |
599c1172 | 84 | post / get |
08451970 | 85 | |
86 | =head2 changed | |
87 | ||
88 | Returns true if a field has been edited. | |
89 | ||
08451970 | 90 | =head1 METHODS |
91 | ||
599c1172 | 92 | =head2 can_apply |
08451970 | 93 | |
599c1172 | 94 | =head2 do_apply |
08451970 | 95 | |
599c1172 | 96 | =head2 sync_action_from_fields |
08451970 | 97 | |
599c1172 | 98 | =head1 SEE ALSO |
08451970 | 99 | |
599c1172 | 100 | L<Reaction::UI::ViewPort> |
08451970 | 101 | |
599c1172 | 102 | L<Reaction::UI::ViewPort::Object> |
08451970 | 103 | |
599c1172 | 104 | L<Reaction::UI::ViewPort::Object::Mutable> |
08451970 | 105 | |
599c1172 | 106 | L<Reaction::InterfaceModel::Action::Role::Apply> |
08451970 | 107 | |
599c1172 | 108 | L<Reaction::InterfaceModel::Action::Role::Close> |
08451970 | 109 | |
599c1172 | 110 | L<Reaction::InterfaceModel::Action::Role::OK> |
08451970 | 111 | |
112 | =head1 AUTHORS | |
113 | ||
114 | See L<Reaction::Class> for authors. | |
115 | ||
116 | =head1 LICENSE | |
117 | ||
118 | See L<Reaction::Class> for the license. | |
119 | ||
120 | =cut | |
121 |