we dont need accept context anymore
[catagits/Reaction.git] / lib / Reaction / UI / ViewPort / Action.pm
CommitLineData
ddccc6a2 1package Reaction::UI::ViewPort::Action;
2
3use Reaction::Class;
4
08451970 5use MooseX::Types::Moose qw/Int/;
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
b343a983 13#this has to fucking go. it BLOWS.
14has method => (
15 is => 'rw',
16 isa => NonEmptySimpleStr,
17 default => sub { 'post' }
18);
08451970 19
20has changed => (
21 is => 'rw',
22 isa => Int,
23 reader => 'is_changed',
24 default => sub{0}
b343a983 25);
08451970 26
27sub 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
38sub do_apply {
39 shift->model->do_apply;
40}
41
42after apply_child_events => sub {
43 # interrupt here because fields will have been updated
44 my ($self) = @_;
45 $self->sync_action_from_fields;
46};
47
48sub 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 621;
ddccc6a2 63
114916fc 64__END__;
ddccc6a2 65
08451970 66=head1 NAME
67
b343a983 68Reaction::UI::ViewPort::Action
08451970 69
70=head1 SYNOPSIS
71
08451970 72=head1 DESCRIPTION
73
599c1172 74This subclass of L<Reaction::UI::ViewPort::Object::Mutable> is used for
75rendering a complete form supporting Apply, Close and OK.
08451970 76
77=head1 ATTRIBUTES
78
599c1172 79=head2 method
08451970 80
599c1172 81post / get
08451970 82
83=head2 changed
84
85Returns 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 97L<Reaction::UI::ViewPort>
08451970 98
599c1172 99L<Reaction::UI::ViewPort::Object>
08451970 100
599c1172 101L<Reaction::UI::ViewPort::Object::Mutable>
08451970 102
599c1172 103L<Reaction::InterfaceModel::Action::Role::Apply>
08451970 104
599c1172 105L<Reaction::InterfaceModel::Action::Role::Close>
08451970 106
599c1172 107L<Reaction::InterfaceModel::Action::Role::OK>
08451970 108
109=head1 AUTHORS
110
111See L<Reaction::Class> for authors.
112
113=head1 LICENSE
114
115See L<Reaction::Class> for the license.
116
117=cut
118