| 1 | package WebAPI::DBIC::Resource::ActiveModel::Role::Item; | ||
| 2 | 25 | $WebAPI::DBIC::Resource::ActiveModel::Role::Item::VERSION = '0.004002'; | |
| 3 | |||
| 4 | 25 | ▶ | use Moo::Role; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | |||
| 7 | 25 | requires '_build_content_types_provided'; | |
| 8 | 25 | requires 'render_item_as_activemodel_hash'; | |
| 9 | 25 | requires 'encode_json'; | |
| 10 | 25 | requires 'item'; | |
| 11 | |||
| 12 | |||
| 13 | 67 | ▶ | around '_build_content_types_provided' => sub { |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 14 | 67 | my $orig = shift; | |
| 15 | 67 | my $self = shift; | |
| 16 | 67 | my $types = $self->$orig(); | |
| 17 | 67 | unshift @$types, { 'application/json' => 'to_json_as_activemodel' }; | |
| 18 | 67 | return $types; | |
| 19 | 25 | }; | |
| 20 | |||
| 21 | #sub to_json_as_activemodel { return $_[0]->encode_json($_[0]->render_item_as_activemodel_hash($_[0]->item)) } | ||
| 22 | 11 | ▶ | sub to_json_as_activemodel { |
|
Subroutine
to_json_as_activemodel: called CC=1 SCAR=0.0
Pod
undocumented
| |||
| 23 | 11 | my $self = shift; | |
| 24 | |||
| 25 | # narrow the set to just contain the specified item | ||
| 26 | # XXX this narrowing ought to be moved elsewhere | ||
| 27 | # it's a bad idea to be a side effect of to_json_as_activemodel | ||
| 28 | 11 | my @id_cols = $self->set->result_source->unique_constraint_columns( $self->id_unique_constraint_name ); | |
| 29 | 11 | @id_cols = map { $self->set->current_source_alias.".$_" } @id_cols; | |
| 30 | 11 | my %id_search; @id_search{ @id_cols } = @{ $self->id }; | |
| 31 | 11 | $self->set( $self->set->search_rs(\%id_search) ); # narrow the set | |
| 32 | |||
| 33 | # set has been narrowed to the item, so we can render the item as if a set | ||
| 34 | # (which is what we need to do for JSON API, which doesn't really have an 'item') | ||
| 35 | |||
| 36 | 11 | return $self->encode_json( $self->render_activemodel_response() ); | |
| 37 | } | ||
| 38 | |||
| 39 | 25 | 1; | |
| 40 | |||
| 41 | __END__ | ||