]> git.decadent.org.uk Git - maypole.git/blob - lib/Maypole/Model/CDBI/Plain.pm
0ca9bc972931e6277f1ca08a4540336c6615644d
[maypole.git] / lib / Maypole / Model / CDBI / Plain.pm
1 package Maypole::Model::CDBI::Plain;
2 use strict;
3
4 =head1 NAME
5
6 Maypole::Model::CDBI::Plain - Class::DBI model without ::Loader
7
8 =head1 SYNOPSIS
9
10     package Foo;
11     use 'Maypole::Application';
12
13     Foo->config->model("Maypole::Model::CDBI::Plain");
14     Foo->setup([qw/ Foo::SomeTable Foo::Other::Table /]);
15
16     # untaint columns and provide custom actions for each class
17
18     Foo::SomeTable->untaint_columns(email => ['email'], printable => [qw/name description/]);
19
20     Foo::Other::Table->untaint_columns ( ... );
21
22     sub Foo::SomeTable::SomeAction : Exported {
23
24         . . .
25
26     }
27
28 =head1 DESCRIPTION
29
30 This module allows you to use Maypole with previously set-up
31 L<Class::DBI> classes; simply call C<setup> with a list reference
32 of the classes you're going to use, and Maypole will work out the
33 tables and set up the inheritance relationships as normal.
34
35 =cut
36
37 use Class::C3;
38 use Maypole::Config;
39 use base 'Maypole::Model::CDBI::Base';
40
41 use Maypole::Model::CDBI::AsForm;
42 use Maypole::Model::CDBI::FromCGI;
43 use CGI::Untaint::Maypole;
44
45 =head1 METHODS
46
47 =head1 Action Methods
48
49 Action methods are methods that are accessed through web (or other public) interface.
50
51 Inherited from L<Maypole::Model::CDBI::Base>
52
53 =head2 do_edit
54
55 If there is an object in C<$r-E<gt>objects>, then it should be edited
56 with the parameters in C<$r-E<gt>params>; otherwise, a new object should
57 be created with those parameters, and put back into C<$r-E<gt>objects>.
58 The template should be changed to C<view>, or C<edit> if there were any
59 errors. A hash of errors will be passed to the template.
60
61 =head2 do_delete
62
63 Inherited from Maypole::Model::CDBI::Base.
64
65 This action deletes records
66
67 =head2 do_search
68
69 Inherited from Maypole::Model::CDBI::Base.
70
71 This action method searches for database records.
72
73 =head2 list
74
75 Inherited from Maypole::Model::CDBI::Base.
76
77 The C<list> method fills C<$r-E<gt>objects> with all of the
78 objects in the class. The results are paged using a pager.
79
80 =head1 Helper Methods
81
82 =head2 Untainter
83
84 Set the class you use to untaint and validate form data
85 Note it must be of type CGI::Untaint::Maypole (takes $r arg) or CGI::Untaint
86
87 =cut
88
89 sub Untainter { 'CGI::Untaint::Maypole' };
90
91 =head2 setup
92
93   This method is inherited from Maypole::Model::Base and calls setup_database,
94   which uses Class::DBI::Loader to create and load Class::DBI classes from
95   the given database schema.
96
97 =head2 setup_database
98
99   This method loads the model classes for the application
100
101 =cut
102
103 sub setup_database {
104     my ( $self, $config, $namespace, $classes ) = @_;
105     $config->{classes}        = $classes;
106     foreach my $class (@$classes) { $namespace->load_model_subclass($class); }
107     $namespace->model_classes_loaded(1);
108     $config->{table_to_class} = { map { $_->table => $_ } @$classes };
109     $config->{tables}         = [ keys %{ $config->{table_to_class} } ];
110 }
111
112 =head2 class_of
113
114   returns class for given table
115
116 =cut
117
118 sub class_of {
119     my ( $self, $r, $table ) = @_;
120     return $r->config->{table_to_class}->{$table};
121 }
122
123 =head2 adopt
124
125 This class method is passed the name of a model class that represensts a table
126 and allows the master model class to do any set-up required.
127
128 =cut
129
130 sub adopt {
131     my ( $self, $child ) = @_;
132     if ( my $col = $child->stringify_column ) {
133         $child->columns( Stringify => $col );
134     }
135 }
136
137 =head1 SEE ALSO
138
139 L<Maypole::Model::Base>
140
141 L<Maypole::Model::CDBI>
142
143 =cut
144
145
146 1;
147
148