[Jifty-commit] r5210 - in jifty/trunk/lib/Jifty: .

Jifty commits jifty-commit at lists.jifty.org
Tue Mar 11 05:20:01 EDT 2008


Author: clkao
Date: Tue Mar 11 05:19:54 2008
New Revision: 5210

Modified:
   jifty/trunk/lib/Jifty/Action.pm
   jifty/trunk/lib/Jifty/Web/Form/Field.pm

Log:
- When form_field() are called with render_as in cached mode,
  Make sure it requires the widget class.

- Refactor and put all the Form::Field blessing logic into
  one place.


Modified: jifty/trunk/lib/Jifty/Action.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Action.pm	(original)
+++ jifty/trunk/lib/Jifty/Action.pm	Tue Mar 11 05:19:54 2008
@@ -520,13 +520,10 @@
             Jifty->log->warn("$arg_name isn't a valid field for $self");
         }
     } 
-    
     # It has been cached, but render_as is explicitly set
-    elsif ( $args{render_as} ) {
+    elsif ( my $widget = $args{render_as} ) {
+        $self->{_private_form_fields_hash}{$arg_name}->rebless( $widget );
 
-        # Rebless the form control as something else
-        bless $self->{_private_form_fields_hash}{$arg_name},
-          "Jifty::Web::Form::Field::".ucfirst($args{render_as});
     }
 
     return $self->{_private_form_fields_hash}{$arg_name};

Modified: jifty/trunk/lib/Jifty/Web/Form/Field.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Web/Form/Field.pm	(original)
+++ jifty/trunk/lib/Jifty/Web/Form/Field.pm	Tue Mar 11 05:19:54 2008
@@ -70,9 +70,7 @@
         render_mode   => 'update' });
     my $args = ref($_[0]) ? $_[0] : {@_};
 
-    my $subclass = ucfirst($args->{render_as} || $args->{type} || 'text');
-    $subclass = 'Jifty::Web::Form::Field::' . $subclass unless $subclass =~ /::/;
-    bless $self, $subclass if Jifty::Util->require($subclass);
+    $self->rebless( $args->{render_as} || $args->{type} || 'text' );
 
     for my $field ( $self->accessors() ) {
         $self->$field( $args->{$field} ) if exists $args->{$field};
@@ -83,8 +81,7 @@
     # but ignore that if the field is a container in the model
     my ($key, $value) = Jifty::Request::Mapper->query_parameters($self->input_name, $self->current_value);
     if ($key ne $self->input_name && !$self->action->arguments->{$self->name}{container}) {
-        Jifty::Util->require('Jifty::Web::Form::Field::Hidden');
-        bless $self, "Jifty::Web::Form::Field::Hidden";
+        $self->rebless('Hidden');
         $self->input_name($key);
         $self->default_value($value);
         $self->sticky_value(undef);
@@ -98,6 +95,21 @@
     return $self;
 }
 
+=head2 $self->rebless($widget)
+
+Turn the current blessed class into the given widget class.
+
+=cut
+
+sub rebless {
+    my ($self, $widget) = @_;
+    my $widget_class = $widget =~ m/::/ ? $widget : "Jifty::Web::Form::Field::".ucfirst($widget);
+
+    $self->log->error("Invalid widget class $widget_class")
+        unless Jifty::Util->require($widget_class);
+
+    bless $self, $widget_class;
+}
 
 =head2 accessors
 


More information about the Jifty-commit mailing list