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