Here's a version of AsForm I've been working on. Its finally complete enough
for my apps. Tony (or whoever maintains this) , please look at it and see what
you think of these changes.
Briefly it does to_select without object creation, makes selects for enum
columns, bool columns. allows you to get inputs for has_a classess instead of
a select box every time (which can get quite annoying when a new customer has_a
phone number, address, and email. :) ). You can limit the selections for
to_select with a sql limiting clause. You can specify which columns to use in
select box. You can use your own column_type sub (which becomes important when
you find out the default one doesn't work). You can get types for is_a columns
(with column_type sub :) ) and maybe more.
I gave a good first run at documenting it . I couldn't get the example formate
good so i'm attaching it and posting it on Maypole wiki along with the code.
see CHANGES and GETTING SPECIFIC IN MODEL CLASS in pod for some details.
thanks
=====
pjs
__________________________________
Do you Yahoo!?
Yahoo! Mail - Helps protect you from nasty viruses.
http://promotions.yahoo.com/new_mail
# Big Example with AsForm.pm Improved
# ********* NOT TESTED **************
# MYSQL schema
CREATE TABLE cd (
cdid INT NOT NULL AUTO_INCREMENT,
genre ENUM('Rock','Dance','Punk') NULL,
hardcopy BOOL NULL,
copy_protection BOOL NULL,
location INTEGER UNSIGNED NULL,
language VARCHAR(50) NULL,
PRIMARY KEY(cdid)
);
CREATE TABLE location (
locid INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
row CHAR(2) NULL,
shelf CHAR(2) NULL,
PRIMARY KEY(locid)
);
CREATE TABLE rip (
ripid INTEGER UNSIGNED NOT NULL,
format VARCHAR(10) NOT NULL,
quality VARCHAR(10) NOT NULL,
PRIMARY KEY(ripid)
);
package Music::DBI;
use Class::DBI::AsForm;
use base Class::DBI;
...
# Define decent column_type until Class::DBI::Column::Type is finished
sub column_type {
my $class = shift;
my $type = $class->_column_info->{$col}->{type} || # check is_a
eval{$class->isa_class($col)->column_type($col)};
if ($type =~ /^tinyint\(1\)$/i) {
$type = "BOOL"; # same as "Bool('No', 'Yes')
}
$type;
}
# Method to get class of is_a inherited columns
# Needed to get proper column type
sub isa_class {
my ($class, $col) = @_;
die "Need a column for isa_class." unless $col;
my ($isaclass, $exists) = ();
eval {
my $isa = $class->meta_info("is_a");
foreach ( keys %$isa ) {
$isaclass = $isa->{$_}->foreign_class;
$exists = $isaclass->find_column($col);
last if $exists;
}
};
$exists ? return $isaclass : return undef;
}
...
package Music::CD;
use base Music::DBI;
...
__PACKAGE__->columns(qw/cdid genre copy_protection hardcopy location language /);
__PACKAGE__->has_a(location => Music::Location);
...
sub has_a_new { { location => [qw/row shelf/] };
# to_field('location') now returns hashref of HTML::Elements for row and shelf.
#class specific customizations of column_type can be useful
sub column_type {
my ($class, $col) = @_;
# change default "No/Yes" options for this boolean field
return "BOOL('Off', 'ON')" if $col eq 'copy_protection';
# Lie and get a cheap select box :)
return "ENUM('English', 'Spanish', 'German')" if $col eq 'language';
return $class->SUPER::column_type($col);
}
...
package Music::Rip;
use base Music::DBI;
use Music::CD;
...
__PACKAGE__->columns(qw/ripid format quality genre copy_protection hardcopy location language /);
__PACKAGE__->is_a(ripid => Music::CD, 'cdid');
sub column_type {
# lock input to one value
return "ENUM('NO')" if $col eq "harcopy";
# Take advantage of easy select box creation for any col
return "ENUM('Best', 'Good', 'Bad')" if $col eq "harcopy";
# Let Class::DBI::Column::Type handle this one
return undef if $col eq "format";
# Be efficient and go right to is_a class to get types for rest
# Also handy if your base column_type method doesn't check is_a classes
return Music::CD->column_type($col);
}
...
# Now this call
my $cgi = Music::Rip->to_cgi;
# RESULTS in these types of elements
# ripid => text input
# format => text input
# quality => select box with "Best" first and thus default
# genre => select box with "Rock" first
# copy_protection => select box with "Off" and "On" having values 0 and 1 and
# column definitions default value selected
# hardcopy => select box with one value, "No".
# location => { row => text input, shelf => text input }
# language => select box "English" first
_______________________________________________
maypole mailing list
maypole at lists.netthink.co.uk
http://lists.netthink.co.uk/listinfo/maypole
This archive was generated by hypermail 2.1.3 : Thu Feb 24 2005 - 22:25:57 GMT