]> git.decadent.org.uk Git - maypole.git/blob - examples/fancy_example/BeerDB.pm
fixes for bug 29982 Inconsistency between examples and tutorial
[maypole.git] / examples / fancy_example / BeerDB.pm
1 package BeerDB;
2 use Maypole::Application;
3 use Class::DBI::Loader::Relationship;
4
5 sub debug { $ENV{BEERDB_DEBUG} || 0 }
6 # This is the sample application.  Change this to the path to your
7 # database. (or use mysql or something)
8 use constant DBI_DRIVER => 'SQLite';
9 use constant DATASOURCE => $ENV{BEERDB_DATASOURCE} || 't/beerdb.db';
10
11 BeerDB->config->model('BeerDB::Base'); 
12
13 BEGIN {
14     my $dbi_driver = DBI_DRIVER;
15     if ($dbi_driver =~ /^SQLite/) {
16         unless -e (DATASOURCE) {
17             die sprintf("SQLite datasource '%s' not found, correct the path or recreate the database by running Makefile.PL", DATASOURCE), "\n";
18         }            
19         eval "require DBD::SQLite";
20         if ($@) {
21             eval "require DBD::SQLite2" and $dbi_driver = 'SQLite2';
22         }
23     }
24     BeerDB->setup(join ':', "dbi", $dbi_driver, DATASOURCE);
25 }
26
27 # Give it a name.
28 BeerDB->config->application_name('The Beer Database');
29
30 # Change this to the root of the web site for your maypole application.
31 BeerDB->config->uri_base( $ENV{BEERDB_BASE} || "http://localhost/beerdb/" );
32
33 # Change this to the htdoc root for your maypole application.
34 my @root=  ('t/templates');
35 push @root,$ENV{BEERDB_TEMPLATE_ROOT} if ($ENV{BEERDB_TEMPLATE_ROOT});
36 BeerDB->config->template_root( [@root] ); 
37 # Specify the rows per page in search results, lists, etc : 10 is a nice round number
38 BeerDB->config->rows_per_page(10);
39
40 # Let TT templates recursively include  themselves
41 BeerDB->config->{view_options} = { RECURSION => 1, };
42
43 # Handpumps should not show up.
44 BeerDB->config->display_tables([qw[beer brewery pub style drinker pint person]]);
45 # Access handpumps if want
46 BeerDB->config->ok_tables([ @{BeerDB->config->display_tables}, qw[handpump]]);
47
48 BeerDB::Brewery->untaint_columns( printable => [qw/name notes url/] );
49 BeerDB::Style->untaint_columns( printable => [qw/name notes/] );
50 BeerDB::Beer->untaint_columns(
51     printable => [qw/abv name price notes/],
52     integer => [qw/style brewery score/],
53     date =>[ qw/tasted/],
54 );
55 BeerDB::Pub->untaint_columns(printable => [qw/name notes url/]);
56 BeerDB::Drinker->untaint_columns( printable => [qw/handle created/] );
57 BeerDB::Pint->untaint_columns( printable => [qw/date_and_time/]);
58
59
60 # Required Fields
61 BeerDB->config->{brewery}{required_cols} = [qw/name/];
62 BeerDB->config->{style}{required_cols} = [qw/name/];
63 BeerDB->config->{beer}{required_cols} = [qw/brewery name price/];
64 BeerDB->config->{pub}{required_cols} = [qw/name/];
65 BeerDB->config->{drinker}{required_cols} = [qw/handle person/];
66 BeerDB->config->{pint}{required_cols} = [qw/drinker handpump/]; 
67 BeerDB->config->{person}{required_cols} = [qw/first_name sur_name dob email/];
68
69 # Columns to display 
70 sub BeerDB::Handpump::display_columns { qw/pub beer/ }
71
72 BeerDB->config->{loader}->relationship($_) for (
73     "a brewery produces beers",
74     "a style defines beers",
75     "a pub has beers on handpumps",
76     "a handpump defines pints",
77     "a drinker drinks pints",);
78
79 # For testing classmetadata
80 #sub BeerDB::Beer::classdata :Exported {};
81 sub BeerDB::Beer::list_columns  { return qw/score name price style brewery/};
82
83 sub BeerDB::Handpump::stringify_self { 
84         my $self = shift; 
85         return $self->beer . " @ " . $self->pub;
86 }
87
88
89 1;