X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FReaction%2FUI%2FViewPort%2FField.pm;h=a4bb219a900b95c522808deee9e43fc18d7a6035;hb=371430b40525607496eb995a6c0976a11ea28b43;hp=41a7c4216cfebbc427cf3b68bd82a7126ecce629;hpb=7adfd53f17f66ffe93763e944ed1d3fc52a369dc;p=catagits%2FReaction.git diff --git a/lib/Reaction/UI/ViewPort/Field.pm b/lib/Reaction/UI/ViewPort/Field.pm index 41a7c42..a4bb219 100644 --- a/lib/Reaction/UI/ViewPort/Field.pm +++ b/lib/Reaction/UI/ViewPort/Field.pm @@ -1,99 +1,66 @@ package Reaction::UI::ViewPort::Field; use Reaction::Class; +use aliased 'Reaction::InterfaceModel::Object'; +use aliased 'Reaction::Meta::InterfaceModel::Object::ParameterAttribute'; -class Field is 'Reaction::UI::ViewPort', which { - - has name => ( - isa => 'Str', is => 'rw', required => 1 - ); - - has action => ( - isa => 'Reaction::InterfaceModel::Action', - is => 'ro', required => 0, predicate => 'has_action', - ); - - has attribute => ( - isa => 'Reaction::Meta::InterfaceModel::Action::ParameterAttribute', - is => 'ro', predicate => 'has_attribute', - ); - - has value => ( - is => 'rw', lazy_build => 1, trigger_adopt('value'), - clearer => 'clear_value', - ); - - has needs_sync => ( - isa => 'Int', is => 'rw', default => 0 - ); - - has label => (isa => 'Str', is => 'rw', lazy_build => 1); - - has message => ( - isa => 'Str', is => 'rw', required => 1, default => sub { '' } - ); - - implements BUILD => as { - my ($self) = @_; - if (!$self->has_attribute != !$self->has_action) { - confess "Should have both action and attribute or neither"; - } - }; - - implements build_label => as { - my ($self) = @_; - return join(' ', map { ucfirst } split('_', $self->name)); - }; - - implements build_value => as { - my ($self) = @_; - if ($self->has_attribute) { - my $reader = $self->attribute->get_read_method; - my $predicate = $self->attribute->predicate; - if (!$predicate || $self->action->$predicate) { - return $self->action->$reader; - } - } - return ''; - }; - - implements adopt_value => as { - my ($self) = @_; - $self->needs_sync(1) if $self->has_attribute; - }; - - implements sync_to_action => as { - my ($self) = @_; - return unless $self->needs_sync && $self->has_attribute && $self->has_value; - my $attr = $self->attribute; - if (my $tc = $attr->type_constraint) { - my $value = $self->value; - if ($tc->has_coercion) { - $value = $tc->coercion->coerce($value); - } - my $error = $tc->validate($self->value); - if (defined $error) { - $self->message($error); - return; - } - } - my $writer = $attr->get_write_method; - confess "No writer for attribute" unless defined($writer); - $self->action->$writer($self->value); - $self->needs_sync(0); - }; - - implements sync_from_action => as { - my ($self) = @_; - return unless !$self->needs_sync && $self->has_attribute; - $self->message($self->action->error_for($self->attribute)||''); - }; - - override accept_events => sub { ('value', super()) }; +use namespace::clean -except => [ qw(meta) ]; +extends 'Reaction::UI::ViewPort'; + + +has value => (is => 'rw', lazy_build => 1); +has name => (is => 'rw', isa => 'Str', lazy_build => 1); +has label => (is => 'rw', isa => 'Str', lazy_build => 1); +has value_string => (is => 'rw', isa => 'Str', lazy_build => 1); + +has model => (is => 'ro', isa => Object, required => 1); +has attribute => (is => 'ro', isa => ParameterAttribute, required => 1); +sub _build_name { shift->attribute->name }; +sub _build_label { + join(' ', map { ucfirst } split('_', shift->name)); +}; +sub _build_value { + my ($self) = @_; + my $reader = $self->attribute->get_read_method; + return $self->model->$reader; +}; +sub _model_has_value { + my ($self) = @_; + my $predicate = $self->attribute->get_predicate_method; + + if (!$predicate || $self->model->$predicate + #|| ($self->attribute->is_lazy + # && !$self->attribute->is_lazy_fail) + ) { + # either model attribute has a value now or can build it + return 1; + } + return 0; +}; +sub _build_value_string { + my ($self) = @_; + # XXX need the defined test because the IM lazy builds from + # the model and DBIC can have nullable fields and DBIC doesn't + # have a way to tell us that doesn't force value inflation (extra + # SELECTs for belongs_to) so basically we're screwed. + return ($self->_model_has_value && defined($self->_build_value) + ? $self->_value_string_from_value + : $self->_empty_string_value); +}; +sub _value_string_from_value { + shift->value; +}; +sub _empty_string_value { '' }; +sub value_is_required { + shift->attribute->is_required; }; +__PACKAGE__->meta->make_immutable; + + 1; +__END__; =head1 NAME @@ -101,59 +68,19 @@ Reaction::UI::ViewPort::Field =head1 DESCRIPTION -This viewport is the base class for all field types. - =head1 ATTRIBUTES -=head2 name - -=head2 action - -L +=head2 model =head2 attribute -L - =head2 value -=head2 needs_sync +=head2 name =head2 label -User friendly label, by default is based on the name. - -=head2 message - -Optional string relating to the field. - -=head1 SEE ALSO - -=head2 L - -=head2 L - -=head2 L - -=head2 L - -=head2 L - -=head2 L - -=head2 L - -=head2 L - -=head2 L - -=head2 L - -=head2 L - -=head2 L - -=head2 L +=head2 value_string =head1 AUTHORS