sync_to_action not required, will trigger off value set
[catagits/Reaction.git] / lib / Reaction / UI / ViewPort / Field.pm
CommitLineData
7adfd53f 1package Reaction::UI::ViewPort::Field;
2
3use Reaction::Class;
ddccc6a2 4use aliased 'Reaction::InterfaceModel::Object';
5use aliased 'Reaction::Meta::InterfaceModel::Object::ParameterAttribute';
7adfd53f 6
7class Field is 'Reaction::UI::ViewPort', which {
8
da57cbb2 9 has value => (is => 'rw', lazy_build => 1);
ddccc6a2 10 has name => (is => 'rw', isa => 'Str', lazy_build => 1);
11 has label => (is => 'rw', isa => 'Str', lazy_build => 1);
12 has value_string => (is => 'rw', isa => 'Str', lazy_build => 1);
7adfd53f 13
ddccc6a2 14 has model => (is => 'ro', isa => Object, required => 1);
15 has attribute => (is => 'ro', isa => ParameterAttribute, required => 1);
7adfd53f 16
ddccc6a2 17 implements adopt_value => as {};
7adfd53f 18
ddccc6a2 19 implements _build_name => as { shift->attribute->name };
7adfd53f 20
89939ff9 21 implements _build_label => as {
ddccc6a2 22 join(' ', map { ucfirst } split('_', shift->name));
7adfd53f 23 };
24
89939ff9 25 implements _build_value => as {
7adfd53f 26 my ($self) = @_;
ddccc6a2 27 my $reader = $self->attribute->get_read_method;
656d19e9 28 return $self->model->$reader;
29 };
30
31 implements _model_has_value => as {
32 my ($self) = @_;
ddccc6a2 33 my $predicate = $self->attribute->predicate;
c03f75a7 34
398c8477 35 if (!$predicate || $self->model->$predicate
80bb08dc 36 || ($self->attribute->is_lazy
cbbaa612 37 && !$self->attribute->is_lazy_fail)
80bb08dc 38 ) {
656d19e9 39 # either model attribute has a value now or can build it
40 return 1;
398c8477 41 }
656d19e9 42 return 0;
43 };
44
45 implements _build_value_string => as {
46 my ($self) = @_;
47 # XXX need the defined test because the IM lazy builds from
48 # the model and DBIC can have nullable fields and DBIC doesn't
49 # have a way to tell us that doesn't force value inflation (extra
50 # SELECTs for belongs_to) so basically we're screwed.
51 return ($self->_model_has_value && defined($self->value)
52 ? $self->_value_string_from_value
53 : $self->_empty_string_value);
54 };
55
56 implements _value_string_from_value => as {
57 shift->value;
7adfd53f 58 };
59
656d19e9 60 implements _empty_string_value => as { '' };
398c8477 61
7adfd53f 62};
63
641;
2dba7201 65__END__;
66
67=head1 NAME
68
69Reaction::UI::ViewPort::Field
70
71=head1 DESCRIPTION
72
73=head1 ATTRIBUTES
74
75=head2 model
76
77=head2 attribute
78
79=head2 value
80
81=head2 name
82
83=head2 label
84
85=head2 value_string
86
87=head1 AUTHORS
88
89See L<Reaction::Class> for authors.
90
91=head1 LICENSE
92
93See L<Reaction::Class> for the license.
94
95=cut