Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

cleanup select field options_method building

  • Loading branch information...
commit 236f4af73bde46fbb7de3ec3ef714713f2ca4638 1 parent a50e5ae
@gshank authored
Showing with 32 additions and 42 deletions.
  1. +23 −42 lib/HTML/FormHandler/Field/Select.pm
  2. +9 −0 t/memory_cycles.t
View
65 lib/HTML/FormHandler/Field/Select.pm
@@ -332,7 +332,7 @@ has 'do_not_reload' => ( isa => 'Bool', is => 'ro' );
sub BUILD {
my $self = shift;
- $self->options_method;
+ $self->build_options_method;
if( $self->options && $self->has_options ) {
$self->options_from('build');
$self->default_from_options([$self->options]);
@@ -341,14 +341,30 @@ sub BUILD {
}
has 'options_method' => (
- traits => ['Code'],
- is => 'rw',
- isa => 'CodeRef',
+ traits => ['Code'],
+ is => 'ro',
+ isa => 'CodeRef',
+ writer => '_set_options_method',
predicate => 'has_options_method',
- handles => {
- get_options => 'execute_method',
- },
+ handles => { 'get_options' => 'execute_method' },
);
+
+sub build_options_method {
+ my $self = shift;
+
+ my $set_options = $self->set_options;
+ $set_options ||= "options_" . HTML::FormHandler::Field::convert_full_name($self->full_name);
+ if ( $self->form && $self->form->can($set_options) ) {
+ my $attr = $self->form->meta->find_method_by_name( $set_options );
+ if ( $attr and $attr->isa('Moose::Meta::Method::Accessor') ) {
+ $self->_set_options_method( sub { my $self = shift; $self->form->$set_options; } );
+ }
+ else {
+ $self->_set_options_method( sub { my $self = shift; $self->form->$set_options($self); } );
+ }
+ }
+}
+
has 'sort_options_method' => (
traits => ['Code'],
is => 'rw',
@@ -360,37 +376,6 @@ has 'sort_options_method' => (
);
has 'set_options' => ( isa => 'Str', is => 'ro');
-sub _set_options_meth {
- my $self = shift;
- return $self->set_options if $self->set_options;
- my $name = $self->full_name;
- if( $name =~ /\./ ) {
- $name =~ s/\.\d+\./_/g;
- $name =~ s/\./_/g;
- }
- return 'options_' . $name;
-}
-sub _can_form_options {
- my $self = shift;
- my $set_options = $self->_set_options_meth;
- return
- unless $self->form &&
- $set_options &&
- $self->form->can( $set_options );
- return $set_options;
-}
-
-sub _form_options {
- my $self = shift;
- return unless (my $meth = $self->_can_form_options);
- my $attr = $self->form->meta->find_method_by_name( $meth );
- if ( $attr and $attr->isa('Moose::Meta::Method::Accessor') ) {
- return $self->form->$meth;
- }
- else {
- return $self->form->$meth($self);
- }
-}
has 'multiple' => ( isa => 'Bool', is => 'rw', default => '0' );
# following is for unusual case where a multiple select is a has_many type relation
@@ -557,10 +542,6 @@ sub _load_options {
@options = $self->get_options;
$self->options_from('method');
}
- elsif ( $self->_can_form_options ) {
- @options = $self->_form_options;
- $self->options_from('method');
- }
elsif ( $self->form ) {
my $full_accessor;
$full_accessor = $self->parent->full_accessor if $self->parent;
View
9 t/memory_cycles.t
@@ -10,6 +10,15 @@ use Test::Memory::Cycle;
has '+name' => ( default => 'testform' );
has_field 'reqname' => ( required => 1 );
+ has_field 'vegetables' => ( type => 'Multiple' );
+ sub options_vegetables {
+ return (
+ 1 => 'lettuce',
+ 2 => 'broccoli',
+ 3 => 'carrots',
+ 4 => 'peas',
+ );
+ }
has_field 'entries' => (
type => 'Repeatable',
required => 1,
Please sign in to comment.
Something went wrong with that request. Please try again.