memory leaks on CRUD fixed
[catagits/Reaction.git] / lib / Reaction / UI / ViewPort / Action.pm
1 package Reaction::UI::ViewPort::Action;
2
3 use Reaction::Class;
4
5 use MooseX::Types::Moose qw/Int/;
6 use Reaction::Types::Core qw/NonEmptySimpleStr/;
7
8 use namespace::clean -except => [ qw(meta) ];
9
10 extends 'Reaction::UI::ViewPort::Object::Mutable';
11 with 'Reaction::UI::ViewPort::Action::Role::OK';
12
13 #this has to fucking go. it BLOWS.
14 has method => (
15   is => 'rw',
16   isa => NonEmptySimpleStr,
17   default => sub { 'post' }
18 );
19
20 has changed => (
21   is => 'rw',
22   isa => Int,
23   reader => 'is_changed',
24   default => sub{0}
25 );
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
59
60 __PACKAGE__->meta->make_immutable;
61
62 1;
63
64 __END__;
65
66 =head1 NAME
67
68 Reaction::UI::ViewPort::Action
69
70 =head1 SYNOPSIS
71
72 =head1 DESCRIPTION
73
74 This subclass of L<Reaction::UI::ViewPort::Object::Mutable> is used for 
75 rendering a complete form supporting Apply, Close and OK.
76
77 =head1 ATTRIBUTES
78
79 =head2 method
80
81 post / get
82
83 =head2 changed
84
85 Returns true if a field has been edited.
86
87 =head1 METHODS
88
89 =head2 can_apply
90
91 =head2 do_apply
92
93 =head2 sync_action_from_fields
94
95 =head1 SEE ALSO
96
97 L<Reaction::UI::ViewPort>
98
99 L<Reaction::UI::ViewPort::Object>
100
101 L<Reaction::UI::ViewPort::Object::Mutable>
102
103 L<Reaction::InterfaceModel::Action::Role::Apply>
104
105 L<Reaction::InterfaceModel::Action::Role::Close>
106
107 L<Reaction::InterfaceModel::Action::Role::OK>
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