]> git.decadent.org.uk Git - maypole.git/blobdiff - ex/fancy_example/BeerDB/Drinker.pm
Beginning to a fancy example app of 2.11 features . See
[maypole.git] / ex / fancy_example / BeerDB / Drinker.pm
diff --git a/ex/fancy_example/BeerDB/Drinker.pm b/ex/fancy_example/BeerDB/Drinker.pm
new file mode 100644 (file)
index 0000000..f3edd7b
--- /dev/null
@@ -0,0 +1,62 @@
+package BeerDB::Drinker;
+use strict;
+use warnings;
+
+use Data::Dumper;
+
+__PACKAGE__->columns('Stringify' => qw/handle/);
+
+# A drinker is a person but we do not want to select who that person is 
+# from a list because this is a 1:1 relationship rather than a M:1. 
+# The no_select option tells AsForm not to bother making a select box
+
+__PACKAGE__->has_a(person => 'BeerDB::Person', no_select => 1);
+
+# Drinker drinks many beers at pubs if they are lucky. I like to specify the
+# name of the foreign key unless i can control the order that the
+# cdbi classes are created. CDBI does not guess very well the fk column.
+
+#__PACKAGE__->has_many(pints => 'BeerDB::Pint', 'drinker');
+
+# When we create a drinker we want to create a person as well
+# So tell AsForm to display the person inputs too.
+
+sub display_columns { qw/person handle/ }
+sub list_columns { qw/person handle/ }
+# AsForm and templates may check for search_colums when making 
+#sub search_columns { qw/person handle/ }
+
+# We need to tweak the cgi inputs a little. 
+# Since list is where addnew is, override that.
+# Person is a has_a rel and AsForm wont make foreign inputs automatically so
+# we manually do it.
+
+sub list : Exported {
+       my ($self, $r) = @_;
+       $self->SUPER::list($r);
+       my %cgi = $self->to_cgi;
+       $cgi{person} = $self->to_field('person', 'foreign_inputs');
+       $r->template_args->{classmetadata}{cgi} = \%cgi;
+       #$r->template_args->{classmetadata}{search_cgi} = $self->search_inputs;
+}
+
+sub view : Exported {
+       my ($self, $r, $obj) = @_;
+       $self->_croak( "Object method only") unless $obj;
+
+       if ($r->params->{submit} eq 'drink') {
+               $r->params->{drinker} = $self;
+               my ($pint, $errs) = $self->related_class($r, 'pints')->create_from_cgi($r);
+               $r->template_args->{errors} = $errs if $errs;
+       }
+
+       my %cgi = $self->to_cgi('pints'); 
+       $cgi{pints}{drinker} =  $obj->to_field(drinker => 'link_hidden', {r => $r});
+       $r->template_args->{classmetadata}{cgi} =  \%cgi ; 
+}
+
+       
+
+#sub foreign_input_delimiter { '__IMODDD__'}
+
+1;