fixed bool field
[catagits/Reaction.git] / lib / Reaction / UI / ViewPort / Action.pm
CommitLineData
ddccc6a2 1package Reaction::UI::ViewPort::Action;
2
3use Reaction::Class;
4
e29819c4 5use MooseX::Types::Moose qw/Int Str/;
08451970 6use Reaction::Types::Core qw/NonEmptySimpleStr/;
7
8use namespace::clean -except => [ qw(meta) ];
b343a983 9
10extends 'Reaction::UI::ViewPort::Object::Mutable';
08451970 11with 'Reaction::UI::ViewPort::Action::Role::OK';
12
e29819c4 13has message => (is => 'rw', isa => Str);
eb52e595 14has '+model' => (handles => [qw/error_message has_error_message/]);
e29819c4 15
b343a983 16#this has to fucking go. it BLOWS.
17has method => (
18 is => 'rw',
19 isa => NonEmptySimpleStr,
20 default => sub { 'post' }
21);
08451970 22
23has changed => (
24 is => 'rw',
25 isa => Int,
26 reader => 'is_changed',
27 default => sub{0}
b343a983 28);
08451970 29
30sub 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
41sub do_apply {
42 shift->model->do_apply;
43}
44
45after apply_child_events => sub {
46 # interrupt here because fields will have been updated
47 my ($self) = @_;
48 $self->sync_action_from_fields;
49};
50
51sub 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 651;
ddccc6a2 66
114916fc 67__END__;
ddccc6a2 68
08451970 69=head1 NAME
70
b343a983 71Reaction::UI::ViewPort::Action
08451970 72
73=head1 SYNOPSIS
74
08451970 75=head1 DESCRIPTION
76
599c1172 77This subclass of L<Reaction::UI::ViewPort::Object::Mutable> is used for
78rendering a complete form supporting Apply, Close and OK.
08451970 79
80=head1 ATTRIBUTES
81
599c1172 82=head2 method
08451970 83
599c1172 84post / get
08451970 85
86=head2 changed
87
88Returns 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 100L<Reaction::UI::ViewPort>
08451970 101
599c1172 102L<Reaction::UI::ViewPort::Object>
08451970 103
599c1172 104L<Reaction::UI::ViewPort::Object::Mutable>
08451970 105
599c1172 106L<Reaction::InterfaceModel::Action::Role::Apply>
08451970 107
599c1172 108L<Reaction::InterfaceModel::Action::Role::Close>
08451970 109
599c1172 110L<Reaction::InterfaceModel::Action::Role::OK>
08451970 111
112=head1 AUTHORS
113
114See L<Reaction::Class> for authors.
115
116=head1 LICENSE
117
118See L<Reaction::Class> for the license.
119
120=cut
121