Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision cb3431bc

Von Moritz Bunkus vor mehr als 11 Jahren hinzugefügt

  • ID cb3431bcc4eae2bc5a2851cae10f9dfd8f517c4a
  • Vorgänger f69dc608
  • Nachfolger f9937164

L./P.select_tag: 'default' auf Liste von Rose::DB::Object-Instanzen setzen können

Unterschiede anzeigen:

SL/Presenter/Tag.pm
105 105

  
106 106
  my $with_optgroups  = delete($attributes{with_optgroups});
107 107

  
108
  my %selected;
109

  
110
  if ( ref($attributes{default}) eq 'ARRAY' ) {
111

  
112
    foreach my $entry (@{$attributes{default}}) {
113
      $selected{$entry} = 1;
114
    }
115
  } elsif ( defined($attributes{default}) ) {
116
    $selected{$attributes{default}} = 1;
117
  }
118

  
119
  delete($attributes{default});
108
  my $default         = delete($attributes{default});
120 109

  
121 110
  my $normalize_entry = sub {
122 111
    my ($type, $entry, $sub, $key) = @_;
......
139 128
    return undef;
140 129
  };
141 130

  
131
  my %selected = map { ( $normalize_entry->('value', $_, $default_sub, $default_key) => 1 ) }
132
                     (@{ ref($default) eq 'ARRAY' ? $default : [ $default ]});
133

  
142 134
  my $list_to_code = sub {
143 135
    my ($sub_collection) = @_;
144 136

  
......
157 149

  
158 150
      my $default = $normalize_entry->('default', $entry, $default_sub, $default_key);
159 151

  
160
      push(@options, [$value, $title, $default]);
161
    }
162

  
163
    foreach my $entry (@options) {
164
      $entry->[2] = 1 if $selected{$entry->[0]};
152
      push(@options, [$value, $title, !!$selected{$value}]);
165 153
    }
166 154

  
167 155
    return join '', map { $self->html_tag('option', $self->escape($_->[1]), value => $_->[0], selected => $_->[2]) } @options;
......
218 206
                                 { direction => 'right', display => 'To the right', selected => 1 } ],
219 207
                               value_key => 'direction', title_key => 'display')) %]
220 208

  
209
  # With Rose::DB::Object instances. For example a group membership
210
  # (SL::DB::AuthGroup) for a user (SL::DB::AuthUser) via the user's
211
  # "groups" relationship:
212
  [% P.select_tag('direction', SELF.all_groups, default=SELF.user.groups,
213
                               title_key='name', default_key='id', multiple=1) %]
214

  
221 215
=head1 DESCRIPTION
222 216

  
223 217
A module modeled a bit after Rails' ActionView helpers. Several small
......
330 324

  
331 325
The option C<default> can be either a scalar or an array reference
332 326
containing the values of the options which should be set to be
333
selected.
327
selected. How the value from the elements is derived depends on three
328
things: the parameters C<default_sub> (unset by default) and
329
C<default_key> (default: "selected") as well as the element's type.
330

  
331
=over 4
332

  
333
=item * If C<default_sub> is a code reference then that reference is
334
called with the element as its only parameter. The return value is the
335
value compared to the value from the elements in C<\@collection>.
336

  
337
=item * If the element in question is a scalar then its value is used.
338

  
339
=item * If the element is a hash then C<default_key> names the index
340
into the hash used as the value to select by default.
341

  
342
=item * If the element is a blessed object then C<default_key> names
343
the method to call on the object. That method's return value is used
344
as the value to select by default.
345

  
346
=back
347

  
348
See the synopsis for an example using C<default> with Rose::DB::Object
349
instances.
334 350

  
335 351
The tag's C<id> defaults to C<name_to_id($name)>.
336 352

  

Auch abrufbar als: Unified diff