| 1 | 113 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | 113 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | package Jifty::Collection; | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 113 | ▶ | use base qw/Jifty::Object Jifty::DBI::Collection Class::Accessor::Fast/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 113 | ▶ | use Data::Page; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | =head1 NAME | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | Jifty::Collection - Collection of Jifty::Record objects | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | package Foo::Model::BarCollection | ||||||||||||||||||||||||||||
| 16 | use base qw/Jifty::Collection/; | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | This is a wrapper over L<Jifty::DBI::Collection> that at the same time | ||||||||||||||||||||||||||||
| 21 | is a L<Jifty::Object>. To use it, subclass it. | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | Alternatively, an 'anonymous' collection can be made by creating a new | ||||||||||||||||||||||||||||
| 24 | C<Jifty::Collection> object, and calling | ||||||||||||||||||||||||||||
| 25 | C<record_class('Application::Model::Foo')> or similar on it. | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | In addition, each L<Jifty::Collection> includes a L<Data::Page> object | ||||||||||||||||||||||||||||
| 28 | to help with calculations related to paged data. You should B<not> | ||||||||||||||||||||||||||||
| 29 | call the C<first_row> and C<rows_per_page> methods from | ||||||||||||||||||||||||||||
| 30 | L<Jifty::DBI::Collection> on a L<Jifty::Collection>. Instead, if | ||||||||||||||||||||||||||||
| 31 | you'd like to use paging, you should use the C<set_page_info> method | ||||||||||||||||||||||||||||
| 32 | to B<set> the number of records per page and first record on the | ||||||||||||||||||||||||||||
| 33 | current page, and you should use the L<Data::Page> object returned by | ||||||||||||||||||||||||||||
| 34 | the C<pager> method to B<get> information related to paging. | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | =head1 MODEL | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | =head2 pager | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | Returns a L<Data::Page> object associated with this collection. This | ||||||||||||||||||||||||||||
| 41 | object defaults to 10 entries per page. You should use only use | ||||||||||||||||||||||||||||
| 42 | L<Data::Page> methods on this object to B<get> information about | ||||||||||||||||||||||||||||
| 43 | paging, not to B<set> it; use C<set_page_info> to set paging | ||||||||||||||||||||||||||||
| 44 | information. | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | =head2 results_are_readable | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | If your results from the query are guaranteed to be readable by | ||||||||||||||||||||||||||||
| 49 | current_user, you can create the collection with | ||||||||||||||||||||||||||||
| 50 | C<< results_are_readable => 1 >>. This causes check_read_rights to bypass | ||||||||||||||||||||||||||||
| 51 | normal current_user_can checks. | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | =cut | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 113 | __PACKAGE__->mk_accessors(qw(results_are_readable)); | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | =head2 as_search_action PARAMHASH | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | Returns the L<Jifty::Action::Record::Search> action for the model | ||||||||||||||||||||||||||||
| 60 | associated with this collection. | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | The PARAMHASH allows you to add additional parameters to pass to | ||||||||||||||||||||||||||||
| 63 | L<Jifty::Web/new_action>. | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | =cut | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 0 | ▶ | sub as_search_action { | ||||||||||||||||||||||||||
|
Subroutine
as_search_action: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 68 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 69 | 0 | return $self->record_class->as_search_action(@_); | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | =head2 add_record | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | If L</results_are_readable> is false, only add records to the | ||||||||||||||||||||||||||||
| 75 | collection that we can read (by checking | ||||||||||||||||||||||||||||
| 76 | L<Jifty::Record/check_read_rights>). Otherwise, make sure all records | ||||||||||||||||||||||||||||
| 77 | added are readable. | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | =cut | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 0 | ▶ | sub add_record { | ||||||||||||||||||||||||||
|
Subroutine
add_record: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 82 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 83 | 0 | my ($record) = (@_); | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | # If results_are_readable is set, guarantee that they are | ||||||||||||||||||||||||||||
| 86 | 0 | ▶ | $record->_is_readable(1) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | if $self->results_are_readable; | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | # Only add a record if results_are_readable or the user has read rights | ||||||||||||||||||||||||||||
| 90 | 0 | ▶ | $self->SUPER::add_record($record) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->results_are_readable or $record->check_read_rights
MC/DC0%0/2
$self->results_are_readable or $record->check_read_rights
$self->results_are_readable
$record->check_read_rights
Truth table0%0/3
A or B
A= $self->results_are_readableB= $record->check_read_rights
| |||||||||||||||||||||||||||||
| 91 | if $self->results_are_readable || $record->check_read_rights; | ||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | # Overrides the _init method of Jifty::DBI::Collection and is called by new. | ||||||||||||||||||||||||||||
| 95 | # This does the following: | ||||||||||||||||||||||||||||
| 96 | # | ||||||||||||||||||||||||||||
| 97 | # - Sets up the current user | ||||||||||||||||||||||||||||
| 98 | # - Sets up the record class, if given as an argument | ||||||||||||||||||||||||||||
| 99 | # - Sets up results_are_readable, if given as an argument | ||||||||||||||||||||||||||||
| 100 | # - Sets up the table used for storage | ||||||||||||||||||||||||||||
| 101 | # | ||||||||||||||||||||||||||||
| 102 | 0 | ▶ | sub _init { | ||||||||||||||||||||||||||
|
Subroutine
_init: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 103 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 104 | 0 | my %args = ( | |||||||||||||||||||||||||||
| 105 | record_class => undef, | ||||||||||||||||||||||||||||
| 106 | current_user => undef, | ||||||||||||||||||||||||||||
| 107 | results_are_readable => undef, | ||||||||||||||||||||||||||||
| 108 | @_ | ||||||||||||||||||||||||||||
| 109 | ); | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | # Setup the current user, record class, results_are_readable | ||||||||||||||||||||||||||||
| 112 | 0 | $self->_get_current_user(%args); | |||||||||||||||||||||||||||
| 113 | 0 | ▶ | $self->record_class( $args{record_class} ) if defined $args{record_class}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | 0 | $self->results_are_readable( $args{results_are_readable} ); | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | # Bad stuff, we really need one of these | ||||||||||||||||||||||||||||
| 117 | 0 | ▶ | unless ( $self->current_user ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | 0 | Carp::confess("Collection created without a current user"); | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | # Setup the table and call the super-implementation | ||||||||||||||||||||||||||||
| 122 | 0 | $self->table( $self->new_item->table() ); | |||||||||||||||||||||||||||
| 123 | 0 | $self->SUPER::_init(%args); | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | =head2 implicit_clauses | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | Defaults to ordering by the C<id> column. | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | =cut | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 0 | ▶ | sub implicit_clauses { | ||||||||||||||||||||||||||
|
Subroutine
implicit_clauses: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 133 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 134 | 0 | $self->order_by( column => 'id', order => 'asc' ); | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 0 | ▶ | sub _new_record_args { | ||||||||||||||||||||||||||
|
Subroutine
_new_record_args: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 138 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 139 | 0 | return ( current_user => $self->current_user ); | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 0 | ▶ | sub _new_collection_args { | ||||||||||||||||||||||||||
|
Subroutine
_new_collection_args: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 143 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 144 | 0 | return ( current_user => $self->current_user ); | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | =head2 jifty_serialize_format | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | This returns an array reference of the individual records that make up this | ||||||||||||||||||||||||||||
| 150 | collection. | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | =cut | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 0 | ▶ | sub jifty_serialize_format { | ||||||||||||||||||||||||||
|
Subroutine
jifty_serialize_format: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 155 | 0 | my $records = shift->items_array_ref; | |||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 0 | return [ map { $_->jifty_serialize_format(@_) } @$records ]; | |||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | L<Jifty::DBI::Collection>, L<Jifty::Object>, L<Jifty::Record> | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | Jifty is Copyright 2005-2010 Best Practical Solutions, LLC. | ||||||||||||||||||||||||||||
| 167 | Jifty is distributed under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | =cut | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 113 | 1; | |||||||||||||||||||||||||||