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