rework skin path handling
[catagits/Reaction.git] / lib / Reaction / UI / ViewPort / Field.pm
index c71ef88..09857ff 100644 (file)
@@ -6,7 +6,7 @@ use aliased 'Reaction::Meta::InterfaceModel::Object::ParameterAttribute';
 
 class Field is 'Reaction::UI::ViewPort', which {
 
-  has value        => (is => 'rw', lazy_fail => 1);
+  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);
@@ -18,34 +18,47 @@ class Field is 'Reaction::UI::ViewPort', which {
 
   implements _build_name => as { shift->attribute->name };
 
-  implements _build_value_string => as {
-    my($self) = @_;
-    return $self->has_value? $self->value : '';
-  };
-
   implements _build_label => as {
     join(' ', map { ucfirst } split('_', shift->name));
   };
 
-  implements BUILD => as {
-    my($self) = @_;
+  implements _build_value => as {
+    my ($self) = @_;
     my $reader = $self->attribute->get_read_method;
+    return $self->model->$reader;
+  };
+
+  implements _model_has_value => as {
+    my ($self) = @_;
     my $predicate = $self->attribute->predicate;
 
     if (!$predicate || $self->model->$predicate
         || ($self->attribute->is_lazy
             && !$self->attribute->is_lazy_fail)
       ) {
-      my $value = $self->model->$reader;
-      if ( $self->attribute->is_required ) {
-        $self->value($value) if defined $value;
-      }
-      else {
-        $self->value($value);
-      }
+      # either model attribute has a value now or can build it
+      return 1;
     }
+    return 0;
   };
 
+  implements _build_value_string => as {
+    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->value)
+              ? $self->_value_string_from_value
+              : $self->_empty_string_value);
+  };
+
+  implements _value_string_from_value => as {
+    shift->value;
+  };
+
+  implements _empty_string_value => as { '' };
+
 };
 
 1;