Subject: | Use acceptable_values for list view too |
I noticed that acceptable_values can be used to specify field names too.
I don't know if it's intentional, but it /is/ really usefully.
For example, this works:
acceptable_values => {
field => [
[ "yes", "Yes, Sir" ],
[ "no", "No, Sir" ],
[ "maybe", "I'm unable to answer, Sir" ],
]
}
It also works really well with selectall_arrayref:
acceptable_values => {
WidgetID => database->selectall_arrayref('SELECT ID, Name FROM Widgets'),
}
Now the edit page has a nice list of widget names instead of a
incomprehensible list of ID's.
The bug (other than not being documented) is that it only works because
of SimpleCRUD passing it to CGI::FormBuilder, and therefore currently
only effects the 'Edit' view.
The attached patch makes the list view work with acceptable_values too.
Subject: | simplecrud-acceptable_values-list.patch |
--- SimpleCRUD.pm.orig 2011-05-13 10:09:37.246425588 -0700
+++ SimpleCRUD.pm 2011-05-13 11:02:35.982164308 -0700
@@ -467,11 +467,30 @@
$sth->execute()
or die "Failed to query for records in $table_name - "
. database->errstr;
+
+ # Setup transforms for columns where acceptable_values specifies alternate names
+ my @acceptable_values_transform;
+ foreach my $column ( keys %{$args->{acceptable_values}} ) {
+ my %replace;
+ foreach my $value ( @{$args->{acceptable_values}->{$column}} ) {
+ next unless ref $value eq "ARRAY";
+ $replace{$value->[0]} = $value->[1];
+ }
+ next unless %replace;
+ push( @acceptable_values_transform, {
+ column => $column,
+ transform => sub {
+ return '' unless $_[0];
+ return $replace{$_[0]} || $_[0]; },
+ });
+ }
+
my $table = HTML::Table::FromDatabase->new
(
-sth => $sth,
-border => 1,
-callbacks => [
+ @acceptable_values_transform,
{
column => 'actions',
transform => sub {