]> git.decadent.org.uk Git - maypole.git/blob - lib/Maypole/Model/Base.pm
"If you set a template in the authenticate method, then the model's action will not...
[maypole.git] / lib / Maypole / Model / Base.pm
1 package Maypole::Model::Base;
2 our %remember;
3 sub MODIFY_CODE_ATTRIBUTES { $remember{$_[1]} = $_[2]; () }
4
5 sub FETCH_CODE_ATTRIBUTES { $remember{$_[1]} } 
6 sub view :Exported { }
7 sub edit :Exported { }
8
9 sub process {
10     my ($class, $r) = @_;
11     my $method = $r->action;
12     return if $r->{template}; # Authentication has set this, we're done.
13
14     $r->{template} = $method;
15     my $obj = $class->retrieve( $r->{args}->[0] );
16     if ($obj) {
17         $r->objects([ $obj ]);
18         shift @{$r->{args}};
19     }
20     $class->$method($r);
21 }
22
23 sub display_columns { 
24     sort shift->columns;
25 }
26
27 =head1 NAME
28
29 Maypole::Model::Base - Base class for model classes
30
31 =head1 DESCRIPTION
32
33 Anyone subclassing this for a different database abstraction mechanism
34 needs to provide the following methods:
35
36 =head2 do_edit
37
38 If there is an object in C<$r-E<gt>objects>, then it should be edited
39 with the parameters in C<$r-E<gt>params>; otherwise, a new object should
40 be created with those parameters, and put back into C<$r-E<gt>objects>.
41 The template should be changed to C<view>, or C<edit> if there were any
42 errors. A hash of errors will be passed to the template.
43
44 =cut
45
46 sub do_edit { die "This is an abstract method" }
47
48 =head2 setup_database
49
50     $model->setup_database($config, $namespace, @data)
51
52 Uses the user-defined data in C<@data> to specify a database- for
53 example, by passing in a DSN. The model class should open the database,
54 and create a class for each table in the database. These classes will
55 then be C<adopt>ed. It should also populate C<< $config->{tables} >> and
56 C<< $config->{classes} >> with the names of the classes and tables
57 respectively. The classes should be placed under the specified
58 namespace. For instance, C<beer> should be mapped to the class
59 C<BeerDB::Beer>.
60
61 =head2 class_of
62
63     $model->class_of($r, $table)
64
65 This maps between a table name and its associated class.
66
67 =head2 retrieve
68
69 This turns an ID into an object of the appropriate class.
70
71 =head2 adopt
72
73 This is called on an model class representing a table and allows the
74 master model class to do any set-up required. 
75
76 =head2 related
77
78 This can go either in the master model class or in the individual
79 classes, and returns a list of has-many accessors. A brewery has many
80 beers, so C<BeerDB::Brewery> needs to return C<beers>.
81
82 =head2 columns
83
84 This is a list of all the columns in a table. You may also override
85 C<display_columns>, which is the list of columns you want to view, in
86 the right order.
87
88 =head2 table
89
90 This is the name of the table.
91
92 =head2 Commands
93
94 =over
95
96 =item list
97
98 The C<list> method should fill C<< $r-> objects >> with all of the
99 objects in the class. You may want to page this using C<Data::Page> or
100 similar.
101
102 =back
103
104 =cut
105
106 sub class_of       { die "This is an abstract method" }
107 sub setup_database { die "This is an abstract method" }
108 sub list :Exported { die "This is an abstract method" };
109
110 =pod
111
112 Also, see the exported commands in C<Maypole::Model::CDBI>.
113
114 =head1 Other overrides
115
116 Additionally, individual derived model classes may want to override the
117 following methods:
118
119 =head2 column_names
120
121 Return a hash mapping column names with human-readable equivalents.
122
123 =cut
124
125 sub column_names { my $class = shift; map { 
126         my $col = $_;
127         $col =~ s/_+(\w)?/ \U\1/g;
128         $_ => ucfirst $col } $class->columns }
129
130 =head2 description
131
132 A description of the class to be passed to the template.
133
134 =cut
135
136 sub description { "A poorly defined class" }
137
138 1;
139