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