Skip to content

Commit

Permalink
fix bug in attempt to allow result source class methods for labels
Browse files Browse the repository at this point in the history
  • Loading branch information
gshank committed Jun 8, 2013
1 parent b5f0824 commit ddc0c72
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 5 deletions.
5 changes: 4 additions & 1 deletion lib/HTML/FormHandler/TraitFor/Model/DBIC.pm
Expand Up @@ -370,10 +370,13 @@ sub lookup_options {
}
return unless $source;

if( $accessor eq 'authors' ) {
$DB::single=1;
}
my $label_column = $field->label_column;
return
unless ( $source->has_column($label_column) ||
$source->can($label_column) );
$source->result_class->can($label_column) );

my $active_col = $self->active_column || $field->active_column;
$active_col = '' unless $source->has_column($active_col);
Expand Down
13 changes: 11 additions & 2 deletions t/book.t
Expand Up @@ -17,6 +17,13 @@ my $form = BookDB::Form::Book->new;

ok( !$form->process( item => $item ), 'Empty data' );

# check authors options
my $author_options = $form->field('authors')->options;
is( $author_options->[0]->{label}, 'J.K. Rowling', 'right author name');

my $borrower_options = $form->field('borrower')->options;
is( $borrower_options->[1]->{label}, 'John Doe <john@gmail.com>', 'right borrower name');

# This is munging up the equivalent of param data from a form
my $good = {
'title' => 'How to Test Perl Form Processors',
Expand All @@ -27,6 +34,7 @@ my $good = {
'publisher' => 'EreWhon Publishing',
'user_updated' => 1,
'comment' => 'this is a comment',
'borrower' => undef,
};

ok( $form->process( item => $item, params => $good ), 'Good data' );
Expand All @@ -38,7 +46,7 @@ ok ($book, 'get book object from form');

is( $book->extra, 'this is a comment', 'comment exists' );
is_deeply( $form->values, $good, 'values correct' );
$good->{$_} = '' for qw/ year pages/;
$good->{$_} = '' for qw/ year pages borrower/;
is_deeply( $form->fif, $good, 'fif correct' );

my $num_genres = $book->genres->count;
Expand All @@ -62,7 +70,8 @@ is( $form->field('publisher')->value, 'EreWhon Publishing', 'right publisher');
my $value_hash = { %{$good},
authors => [],
year => undef,
pages => undef
pages => undef,
borrower => undef,
};
delete $value_hash->{submit};
is_deeply( $form->values, $value_hash, 'get right values from form');
Expand Down
3 changes: 2 additions & 1 deletion t/fif.t
Expand Up @@ -40,6 +40,7 @@ is_deeply( $fif, {
format => '',
year => '',
user_updated => 0,
borrower => '',
}, 'get form fif' );

$fif->{pages} = '501';
Expand Down Expand Up @@ -81,7 +82,7 @@ is( $form->field('pages')->fif, 699, 'get field fif after validation' );

is( $form->field('isbn')->fif, '02340234', 'get field author after validation' );

$params->{$_} = '' for qw/ comment format year /;
$params->{$_} = '' for qw/ comment format year borrower /;
$params->{user_updated} = 0;
is_deeply( $form->fif, $params, 'get form fif after validation' );

Expand Down
6 changes: 6 additions & 0 deletions t/lib/BookDB/Form/Book.pm
Expand Up @@ -31,6 +31,7 @@ has_field 'title' => (
has_field 'authors' => (
type => 'Multiple',
label => 'Authors',
label_column => 'full_name',
order => '2',
);

Expand Down Expand Up @@ -78,6 +79,11 @@ has_field 'comment' => (
order => 9,
);

has_field 'borrower' => (
type => 'Select',
label_column => 'name_email',
);

has_field submit => ( type => 'Submit', value => 'Update' );

sub validate_year {
Expand Down
2 changes: 1 addition & 1 deletion t/lib/BookDB/Schema/Result/Author.pm
Expand Up @@ -40,7 +40,7 @@ __PACKAGE__->many_to_many(
'books' => 'author_books', 'book'
);

sub name {
sub full_name {
my $self = shift;
return $self->first_name . " " . $self->last_name;
}
Expand Down
5 changes: 5 additions & 0 deletions t/lib/BookDB/Schema/Result/Borrower.pm
Expand Up @@ -28,5 +28,10 @@ __PACKAGE__->has_many(
{ "foreign.borrower" => "self.id" },
);

sub name_email {
my $self = shift;
return $self->name . " <" . $self->email . ">";
}


1;

0 comments on commit ddc0c72

Please sign in to comment.