[Jifty-commit] r1304 - in jifty/trunk: lib/Jifty/Action

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Thu Jun 15 15:16:11 EDT 2006


Author: alexmv
Date: Thu Jun 15 15:16:11 2006
New Revision: 1304

Modified:
   jifty/trunk/   (props changed)
   jifty/trunk/lib/Jifty/Action/Record.pm

Log:
 r13961 at zoq-fot-pik:  chmrr | 2006-06-14 22:19:39 -0400
  * Perltidy


Modified: jifty/trunk/lib/Jifty/Action/Record.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Action/Record.pm	(original)
+++ jifty/trunk/lib/Jifty/Action/Record.pm	Thu Jun 15 15:16:11 2006
@@ -1,6 +1,7 @@
 use warnings;
 use strict;
 use Date::Manip ();
+
 package Jifty::Action::Record;
 
 =head1 NAME
@@ -43,7 +44,6 @@
     $self->log->fatal("Jifty::Action::Record must be subclassed to be used");
 }
 
-
 =head2 new PARAMHASH
 
 Construct a new C<Jifty::Action::Record> (as mentioned in
@@ -57,35 +57,40 @@
 
 sub new {
     my $class = shift;
-    my %args = (record => undef,
-                @_,
-               );
+    my %args  = (
+        record => undef,
+        @_,
+    );
     my $self = $class->SUPER::new(%args);
 
     my $record_class = $self->record_class;
     Jifty::Util->require($record_class);
 
     # Set up record
-    if (ref $record_class) {
+    if ( ref $record_class ) {
         $self->record($record_class);
-        $self->argument_value($_, $self->record->$_) for @{ $self->record->_primary_keys };
-    } elsif (ref $args{record} and $args{record}->isa($record_class)) {
-        $self->record($args{record});
-        $self->argument_value($_, $self->record->$_) for @{ $self->record->_primary_keys };
+        $self->argument_value( $_, $self->record->$_ )
+            for @{ $self->record->_primary_keys };
+    } elsif ( ref $args{record} and $args{record}->isa($record_class) ) {
+        $self->record( $args{record} );
+        $self->argument_value( $_, $self->record->$_ )
+            for @{ $self->record->_primary_keys };
     } else {
-        # We could leave out the explicit current user, but it'd have a slight negative
-        # performance implications
-        $self->record($record_class->new( current_user => Jifty->web->current_user));
+
+        # We could leave out the explicit current user, but it'd have
+        # a slight negative performance implications
+        $self->record(
+            $record_class->new( current_user => Jifty->web->current_user ) );
         my %given_pks = ();
-        for my $pk (@{ $self->record->_primary_keys }) {
-            $given_pks{$pk} = $self->argument_value($pk) if defined $self->argument_value($pk);
+        for my $pk ( @{ $self->record->_primary_keys } ) {
+            $given_pks{$pk} = $self->argument_value($pk)
+                if defined $self->argument_value($pk);
         }
         $self->record->load_by_primary_keys(%given_pks) if %given_pks;
     }
     return $self;
 }
 
-
 =head2 arguments
 
 Overrides the L<Jifty::Action/arguments> method, to automatically
@@ -100,153 +105,161 @@
 =cut
 
 sub arguments {
-  my $self = shift;
+    my $self = shift;
 
-  unless ( $self->_cached_arguments ) {
-    my $field_info = {};
+    unless ( $self->_cached_arguments ) {
+        my $field_info = {};
 
-    my @fields = $self->possible_fields;
+        my @fields = $self->possible_fields;
 
-    # we use a while here because we may be modifying the fields on the fly.
-    while ( my $field = shift @fields ) {
-      my $info = {};
-      my $column;
-      if ( ref $field ) {
-        $column = $field;
-        $field  = $column->name;
-      }
-      else {
-        $column = $self->record->column($field);
-        my $current_value = $self->record->$field;
-
-# If the current value is actually a pointer to another object, dereference it
-        $current_value = $current_value->id
-          if ref($current_value)
-          and $current_value->isa('Jifty::Record');
-        $info->{default_value} = $current_value if $self->record->id;
-      }
-
-      if ( defined $column->valid_values && $column->valid_values ) {
-        $info->{valid_values} = [ @{ $column->valid_values } ];
-        $info->{render_as}    = 'Select';
-      }
-      elsif ( defined $column->type && $column->type =~ /^bool/i ) {
-        $info->{render_as} = 'Checkbox';
-      }
-      elsif ( defined $column->render_as
-        && $column->render_as =~ /^password$/i )
-      {
-        my $same = sub {
-          my ( $self, $value ) = @_;
-          if ( $value ne $self->argument_value($field) ) {
-            return $self->validation_error( $field
-                . '_confirm' =>
-                "The passwords you typed didn't match each other." );
-          }
-          else {
-            return $self->validation_ok( $field . '_confirm' );
-          }
-        };
-
-        $field_info->{ $field . "_confirm" } = {
-          render_as => 'Password',
-          validator => $same,
-          mandatory => 0
-        };
-      }
-
-      elsif ( defined $column->refers_to ) {
-        my $refers_to = $column->refers_to;
-        if ( UNIVERSAL::isa($refers_to, 'Jifty::Record') ) {
-
-          my $collection = Jifty::Collection->new(
-            record_class => $refers_to,
-            current_user => $self->record->current_user
-          );
-          $collection->unlimit;
-
-          # XXX This assumes a ->name and a ->id method
-          $info->{valid_values} = [
-            { display_from => $refers_to->can('name') ? "name" : "id",
-              value_from   => 'id',
-              collection   => $collection
+        # we use a while here because we may be modifying the fields
+        # on the fly.
+        while ( my $field = shift @fields ) {
+            my $info = {};
+            my $column;
+            if ( ref $field ) {
+                $column = $field;
+                $field  = $column->name;
+            } else {
+                $column = $self->record->column($field);
+                my $current_value = $self->record->$field;
+
+                # If the current value is actually a pointer to
+                # another object, dereference it
+                $current_value = $current_value->id
+                    if ref($current_value)
+                    and $current_value->isa('Jifty::Record');
+                $info->{default_value} = $current_value if $self->record->id;
             }
-          ];
 
-          $info->{render_as} = 'Select';
-        }
-      }
+            if ( defined $column->valid_values && $column->valid_values ) {
+                $info->{valid_values} = [ @{ $column->valid_values } ];
+                $info->{render_as}    = 'Select';
+            } elsif ( defined $column->type && $column->type =~ /^bool/i ) {
+                $info->{render_as} = 'Checkbox';
+            } elsif ( defined $column->render_as
+                && $column->render_as =~ /^password$/i )
+            {
+                my $same = sub {
+                    my ( $self, $value ) = @_;
+                    if ( $value ne $self->argument_value($field) ) {
+                        return $self->validation_error( $field
+                                . '_confirm' =>
+                                "The passwords you typed didn't match each other."
+                        );
+                    } else {
+                        return $self->validation_ok( $field . '_confirm' );
+                    }
+                };
+
+                $field_info->{ $field . "_confirm" } = {
+                    render_as => 'Password',
+                    validator => $same,
+                    mandatory => 0
+                };
+            }
 
-      # build up a validator sub if the column implements validation
-      # and we're not overriding it at the action level
-      my $validate_method = "validate_" . $field;
-
-      if ( ($column->validator ||  $self->record->can($validate_method)) and not $self->can($validate_method)) {
-        $info->{ajax_validates} = 1;
-        $info->{validator} = sub {
-          my $self  = shift;
-          my $value = shift;
-          my ( $is_valid, $message );
-      	if ( $self->record->can($validate_method) ) {
-          ($is_valid, $message) =  $self->record->$validate_method($value);
-	 } else {
-          ( $is_valid, $message ) = &{ $column->validator }( $self->record, $value );
-	}
-          if ($is_valid) {
-            return $self->validation_ok($field);
-          }
-          else {
-            unless ($message) {
-              $self->log->error(
-                qq{Schema validator for $field didn't explain why the value '$value' is invalid}
-              );
+            elsif ( defined $column->refers_to ) {
+                my $refers_to = $column->refers_to;
+                if ( UNIVERSAL::isa( $refers_to, 'Jifty::Record' ) ) {
+
+                    my $collection = Jifty::Collection->new(
+                        record_class => $refers_to,
+                        current_user => $self->record->current_user
+                    );
+                    $collection->unlimit;
+
+                    # XXX This assumes a ->name and a ->id method
+                    $info->{valid_values} = [
+                        {   display_from => $refers_to->can('name')
+                            ? "name"
+                            : "id",
+                            value_from => 'id',
+                            collection => $collection
+                        }
+                    ];
+
+                    $info->{render_as} = 'Select';
+                }
             }
-            return (
-              $self->validation_error(
-                $field => $message
-                  || q{That doesn't look right, but I don't know why}
-              )
-            );
-          }
-        };
-      }
-      my $autocomplete_method = "autocomplete_" . $field;
-      if ( $self->record->can($autocomplete_method) ) {
-        $info->{'autocompleter'} ||= sub {
-          my ( $self, $value ) = @_;
-          my %columns;
-          $columns{$_} = $self->argument_value($_) for grep {$_ ne $field} $self->possible_fields;
-          return $self->record->$autocomplete_method($value, %columns);
-        };
-      }
-
-      my $canonicalize_method = "canonicalize_" . $field;
-      if ( $self->record->can($canonicalize_method) ) {
-        $info->{'ajax_canonicalizes'} = 1;
-        $info->{'canonicalizer'} ||= sub {
-          my ( $self, $value ) = @_;
-          return $self->record->$canonicalize_method($value);
-        };
-      }
-      elsif ( defined $column->render_as
-        and $column->render_as eq "Date" )
-      {
-        $info->{'ajax_canonicalizes'} = 1;
-      }
 
-      # If we're hand-coding a render_as, hints or label, let's use it.
-      for (qw(render_as label hints length mandatory sort_order)) {
+            # build up a validator sub if the column implements validation
+            # and we're not overriding it at the action level
+            my $validate_method = "validate_" . $field;
+
+            if ((   $column->validator || $self->record->can($validate_method)
+                )
+                and not $self->can($validate_method)
+                )
+            {
+                $info->{ajax_validates} = 1;
+                $info->{validator} = sub {
+                    my $self  = shift;
+                    my $value = shift;
+                    my ( $is_valid, $message );
+                    if ( $self->record->can($validate_method) ) {
+                        ( $is_valid, $message )
+                            = $self->record->$validate_method($value);
+                    } else {
+                        ( $is_valid, $message )
+                            = &{ $column->validator }( $self->record,
+                            $value );
+                    }
+                    if ($is_valid) {
+                        return $self->validation_ok($field);
+                    } else {
+                        unless ($message) {
+                            $self->log->error(
+                                qq{Schema validator for $field didn't explain why the value '$value' is invalid}
+                            );
+                        }
+                        return (
+                            $self->validation_error(
+                                $field => $message
+                                    || q{That doesn't look right, but I don't know why}
+                            )
+                        );
+                    }
+                };
+            }
+            my $autocomplete_method = "autocomplete_" . $field;
+            if ( $self->record->can($autocomplete_method) ) {
+                $info->{'autocompleter'} ||= sub {
+                    my ( $self, $value ) = @_;
+                    my %columns;
+                    $columns{$_} = $self->argument_value($_)
+                        for grep { $_ ne $field } $self->possible_fields;
+                    return $self->record->$autocomplete_method( $value,
+                        %columns );
+                };
+            }
+
+            my $canonicalize_method = "canonicalize_" . $field;
+            if ( $self->record->can($canonicalize_method) ) {
+                $info->{'ajax_canonicalizes'} = 1;
+                $info->{'canonicalizer'} ||= sub {
+                    my ( $self, $value ) = @_;
+                    return $self->record->$canonicalize_method($value);
+                };
+            } elsif ( defined $column->render_as
+                and $column->render_as eq "Date" )
+            {
+                $info->{'ajax_canonicalizes'} = 1;
+            }
+
+            # If we're hand-coding a render_as, hints or label, let's use it.
+            for (qw(render_as label hints length mandatory sort_order)) {
 
-        if ( defined $column->$_ ) {
-          $info->{$_} = $column->$_;
+                if ( defined $column->$_ ) {
+                    $info->{$_} = $column->$_;
+                }
+            }
+            $field_info->{$field} = $info;
         }
-      }
-      $field_info->{$field} = $info;
-    }
 
-    $self->_cached_arguments($field_info);
-  }
-  return $self->_cached_arguments();
+        $self->_cached_arguments($field_info);
+    }
+    return $self->_cached_arguments();
 }
 
 =head2 possible_fields
@@ -270,10 +283,11 @@
 
 sub take_action {
     my $self = shift;
-    $self->log->fatal("Use one of the Jifty::Action::Record subclasses, ::Create, ::Update or ::Delete");
+    $self->log->fatal(
+        "Use one of the Jifty::Action::Record subclasses, ::Create, ::Update or ::Delete"
+    );
 }
 
-
 =head1 SEE ALSO
 
 L<Jifty::Action>, L<Jifty::Record>, L<Jifty::DBI::Record>,


More information about the Jifty-commit mailing list