From cb3431bcc4eae2bc5a2851cae10f9dfd8f517c4a Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Thu, 6 Jun 2013 11:05:15 +0200 Subject: [PATCH] =?utf8?q?L./P.select=5Ftag:=20'default'=20auf=20Liste=20v?= =?utf8?q?on=20Rose::DB::Object-Instanzen=20setzen=20k=C3=B6nnen?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- SL/Presenter/Tag.pm | 52 +++++++++++++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 18 deletions(-) diff --git a/SL/Presenter/Tag.pm b/SL/Presenter/Tag.pm index e5690e79d..2bf78ee04 100644 --- a/SL/Presenter/Tag.pm +++ b/SL/Presenter/Tag.pm @@ -105,18 +105,7 @@ sub select_tag { my $with_optgroups = delete($attributes{with_optgroups}); - my %selected; - - if ( ref($attributes{default}) eq 'ARRAY' ) { - - foreach my $entry (@{$attributes{default}}) { - $selected{$entry} = 1; - } - } elsif ( defined($attributes{default}) ) { - $selected{$attributes{default}} = 1; - } - - delete($attributes{default}); + my $default = delete($attributes{default}); my $normalize_entry = sub { my ($type, $entry, $sub, $key) = @_; @@ -139,6 +128,9 @@ sub select_tag { return undef; }; + my %selected = map { ( $normalize_entry->('value', $_, $default_sub, $default_key) => 1 ) } + (@{ ref($default) eq 'ARRAY' ? $default : [ $default ]}); + my $list_to_code = sub { my ($sub_collection) = @_; @@ -157,11 +149,7 @@ sub select_tag { my $default = $normalize_entry->('default', $entry, $default_sub, $default_key); - push(@options, [$value, $title, $default]); - } - - foreach my $entry (@options) { - $entry->[2] = 1 if $selected{$entry->[0]}; + push(@options, [$value, $title, !!$selected{$value}]); } return join '', map { $self->html_tag('option', $self->escape($_->[1]), value => $_->[0], selected => $_->[2]) } @options; @@ -218,6 +206,12 @@ Usage from a template: { direction => 'right', display => 'To the right', selected => 1 } ], value_key => 'direction', title_key => 'display')) %] + # With Rose::DB::Object instances. For example a group membership + # (SL::DB::AuthGroup) for a user (SL::DB::AuthUser) via the user's + # "groups" relationship: + [% P.select_tag('direction', SELF.all_groups, default=SELF.user.groups, + title_key='name', default_key='id', multiple=1) %] + =head1 DESCRIPTION A module modeled a bit after Rails' ActionView helpers. Several small @@ -330,7 +324,29 @@ an empty string. The option C can be either a scalar or an array reference containing the values of the options which should be set to be -selected. +selected. How the value from the elements is derived depends on three +things: the parameters C (unset by default) and +C (default: "selected") as well as the element's type. + +=over 4 + +=item * If C is a code reference then that reference is +called with the element as its only parameter. The return value is the +value compared to the value from the elements in C<\@collection>. + +=item * If the element in question is a scalar then its value is used. + +=item * If the element is a hash then C names the index +into the hash used as the value to select by default. + +=item * If the element is a blessed object then C names +the method to call on the object. That method's return value is used +as the value to select by default. + +=back + +See the synopsis for an example using C with Rose::DB::Object +instances. The tag's C defaults to C. -- 2.20.1