File Coverage

blib/lib/Gantry/Utils/ModelHelper.pm
Criterion Covered Total %
statement 22 83 26.5
branch 2 22 9.0
condition 0 5 0.0
subroutine 5 10 50.0
pod 5 5 100.0
total 34 125 27.2


line stmt bran cond sub pod time code
1             package Gantry::Utils::ModelHelper;
2 4     4   1202 use strict; use warnings;
  4     4   10  
  4         140  
  4         23  
  4         10  
  4         261  
3              
4             sub import {
5 5     5   22 my $class = shift;
6              
7 5         17 my $callpkg = caller( 0 );
8              
9 5         316 foreach my $method ( @_ ) {
10 12 100       36 if ( $method eq 'auth_db_Main' ) {
11 4     4   19 no strict;
  4         50  
  4         236  
12 4         7 *{ "$callpkg\::db_Main" } = \&{ "$class\::auth_db_Main" };
  4         30  
  4         24  
13             }
14             else {
15 4     4   30 no strict;
  4         7  
  4         3413  
16 8         11 *{ "$callpkg\::$method" } = \&{ "$class\::$method" };
  8         180  
  8         34  
17             }
18             }
19             } # END of import
20              
21             #----------------------------------------------------------------------
22             # db_Main
23             # compatible with Class::DBI and Gantry::Plugins::DBIxClassConn
24             #----------------------------------------------------------------------
25             sub db_Main {
26 0     0 1   my $invocant = shift;
27 0 0         my $class = ( ref $invocant ) ? ref $invocant : $invocant;
28              
29 0           my $dbh;
30              
31 0           my $helper = Gantry::Utils::DBConnHelper->get_subclass();
32              
33 0           $dbh = $helper->get_dbh();
34              
35 0 0         if ( not $dbh ) {
36 0           my $conn_info = $helper->get_conn_info();
37              
38 0           my $db_options = $class->get_db_options();
39              
40 0 0         $db_options->{AutoCommit} = 0 unless defined $db_options->{AutoCommit};
41              
42 0           $dbh = DBI->connect(
43             $conn_info->{ 'dbconn' },
44             $conn_info->{ 'dbuser' },
45             $conn_info->{ 'dbpass' },
46             $db_options
47             );
48 0           $helper->set_dbh( $dbh );
49             }
50            
51 0           return $dbh;
52              
53             } # end db_Main
54              
55             #----------------------------------------------------------------------
56             # auth_db_Main
57             # compatible with Class::DBI and Gantry::Plugins::DBIxClassConn
58             #----------------------------------------------------------------------
59             sub auth_db_Main {
60 0     0 1   my $invocant = shift;
61 0 0         my $class = ( ref $invocant ) ? ref $invocant : $invocant;
62              
63 0           my $auth_dbh;
64              
65 0           my $helper = Gantry::Utils::DBConnHelper->get_subclass();
66              
67 0           $auth_dbh = $helper->get_auth_dbh();
68              
69 0 0         if ( not $auth_dbh ) {
70 0           my $auth_conn_info = $helper->get_auth_conn_info();
71              
72 0           my $db_options = $class->get_db_options();
73              
74 0 0         $db_options->{AutoCommit} = 0 unless defined $db_options->{AutoCommit};
75            
76 0           $auth_dbh = DBI->connect(
77             $auth_conn_info->{ 'auth_dbconn' },
78             $auth_conn_info->{ 'auth_dbuser' },
79             $auth_conn_info->{ 'auth_dbpass' },
80             $db_options
81             );
82 0           $helper->set_auth_dbh( $auth_dbh );
83             }
84              
85 0           return $auth_dbh;
86              
87             } # end auth_db_Main
88              
89             #-------------------------------------------------
90             # $class->get_listing
91             #-------------------------------------------------
92             sub get_listing {
93 0     0 1   my ( $class, $params ) = @_;
94              
95 0           my $f_display_fields = [];
96 0           eval {
97 0           $f_display_fields = $class->get_foreign_display_fields;
98             };
99            
100 0 0         if ( $params->{order_by} ) {
    0          
101 0           return $class->retrieve_all( order_by => $params->{order_by} );
102             }
103             elsif ( $f_display_fields ) {
104 0           return $class->retrieve_all(
105 0           order_by => join( ', ', @{ $f_display_fields } )
106             );
107             }
108             else {
109 0           return $class->retrieve_all( );
110             }
111              
112             }
113              
114             #-------------------------------------------------
115             # $class->retrieve_all_for_main_listing
116             # DEPRECATED
117             #-------------------------------------------------
118             sub retrieve_all_for_main_listing {
119 0     0 1   my ( $class, $order_fields ) = ( shift, shift );
120              
121 0   0       $order_fields ||= join ', ', @{ $class->get_foreign_display_fields };
  0            
122              
123 0           return( $class->retrieve_all( order_by => $order_fields ) );
124              
125             } # retrieve_all_for_main_listing
126              
127             #-------------------------------------------------
128             # $class->get_form_selctions
129             #-------------------------------------------------
130             sub get_form_selections {
131 0     0 1   my $class = shift;
132              
133 0           my %retval;
134              
135             # foreach foreign key get a selection list
136             FOREIGN_TABLE:
137 0           foreach my $foreign_table ( $class->get_foreign_tables() ) {
138              
139 0           my $short_table_name = $foreign_table;
140 0           $short_table_name =~ s/.*:://;
141              
142 0           my $foreigners;
143              
144 0           eval {
145 0           $foreigners = $foreign_table->get_foreign_display_fields();
146             };
147 0 0         if ( $@ ) {
148 0           warn $@;
149 0           next FOREIGN_TABLE;
150             }
151              
152 0           my $order_by = join ', ', @{ $foreigners };
  0            
153              
154             # get all rows in foreign table ordered by foreign display
155 0           my @foreign_display_rows = $foreign_table->retrieve_all(
156             { order_by => $order_by }
157             );
158              
159             # push into returnable hash
160 0           my @items;
161 0           push( @items, { value => '', label => '- Select -' } );
162              
163 0           foreach my $item ( @foreign_display_rows ) {
164              
165 0           my $label;
166              
167 0           eval {
168 0           $label = $item->foreign_display();
169             };
170 0 0         warn " ERROR for " . $item->id() . " $@" if $@;
171              
172 0   0       push @items, {
173             value => $item->id(),
174             label => $label || '',
175             };
176             }
177              
178 0           $retval{$short_table_name} = \@items;
179             }
180              
181 0           return( \%retval );
182              
183             } # end get_form_selections
184              
185             1;
186              
187             =head1 NAME
188              
189             Gantry::Utils::ModelHelper - mixin for model base classes
190              
191             =head1 SYNOPSIS
192              
193             use Gantry::Utils::ModelHelper qw(
194             db_Main
195             get_listing
196             get_form_selections
197             );
198              
199             sub get_db_options {
200             return {}; # put your default options here
201             # consider calling __PACKAGE->_default_attributes
202             }
203              
204             =head1 DESCRIPTION
205              
206             This module provides mixin methods commonly needed by model base classes.
207             Note that you must request the methods you want for the mixin scheme to
208             work. Also note that you can request either db_Main or auth_db_Main, but
209             not both. Whichever one you choose will be exported as db_Main in your
210             package.
211              
212             =head1 METHODS
213              
214             =over 4
215              
216             =item db_Main
217              
218             This method returns a valid dbh using the scheme described in
219             Gantry::Docs::DBConn. It is compatible with Class::DBI and
220             Gantry::Plugins::DBIxClassConn (the later is a mixin which allows
221             easy access to a DBIx::Schema object for controllers).
222              
223             =item auth_db_Main
224              
225             This method is exported as db_Main and works with the scheme described
226             in Gantry::Docs::DBConn. It too is compatible with Class::DBI and
227             Gantry::Plugins::DBIxClassConn.
228              
229             I will repeat, if you ask for this method in your use statement:
230              
231             use lib/Gantry/Utils/ModelHelper qw( auth_db_Main ... );
232              
233             it will come into your namespace as db_Main.
234              
235             =item get_form_selections
236              
237             This method gives you a selection list for each foriegn key in your
238             table. The lists come to you as a single hash keyed by the table names
239             of the foreign keys. The values in the hash are ready for use
240             by form.tt as options on the field (whose type should be select).
241             Example:
242              
243             {
244             status => [
245             { value => 2, label => 'Billed' },
246             { value => 1, label => 'In Progress' },
247             { value => 3, label => 'Paid' },
248             ],
249             other_table => [ ... ],
250             }
251              
252             To use this method, your models must implement these class methods:
253              
254             =over 4
255              
256             =item get_foreign_tables
257              
258             (Must be implemented by the model on which get_form_selections is called.)
259             Returns a list of the fully qualified package names of the models
260             of this table's foreign keys. Example:
261              
262             sub get_foreign_tables {
263             return qw(
264             Apps::AppName::Model::users
265             Apps::AppName::Model::other_table
266             );
267             }
268              
269             =item get_foreign_display_fields
270              
271             (Must be implemented by all the models of this table's foreign keys.)
272             Returns an array reference whose elements are the names of the columns
273             which will appear on the screen in the selection list. Example:
274              
275             sub get_foreign_display_fields {
276             return [ qw( last_name first_name ) ];
277             }
278              
279             =back
280              
281             =item get_listing
282              
283             Replacement for retrieve_all_for_main_listing.
284              
285             Returns a list of row objects (one for each row in the table). The
286             ORDER BY clause is either the same as the foreign_display columns
287             or chosen by you. If you want to supply the order do it like this:
288              
289             my @rows = $MODEL->get_listing ( { order_by => 'last, first' } );
290              
291             Note that your order_by will be used AS IS, so it must be a valid SQL
292             ORDER BY clause, but feel free to include DESC or anything else you and SQL
293             like.
294              
295             =item retrieve_all_for_main_listing
296              
297             DEPRECATED use get_listing instead
298              
299             Returns a list of row objects (one for each row in the table) in order
300             by their foreign_display columns.
301              
302             =back
303              
304             =head1 AUTHOR
305              
306             Phil Crow
307              
308             =head1 COPYRIGHT and LICENSE
309              
310             Copyright (c) 2006, Phil Crow
311              
312             This library is free software; you can redistribute it and/or modify
313             it under the same terms as Perl itself, either Perl version 5.8.6 or,
314             at your option, any later version of Perl 5 you may have available.
315              
316             =cut