cleanup of hard to read layout_args code
[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
7b5e71ad 7use MooseX::Types::Moose qw/Str/;
8
81393881 9use namespace::clean -except => [ qw(meta) ];
10extends 'Reaction::UI::ViewPort';
f25cb331 11
81393881 12has value => (is => 'rw', lazy_build => 1);
7b5e71ad 13has name => (is => 'rw', isa => Str, lazy_build => 1);
14has label => (is => 'rw', isa => Str, lazy_build => 1);
15has value_string => (is => 'rw', isa => Str, lazy_build => 1);
81393881 16
17has model => (is => 'ro', isa => Object, required => 1);
18has attribute => (is => 'ro', isa => ParameterAttribute, required => 1);
599c1172 19
81393881 20sub _build_name { shift->attribute->name };
599c1172 21
81393881 22sub _build_label {
23 join(' ', map { ucfirst } split('_', shift->name));
599c1172 24}
25
81393881 26sub _build_value {
27 my ($self) = @_;
28 my $reader = $self->attribute->get_read_method;
29 return $self->model->$reader;
599c1172 30}
31
81393881 32sub _model_has_value {
33 my ($self) = @_;
34 my $predicate = $self->attribute->get_predicate_method;
35
36 if (!$predicate || $self->model->$predicate
fbb9dacb 37 # || ($self->attribute->is_lazy
38 # && !$self->attribute->is_lazy_fail)
81393881 39 ) {
588a35ac 40 # edenc -- uncommented the lazy checks above
41 # model->$predicate returns false if the value isn't set
42 # but has a lazy builder
43
81393881 44 # either model attribute has a value now or can build it
45 return 1;
46 }
47 return 0;
599c1172 48}
49
81393881 50sub _build_value_string {
51 my ($self) = @_;
52 # XXX need the defined test because the IM lazy builds from
53 # the model and DBIC can have nullable fields and DBIC doesn't
54 # have a way to tell us that doesn't force value inflation (extra
55 # SELECTs for belongs_to) so basically we're screwed.
56 return ($self->_model_has_value && defined($self->_build_value)
57 ? $self->_value_string_from_value
58 : $self->_empty_string_value);
599c1172 59}
60
81393881 61sub _value_string_from_value {
62 shift->value;
599c1172 63}
64
65sub _empty_string_value { '' }
66
81393881 67sub value_is_required {
2a50048e 68 my $self = shift;
69 $self->model->attribute_is_required($self->attribute);
599c1172 70}
81393881 71
72__PACKAGE__->meta->make_immutable;
73
7adfd53f 74
751;
2dba7201 76__END__;
77
78=head1 NAME
79
80Reaction::UI::ViewPort::Field
81
82=head1 DESCRIPTION
83
84=head1 ATTRIBUTES
85
86=head2 model
87
88=head2 attribute
89
90=head2 value
91
92=head2 name
93
94=head2 label
95
96=head2 value_string
97
98=head1 AUTHORS
99
100See L<Reaction::Class> for authors.
101
102=head1 LICENSE
103
104See L<Reaction::Class> for the license.
105
106=cut