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