From: Moritz Bunkus Date: Thu, 6 Jun 2013 09:05:15 +0000 (+0200) Subject: L./P.select_tag: 'default' auf Liste von Rose::DB::Object-Instanzen setzen können X-Git-Tag: release-3.1.0beta1~357 X-Git-Url: http://wagnertech.de/git?a=commitdiff_plain;h=cb3431bcc4eae2bc5a2851cae10f9dfd8f517c4a;p=kivitendo-erp.git L./P.select_tag: 'default' auf Liste von Rose::DB::Object-Instanzen setzen können --- 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.