sub begin :Private
{
- $DB::single = 1;
my ($self, $c) = @_;
Catalyst::Controller::DBIC::API::Request->meta->apply($c->req)
sub setup :Chained('specify.in.subclass.config') :CaptureArgs(0) :PathPart('specify.in.subclass.config')
{
- $DB::single = 1;
my ($self, $c) = @_;
$c->req->_set_current_result_set($self->stored_result_source->resultset);
unless(defined($vals))
{
# no data root, assume the request_data itself is the payload
- $vals = [$c->req->request_data || {}];
+ $vals = [$c->req->request_data];
}
elsif(reftype($vals) eq 'HASH')
{
sub deserialize :ActionClass('Deserialize')
{
- $DB::single = 1;
my ($self, $c) = @_;
my $req_params;
sub inflate_request
{
- $DB::single = 1;
my ($self, $c, $params) = @_;
try
List level action chained from L</setup>. List's steps are broken up into three distinct methods: L</list_munge_parameters>, L</list_perform_search>, and L</list_format_output>.
-The goal of this method is to call ->search() on the current_result_set, HashRefInflator the result, and return it in $c->stash->{response}->{$self->data_root}. Pleasee see the individual methods for more details on what actual processing takes place.
+The goal of this method is to call ->search() on the current_result_set, HashRefInflator the result, and return it in $c->stash->{response}->{$self->data_root}. Please see the individual methods for more details on what actual processing takes place.
If the L</select> config param is defined then the hashes will contain only those columns, otherwise all columns in the object will be returned. L</select> of course supports the function/procedure calling semantics that L<DBIx::Class::ResultSet/select>. In order to have proper column names in the result, provide arguments in L</as> (which also follows L<DBIx::Class::ResultSet/as> semantics. Similarly L</count>, L</page>, L</grouped_by> and L</ordered_by> affect the maximum number of rows returned as well as the ordering and grouping. Note that if select, count, ordered_by or grouped_by request parameters are present then these will override the values set on the class with select becoming bound by the select_exposes attribute.
sub list :Private
{
- $DB::single = 1;
my ($self, $c) = @_;
$self->list_munge_parameters($c);
sub list_perform_search
{
- $DB::single = 1;
my ($self, $c) = @_;
try
$req->_set_current_result_set($rs);
$req->_set_search_total_entries($req->current_result_set->pager->total_entries)
- if $req->has_search_attributes &&
- (
- (exists($req->search_attributes->{page}) && defined($req->search_attributes->{page}) && length($req->search_attributes->{page}))
- ||(exists($req->search_attributes->{offset}) && defined($req->search_attributes->{offset}) && length($req->search_attributes->{offset}))
- ||(exists($req->search_attributes->{rows}) && defined($req->search_attributes->{rows}) && length($req->search_attributes->{rows}))
- );
+ if $req->has_search_attributes && (exists($req->search_attributes->{page}) && defined($req->search_attributes->{page}) && length($req->search_attributes->{page}));
}
catch
{
sub list_format_output
{
- $DB::single = 1;
my ($self, $c) = @_;
my $rs = $c->req->current_result_set->search;
foreach my $row ($rs->all)
{
- push(@$formatted, $self->row_format_output($row));
+ push(@$formatted, $self->row_format_output($c, $row));
}
$output->{$self->data_root} = $formatted;
=method_protected row_format_output
-row_format_output is called each row of the inflated output generated from the search. It receives only one argument, the hashref that represents the row. By default, this method is merely a passthrough.
+row_format_output is called each row of the inflated output generated from the search. It receives two arguments, the catalyst context and the hashref that represents the row. By default, this method is merely a passthrough.
=cut
-sub row_format_output { shift; shift; } # passthrough by default
+sub row_format_output
+{
+ my ($self, $c, $row) = @_;
+ return $row; # passthrough by default
+}
=method_protected update_or_create
:Private
-update_or_create is responsible for iterating any stored objects and performing updates or creates. Each object is first validated to ensure it meets the criteria specified in the L</create_requires> and L</create_allows> (or L</update_allows>) parameters of the controller config. The objects are then committed within a transaction via L</transact_objects>.
+update_or_create is responsible for iterating any stored objects and performing updates or creates. Each object is first validated to ensure it meets the criteria specified in the L</create_requires> and L</create_allows> (or L</update_allows>) parameters of the controller config. The objects are then committed within a transaction via L</transact_objects> using a closure around L</save_objects>.
=cut
sub update_or_create :Private
{
- $DB::single = 1;
my ($self, $c) = @_;
if($c->req->has_objects)
{
$self->validate_objects($c);
- $self->transact_objects($c, \&save_objects);
+ $self->transact_objects($c, sub { $self->save_objects($c, @_) } );
}
else
{
sub transact_objects
{
- $DB::single = 1;
my ($self, $c, $coderef) = @_;
try
sub validate_objects
{
- $DB::single = 1;
my ($self, $c) = @_;
try
sub validate_object
{
- $DB::single = 1;
my ($self, $c, $obj) = @_;
my ($object, $params) = @$obj;
:Private
-delete operates on the stored objects in the request. It first transacts the objects, deleting them in the database, and then clears the request store of objects.
+delete operates on the stored objects in the request. It first transacts the objects, deleting them in the database using L</transact_objects> and a closure around L</delete_objects>, and then clears the request store of objects.
=cut
sub delete :Private
{
- $DB::single = 1;
my ($self, $c) = @_;
if($c->req->has_objects)
{
- $self->transact_objects($c, \&delete_objects);
+ $self->transact_objects($c, sub { $self->delete_objects($c, @_) });
$c->req->clear_objects;
}
else
}
}
-=head1 HELPER FUNCTIONS
+=method_protected save_objects
-This functions are only helper functions and should have a void invocant. If they are called as methods, they will die. The only reason they are stored in the class is to allow for customization without rewriting the methods that make use of these helper functions.
+This method is used by update_or_create to perform the actual database manipulations. It iterates each object calling L</save_object>.
-=head2 save_objects
+=cut
-This helper function is used by update_or_create to perform the actual database manipulations.
+sub save_objects
+{
+ my ($self, $c, $objects) = @_;
-=head2 delete_objects
+ foreach my $obj (@$objects)
+ {
+ $self->save_object($c, $obj);
+ }
+}
+
+=method_protected save_object
-This helper function is used by delete to perform the actual database delete of objects.
+save_object first checks to see if the object is already in storage. If so, it calls L</update_object_from_params> otherwise it calls L</insert_object_from_params>
=cut
-# NOT A METHOD
-sub save_objects
+sub save_object
{
- my ($objects) = @_;
- die 'save_objects coderef had an invocant and shouldn\'t have had one' if blessed($objects);
+ my ($self, $c, $obj) = @_;
- foreach my $obj (@$objects)
+ my ($object, $params) = @$obj;
+
+ if ($object->in_storage)
{
- my ($object, $params) = @$obj;
-
- if ($object->in_storage) {
- foreach my $key (keys %{$params}) {
- my $value = $params->{$key};
- if (ref($value) && !($value == JSON::Any::true || $value == JSON::Any::false)) {
- my $related_params = delete $params->{$key};
- my $row = $object->find_related($key, {} , {});
- $row->update($related_params);
- }
- }
- $object->update($params);
- } else {
- $object->set_columns($params);
- $object->insert;
+ $self->update_object_from_params($c, $object, $params);
+ }
+ else
+ {
+ $self->insert_object_from_params($c, $object, $params);
+ }
+
+}
+
+=method_protected update_object_from_params
+
+update_object_from_params iterates through the params to see if any of them are pertinent to relations. If so it calls L</update_object_relation> with the object, and the relation parameters. Then it calls ->upbdate on the object.
+
+=cut
+
+sub update_object_from_params
+{
+ my ($self, $c, $object, $params) = @_;
+
+ foreach my $key (keys %$params)
+ {
+ my $value = $params->{$key};
+ if (ref($value) && !($value == JSON::Any::true || $value == JSON::Any::false))
+ {
+ $self->update_object_relation($c, $object, delete $params->{$key}, $key);
}
}
+
+ $object->update($params);
+}
+
+=method_protected update_object_relation
+
+update_object_relation finds the relation to the object, then calls ->update with the specified parameters
+
+=cut
+
+sub update_object_relation
+{
+ my ($self, $c, $object, $related_params, $relation) = @_;
+ my $row = $object->find_related($relation, {} , {});
+ $row->update($related_params);
+}
+
+=method_protected insert_object_from_params
+
+insert_object_from_params sets the columns for the object, then calls ->insert
+
+=cut
+
+sub insert_object_from_params
+{
+ my ($self, $c, $object, $params) = @_;
+ $object->set_columns($params);
+ $object->insert;
}
-# NOT A METHOD
+=method_protected delete_objects
+
+delete_objects iterates through each object calling L</delete_object>
+
+=cut
+
sub delete_objects
{
- my ($objects) = @_;
- die 'delete_objects coderef had an invocant and shouldn\'t have had one' if blessed($objects);
+ my ($self, $c, $objects) = @_;
+
+ map { $self->delete_object($c, $_->[0]) } @$objects;
+}
+
+=method_protected delete_object
+
+Performs the actual ->delete on the object
+
+=cut
+
+sub delete_object
+{
+ my ($self, $c, $object) = @_;
- map { $_->[0]->delete } @$objects;
+ $object->delete;
}
=method_protected end
sub end :Private
{
- $DB::single = 1;
my ($self, $c) = @_;
# check for errors
}
elsif($self->return_object && $c->req->has_objects)
{
- $DB::single = 1;
my $returned_objects = [];
push(@$returned_objects, $self->each_object_inflate($c, $_)) for map { $_->[0] } $c->req->all_objects;
$c->stash->{response}->{$self->data_root} = scalar(@$returned_objects) > 1 ? $returned_objects : $returned_objects->[0];