Jifty::Collection - Collection of Jifty::Record objects


Jifty documentation Contained in the Jifty distribution.

Index


Code Index:

NAME

Top

Jifty::Collection - Collection of Jifty::Record objects

SYNOPSIS

Top

  package Foo::Model::BarCollection
  use base qw/Jifty::Collection/;

DESCRIPTION

Top

This is a wrapper over Jifty::DBI::Collection that at the same time is a Jifty::Object. To use it, subclass it.

Alternatively, an 'anonymous' collection can be made by creating a new Jifty::Collection object, and calling record_class('Application::Model::Foo') or similar on it.

In addition, each Jifty::Collection includes a Data::Page object to help with calculations related to paged data. You should not call the first_row and rows_per_page methods from Jifty::DBI::Collection on a Jifty::Collection. Instead, if you'd like to use paging, you should use the set_page_info method to set the number of records per page and first record on the current page, and you should use the Data::Page object returned by the pager method to get information related to paging.

MODEL

Top

pager

Returns a Data::Page object associated with this collection. This object defaults to 10 entries per page. You should use only use Data::Page methods on this object to get information about paging, not to set it; use set_page_info to set paging information.

results_are_readable

If your results from the query are guaranteed to be readable by current_user, you can create the collection with results_are_readable => 1. This causes check_read_rights to bypass normal current_user_can checks.

as_search_action PARAMHASH

Returns the Jifty::Action::Record::Search action for the model associated with this collection.

The PARAMHASH allows you to add additional parameters to pass to new_action in Jifty::Web.

add_record

If results_are_readable is false, only add records to the collection that we can read (by checking check_read_rights in Jifty::Record). Otherwise, make sure all records added are readable.

implicit_clauses

Defaults to ordering by the id column.

jifty_serialize_format

This returns an array reference of the individual records that make up this collection.

SEE ALSO

Top

Jifty::DBI::Collection, Jifty::Object, Jifty::Record

LICENSE

Top

Jifty is Copyright 2005-2010 Best Practical Solutions, LLC. Jifty is distributed under the same terms as Perl itself.


Jifty documentation Contained in the Jifty distribution.
use warnings;
use strict;

package Jifty::Collection;

use base qw/Jifty::Object Jifty::DBI::Collection Class::Accessor::Fast/;
use Data::Page;

__PACKAGE__->mk_accessors(qw(results_are_readable));

sub as_search_action {
    my $self = shift;
    return $self->record_class->as_search_action(@_);
}

sub add_record {
    my $self = shift;
    my ($record) = (@_);

    # If results_are_readable is set, guarantee that they are
    $record->_is_readable(1)
        if $self->results_are_readable;

    # Only add a record if results_are_readable or the user has read rights
    $self->SUPER::add_record($record)
        if $self->results_are_readable || $record->check_read_rights;
}

# Overrides the _init method of Jifty::DBI::Collection and is called by new.
# This does the following:
#
#  - Sets up the current user
#  - Sets up the record class, if given as an argument
#  - Sets up results_are_readable, if given as an argument
#  - Sets up the table used for storage
#
sub _init {
    my $self = shift;
    my %args = (
        record_class         => undef,
        current_user         => undef,
        results_are_readable => undef,
        @_
    );

    # Setup the current user, record class, results_are_readable
    $self->_get_current_user(%args);
    $self->record_class( $args{record_class} ) if defined $args{record_class};
    $self->results_are_readable( $args{results_are_readable} );

    # Bad stuff, we really need one of these
    unless ( $self->current_user ) {
        Carp::confess("Collection created without a current user");
    }

    # Setup the table and call the super-implementation
    $self->table( $self->new_item->table() );
    $self->SUPER::_init(%args);
}

sub implicit_clauses {
    my $self = shift;
    $self->order_by( column => 'id', order => 'asc' );
}

sub _new_record_args {
    my $self = shift;
    return ( current_user => $self->current_user );
}

sub _new_collection_args {
    my $self = shift;
    return ( current_user => $self->current_user );
}

sub jifty_serialize_format {
    my $records = shift->items_array_ref;

    return [ map { $_->jifty_serialize_format(@_) } @$records ];
}

1;