| 1 | package WebAPI::DBIC::Resource::Role::Relationship; | ||||||||||||||||||||
| 2 | 25 | $WebAPI::DBIC::Resource::Role::Relationship::VERSION = '0.004002'; | |||||||||||||||||||
| 3 | |||||||||||||||||||||
| 4 | 25 | ▶ | use Devel::Dwarn; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 25 | ▶ | use Carp qw(confess); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 25 | ▶ | use Hash::Util qw(lock_keys); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 25 | ▶ | use Moo::Role; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 25 | requires 'uri_for'; | |||||||||||||||||||
| 11 | 25 | requires 'id_column_values_for_item'; | |||||||||||||||||||
| 12 | 25 | requires 'add_params_to_url'; | |||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | # recurse into a prefetch-like structure invoking a callback | ||||||||||||||||||||
| 16 | # XXX still a work in progress, only used by ActiveModule so far | ||||||||||||||||||||
| 17 | 107 | ▶ | sub traverse_prefetch { | ||||||||||||||||||
|
Subroutine
traverse_prefetch: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||||||||||||
| 18 | 107 | my $self = shift; | |||||||||||||||||||
| 19 | 107 | my $set = shift; | |||||||||||||||||||
| 20 | 107 | my $parent_rel = shift; | |||||||||||||||||||
| 21 | 107 | my $prefetch = shift; | |||||||||||||||||||
| 22 | 107 | my $callback = shift; | |||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | 107 | ▶ | return unless $prefetch; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | 107 | ▶ | if (not ref($prefetch)) { # leaf node | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 27 | 23 | $callback->($self, $set, $parent_rel, $prefetch); | |||||||||||||||||||
| 28 | 23 | return; | |||||||||||||||||||
| 29 | } | ||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | 84 | ▶ | if (ref($prefetch) eq 'HASH') { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 32 | 41 | ▶ | while (my ($prefetch_key, $prefetch_value) = each(%$prefetch)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 33 | #warn "traverse_prefetch [@$parent_rel] $prefetch\{$prefetch_key}\n"; | ||||||||||||||||||||
| 34 | 23 | $self->traverse_prefetch($set, $parent_rel, $prefetch_key, $callback); | |||||||||||||||||||
| 35 | # XXX traverse_prefetch first arg is a set but this passes a class: | ||||||||||||||||||||
| 36 | 23 | my $result_subclass = $set->result_class->relationship_info($prefetch_key)->{class}; | |||||||||||||||||||
| 37 | 23 | $self->traverse_prefetch($result_subclass, [ @$parent_rel, $prefetch_key ], $prefetch_value, $callback); | |||||||||||||||||||
| 38 | } | ||||||||||||||||||||
| 39 | } | ||||||||||||||||||||
| 40 | elsif (ref($prefetch) eq 'ARRAY') { | ||||||||||||||||||||
| 41 | 43 | for my $sub_prefetch (@$prefetch) { | |||||||||||||||||||
| 42 | 41 | $self->traverse_prefetch($set, $parent_rel, $sub_prefetch, $callback); | |||||||||||||||||||
| 43 | } | ||||||||||||||||||||
| 44 | } | ||||||||||||||||||||
| 45 | else { | ||||||||||||||||||||
| 46 | 0 | confess "Unsupported ref(prefetch): " . ref($prefetch); | |||||||||||||||||||
| 47 | } | ||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | 84 | return; | |||||||||||||||||||
| 50 | } | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | 25 | our %_get_relationship_link_info_cache; | |||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | 451 | ▶ | sub _get_relationship_link_info_cached { | ||||||||||||||||||
|
Subroutine
_get_relationship_link_info_cached: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 56 | 451 | ▶ | my $wrapped = $_get_relationship_link_info_cache{join "\t", @_} ||= [ _get_relationship_link_info(@_) ]; | ||||||||||||||||||
|
Condition100%2/2
$WebAPI::DBIC::Resource::Role::Relationship::_get_relationship_link_info_cache{join "\t", @_} ||= [_get_relationship_link_info(@_)]
MC/DC100%1/1
$WebAPI::DBIC::Resource::Role::Relationship::_get_relationship_link_info_cache{join "\t", @_} ||= [_get_relationship_link_info(@_)]
$WebAPI::DBIC::Resource::Role::Relationship::_get_relationship_link_info_cache{join "\t", @_}
Truth table100%2/2
A
A= $WebAPI::DBIC::Resource::Role::Relationship::_get_relationship_link_info_cache{join "\t", @_}
| |||||||||||||||||||||
| 57 | 451 | return $wrapped->[0]; | |||||||||||||||||||
| 58 | } | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | 50 | ▶ | sub _get_relationship_link_info { | ||||||||||||||||||
|
Subroutine
_get_relationship_link_info: called CC=18 SCAR=37.4
| |||||||||||||||||||||
| 62 | 50 | my ($result_class, $relname) = @_; | |||||||||||||||||||
| 63 | 50 | my $rel = $result_class->relationship_info($relname); | |||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | my $link_info = { # what we'll return | ||||||||||||||||||||
| 66 | result_class => $rel->{source}, | ||||||||||||||||||||
| 67 | 50 | id_fields => undef, | |||||||||||||||||||
| 68 | id_filter => undef, | ||||||||||||||||||||
| 69 | }; | ||||||||||||||||||||
| 70 | 50 | lock_keys(%$link_info); | |||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | 50 | my $cond = $rel->{cond}; | |||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | # https://metacpan.org/pod/DBIx::Class::Relationship::Base#add_relationship | ||||||||||||||||||||
| 75 | 50 | ▶ | if (ref $cond eq 'CODE') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | 43 | ▶ | my $bail = sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.1
| |||||||||||||||||||||
| 79 | 43 | ▶ | my ($inform) = shift || ''; | ||||||||||||||||||
|
Condition50%1/2
shift() || ''
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||
| 80 | 43 | ▶ | unless (our $warn_once->{"$result_class $relname"}++) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 81 | 8 | warn "$result_class relationship $relname has coderef-based condition which is not handled yet $inform\n"; | |||||||||||||||||||
| 82 | 8 | ▶ | Dwarn $rel if $ENV{WEBAPI_DBIC_DEBUG}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 83 | } | ||||||||||||||||||||
| 84 | 43 | return undef; | |||||||||||||||||||
| 85 | 16 | }; | |||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | 86 | ▶ | return sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=10 SCAR=24.2
| |||||||||||||||||||||
| 88 | 86 | my ($self, $code_cond_args) = @_; | |||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | 86 | my ($crosstable_cond, $joinfree_cond) = $cond->({ | |||||||||||||||||||
| 91 | self_alias => 'self', # alias of the invoking resultset ('me' in case of a result object), | ||||||||||||||||||||
| 92 | foreign_alias => 'foreign', # alias of the to-be-joined resultset (often matches relname), | ||||||||||||||||||||
| 93 | %$code_cond_args # eg self_resultsource, foreign_relname, self_rowobj | ||||||||||||||||||||
| 94 | }); | ||||||||||||||||||||
| 95 | #Dwarn [ $crosstable_cond, $joinfree_cond ] unless our $warn_once->{"$result_class $relname dwarn"}++; | ||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | # XXX herein we attempt the insane task of mapping SQL::Abstract conditions | ||||||||||||||||||||
| 98 | # into something usable by WebAPI::DBIC - this is a total hack | ||||||||||||||||||||
| 99 | # There is a better way: https://github.com/timbunce/WebAPI-DBIC/issues/8 | ||||||||||||||||||||
| 100 | # but it requires DBIC 0.082801+ (2014-10-05) | ||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | 86 | for my $crosstable_cond_key (keys %$crosstable_cond) { | |||||||||||||||||||
| 103 | 110 | my $cond = $crosstable_cond->{$crosstable_cond_key}; | |||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | # first we look for the FK indentity field | ||||||||||||||||||||
| 106 | 110 | my $ident; | |||||||||||||||||||
| 107 | 110 | ▶ | if (ref $cond eq 'HASH') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 108 | 110 | ▶ | if ($cond->{'-ident'}) { | ||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||
| 109 | # "foreign.artist" => { "-ident" => "self.artistid" }, | ||||||||||||||||||||
| 110 | 29 | $ident = $cond->{'-ident'} | |||||||||||||||||||
| 111 | } | ||||||||||||||||||||
| 112 | ▶ | elsif (ref $cond->{'='} eq 'HASH' && $cond->{'='}{'-ident'}) { | |||||||||||||||||||
|
Condition66%2/3
ref $cond->{"="} eq "HASH" and $cond->{"="}{"-ident"}
MC/DC50%1/2
ref $cond->{"="} eq "HASH" and $cond->{"="}{"-ident"}
ref $cond->{"="} eq "HASH"
$cond->{"="}{"-ident"}
Truth table66%2/3
A and B
A= ref $cond->{"="} eq "HASH"B= $cond->{"="}{"-ident"}
| |||||||||||||||||||||
| 113 | # "foreign.artist" => { "=" => { "-ident" => "self.artistid" } } | ||||||||||||||||||||
| 114 | 38 | $ident = $cond->{'='}{'-ident'}; | |||||||||||||||||||
| 115 | } | ||||||||||||||||||||
| 116 | elsif (ref $cond->{'='} eq 'SCALAR') { | ||||||||||||||||||||
| 117 | # "foreign.artist" => { "=" => \"self.artistid" }, | ||||||||||||||||||||
| 118 | 0 | $ident = ${ $cond->{'='} }; | |||||||||||||||||||
| 119 | } | ||||||||||||||||||||
| 120 | } | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | 110 | ▶ | if ($ident) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 123 | 67 | ▶ | $ident =~ s/^self\.// or die "panic"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 124 | 67 | $link_info->{id_fields} = [ $ident ]; | |||||||||||||||||||
| 125 | } | ||||||||||||||||||||
| 126 | else { | ||||||||||||||||||||
| 127 | # other kinds of conditions which we'll translate into me.field=foo url params | ||||||||||||||||||||
| 128 | 43 | return $bail->('- unknown crosstable_cond_key'); | |||||||||||||||||||
| 129 | } | ||||||||||||||||||||
| 130 | } | ||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | 43 | ▶ | if ($joinfree_cond) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 133 | # The join-free condition returned for relationship '$rel_name' must be a hash | ||||||||||||||||||||
| 134 | # reference with all keys being valid columns on the related result source | ||||||||||||||||||||
| 135 | 0 | return $bail->('- has join-free condition'); | |||||||||||||||||||
| 136 | } | ||||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | 43 | return $link_info; | |||||||||||||||||||
| 139 | 16 | }; | |||||||||||||||||||
| 140 | } | ||||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | 34 | ▶ | if (ref $cond ne 'HASH') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 143 | # we'll may end up silencing this warning till we can offer better support | ||||||||||||||||||||
| 144 | 0 | ▶ | unless (our $warn_once->{"$result_class $relname"}++) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 145 | 0 | warn "$result_class relationship $relname cond value $cond not handled yet\n"; | |||||||||||||||||||
| 146 | 0 | ▶ | Dwarn $rel if $ENV{WEBAPI_DBIC_DEBUG}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 147 | } | ||||||||||||||||||||
| 148 | 0 | return undef; | |||||||||||||||||||
| 149 | } | ||||||||||||||||||||
| 150 | |||||||||||||||||||||
| 151 | 34 | ▶ | if (keys %$cond > 1) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 152 | # if we loosen this constraint we might need to recheck it for some cases below | ||||||||||||||||||||
| 153 | 0 | ▶ | unless (our $warn_once->{"$result_class $relname"}++) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 154 | 0 | warn "$result_class relationship $relname ignored since it has multiple conditions\n"; | |||||||||||||||||||
| 155 | 0 | ▶ | Dwarn $rel if $ENV{WEBAPI_DBIC_DEBUG}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 156 | } | ||||||||||||||||||||
| 157 | 0 | return undef; | |||||||||||||||||||
| 158 | } | ||||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | # TODO support and test more kinds of relationships | ||||||||||||||||||||
| 161 | # TODO refactor | ||||||||||||||||||||
| 162 | |||||||||||||||||||||
| 163 | 34 | ▶ | if ($rel->{attrs}{accessor} eq 'multi') { # a 1-to-many relationship | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 164 | |||||||||||||||||||||
| 165 | # XXX are there any cases we're not dealing with here? | ||||||||||||||||||||
| 166 | # such as multi-colum FKs | ||||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | 13 | ▶ | Dwarn $rel if $ENV{WEBAPI_DBIC_DEBUG}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 169 | |||||||||||||||||||||
| 170 | 13 | my $foreign_key = (keys %$cond)[0]; | |||||||||||||||||||
| 171 | 13 | ▶ | $foreign_key =~ s/^foreign\.// | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 172 | or warn "Odd, no 'foreign.' prefix on $foreign_key ($result_class, $relname)"; | ||||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | # express that we want to filter the many to match the key(s) of the 1 | ||||||||||||||||||||
| 175 | # here we list the names of the fields in the foreign table that correspond | ||||||||||||||||||||
| 176 | # to the names of the id columns in the result_class table | ||||||||||||||||||||
| 177 | 13 | $link_info->{id_filter} = [ $foreign_key ]; | |||||||||||||||||||
| 178 | 13 | return $link_info; | |||||||||||||||||||
| 179 | } | ||||||||||||||||||||
| 180 | |||||||||||||||||||||
| 181 | # accessor is the inflation type (single/filter/multi) | ||||||||||||||||||||
| 182 | 21 | ▶ | if ($rel->{attrs}{accessor} !~ /^(?: single | filter )$/x) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 183 | 0 | ▶ | unless (our $warn_once->{"$result_class $relname"}++) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 184 | 0 | warn "$result_class relationship $relname ignored since we only support 'single' accessors (not $rel->{attrs}{accessor}) at the moment\n"; | |||||||||||||||||||
| 185 | 0 | ▶ | Dwarn $rel if $ENV{WEBAPI_DBIC_DEBUG}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 186 | } | ||||||||||||||||||||
| 187 | 0 | return undef; | |||||||||||||||||||
| 188 | } | ||||||||||||||||||||
| 189 | |||||||||||||||||||||
| 190 | 21 | my $fieldname = (values %$cond)[0]; # first and only value | |||||||||||||||||||
| 191 | 21 | ▶ | $fieldname =~ s/^self\.// if $fieldname; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 192 | |||||||||||||||||||||
| 193 | 21 | ▶ | if (not $fieldname) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 194 | 0 | ▶ | unless (our $warn_once->{"$result_class $relname"}++) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 195 | 0 | warn "$result_class relationship $relname ignored since we can't determine a fieldname (@{[ %$cond ]})\n"; | |||||||||||||||||||
| 196 | 0 | ▶ | Dwarn $rel if $ENV{WEBAPI_DBIC_DEBUG}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 197 | } | ||||||||||||||||||||
| 198 | 0 | return undef; | |||||||||||||||||||
| 199 | } | ||||||||||||||||||||
| 200 | |||||||||||||||||||||
| 201 | 21 | $link_info->{id_fields} = [ $fieldname ]; | |||||||||||||||||||
| 202 | 21 | return $link_info; | |||||||||||||||||||
| 203 | } | ||||||||||||||||||||
| 204 | |||||||||||||||||||||
| 205 | |||||||||||||||||||||
| 206 | |||||||||||||||||||||
| 207 | 436 | ▶ | sub get_url_for_item_relationship { | ||||||||||||||||||
|
Subroutine
get_url_for_item_relationship: called CC=12 SCAR=25.4
Pod
documented
| |||||||||||||||||||||
| 208 | 436 | my ($self, $item, $relname) = @_; | |||||||||||||||||||
| 209 | |||||||||||||||||||||
| 210 | 436 | my $result_class = $item->result_class; | |||||||||||||||||||
| 211 | |||||||||||||||||||||
| 212 | #Dwarn | ||||||||||||||||||||
| 213 | 436 | ▶ | my $rel_link_info = _get_relationship_link_info_cached($result_class, $relname) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 214 | or return undef; | ||||||||||||||||||||
| 215 | |||||||||||||||||||||
| 216 | 436 | ▶ | if (ref $rel_link_info eq 'CODE') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 217 | 86 | ▶ | $rel_link_info = $rel_link_info->($self, { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 218 | self_resultsource => $item->result_source, | ||||||||||||||||||||
| 219 | self_rowobj => $item, | ||||||||||||||||||||
| 220 | foreign_relname => $relname, # XXX ? | ||||||||||||||||||||
| 221 | }) | ||||||||||||||||||||
| 222 | or return undef; | ||||||||||||||||||||
| 223 | } | ||||||||||||||||||||
| 224 | |||||||||||||||||||||
| 225 | 393 | my @uri_for_args; | |||||||||||||||||||
| 226 | 393 | ▶ | if ($rel_link_info->{id_fields}) { # link to an item (1-1) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 227 | 262 | my @id_kvs = map { $item->get_column($_) } @{ $rel_link_info->{id_fields} }; | |||||||||||||||||||
| 228 | 262 | ▶ | return undef if grep { not defined } @id_kvs; # no link because a key value is null | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 229 | 170 | push @uri_for_args, map { $_ => shift @id_kvs } 1..@id_kvs; | |||||||||||||||||||
| 230 | } | ||||||||||||||||||||
| 231 | |||||||||||||||||||||
| 232 | 301 | ▶ | my $dst_class = $rel_link_info->{result_class} or die "panic"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 233 | 301 | push @uri_for_args, result_class => $dst_class; | |||||||||||||||||||
| 234 | |||||||||||||||||||||
| 235 | 301 | my $linkurl = $self->uri_for( @uri_for_args ); | |||||||||||||||||||
| 236 | |||||||||||||||||||||
| 237 | 301 | ▶ | if (not $linkurl) { # XXX this comment is HAL-specific | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 238 | warn "Result source $dst_class has no resource uri in this app so relations (like $result_class $relname) won't have _links for it.\n" | ||||||||||||||||||||
| 239 | 0 | ▶ | unless our $warn_once->{"$result_class $relname $dst_class"}++; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 240 | 0 | return undef; | |||||||||||||||||||
| 241 | } | ||||||||||||||||||||
| 242 | |||||||||||||||||||||
| 243 | 301 | my %params; | |||||||||||||||||||
| 244 | 301 | ▶ | if (my $id_filter = $rel_link_info->{id_filter}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 245 | 131 | my @id_vals = $self->id_column_values_for_item($item); | |||||||||||||||||||
| 246 | 131 | ▶ | die "panic" if @id_vals != @$id_filter; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 247 | 131 | for my $id_field (@$id_filter) { | |||||||||||||||||||
| 248 | 131 | $params{ "me.".$id_field } = shift @id_vals; | |||||||||||||||||||
| 249 | } | ||||||||||||||||||||
| 250 | } | ||||||||||||||||||||
| 251 | |||||||||||||||||||||
| 252 | 301 | my $href = $self->add_params_to_url( | |||||||||||||||||||
| 253 | $linkurl, | ||||||||||||||||||||
| 254 | {}, | ||||||||||||||||||||
| 255 | \%params, | ||||||||||||||||||||
| 256 | ); | ||||||||||||||||||||
| 257 | |||||||||||||||||||||
| 258 | 301 | return $href; | |||||||||||||||||||
| 259 | } | ||||||||||||||||||||
| 260 | |||||||||||||||||||||
| 261 | |||||||||||||||||||||
| 262 | 15 | ▶ | sub get_url_template_for_set_relationship { # XXX hack, for jsonapi, move? | ||||||||||||||||||
|
Subroutine
get_url_template_for_set_relationship: called CC=11 SCAR=25.8
Pod
undocumented
| |||||||||||||||||||||
| 263 | 15 | my ($self, $set, $relname) = @_; | |||||||||||||||||||
| 264 | |||||||||||||||||||||
| 265 | 15 | my $result_class = $set->result_class; | |||||||||||||||||||
| 266 | 15 | my $result_source = $set->result_source; | |||||||||||||||||||
| 267 | |||||||||||||||||||||
| 268 | #Dwarn | ||||||||||||||||||||
| 269 | 15 | ▶ | my $rel_link_info = _get_relationship_link_info_cached($result_class, $relname) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 270 | or return undef; | ||||||||||||||||||||
| 271 | |||||||||||||||||||||
| 272 | 15 | ▶ | if (ref $rel_link_info eq 'CODE') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 273 | 0 | ▶ | $rel_link_info = $rel_link_info->($self, { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 274 | self_resultsource => $result_source, | ||||||||||||||||||||
| 275 | self_rowobj => undef, # XXX | ||||||||||||||||||||
| 276 | foreign_relname => $relname, # XXX ? | ||||||||||||||||||||
| 277 | }) | ||||||||||||||||||||
| 278 | or return undef; | ||||||||||||||||||||
| 279 | } | ||||||||||||||||||||
| 280 | |||||||||||||||||||||
| 281 | 15 | my @uri_for_args; | |||||||||||||||||||
| 282 | 15 | ▶ | if ($rel_link_info->{id_fields}) { # link to an item (1-1) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 283 | 11 | my @id_kvs = @{ $rel_link_info->{id_fields} }; | |||||||||||||||||||
| 284 | push @uri_for_args, map { | ||||||||||||||||||||
| 285 | 11 | my $name = shift @id_kvs; | |||||||||||||||||||
| 286 | 11 | $_ => "{$relname.$name}" | |||||||||||||||||||
| 287 | } 1..@id_kvs; | ||||||||||||||||||||
| 288 | } | ||||||||||||||||||||
| 289 | |||||||||||||||||||||
| 290 | 15 | ▶ | my $dst_class = $rel_link_info->{result_class} or die "panic"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 291 | 15 | push @uri_for_args, result_class => $dst_class; | |||||||||||||||||||
| 292 | |||||||||||||||||||||
| 293 | 15 | my $linkurl = $self->uri_for( @uri_for_args ); | |||||||||||||||||||
| 294 | |||||||||||||||||||||
| 295 | 15 | ▶ | if (not $linkurl) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 296 | warn "Result source $dst_class has no resource uri in this app so relations (like $result_class $relname) won't have _links for it.\n" | ||||||||||||||||||||
| 297 | 0 | ▶ | unless our $warn_once->{"$result_class $relname $dst_class"}++; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 298 | 0 | return undef; | |||||||||||||||||||
| 299 | } | ||||||||||||||||||||
| 300 | |||||||||||||||||||||
| 301 | 15 | my %params; | |||||||||||||||||||
| 302 | 15 | ▶ | if (my $id_filter = $rel_link_info->{id_filter}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 303 | 4 | my @names = $result_source->unique_constraint_columns( $self->id_unique_constraint_name ); | |||||||||||||||||||
| 304 | 4 | ▶ | die "panic" if @names != @$id_filter; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 305 | 4 | for my $id_field (@$id_filter) { | |||||||||||||||||||
| 306 | 4 | my $name = shift @names; | |||||||||||||||||||
| 307 | 4 | $params{ "me.".$id_field } = "{$relname.$name}"; | |||||||||||||||||||
| 308 | } | ||||||||||||||||||||
| 309 | } | ||||||||||||||||||||
| 310 | |||||||||||||||||||||
| 311 | 15 | my $href = $self->add_params_to_url( | |||||||||||||||||||
| 312 | $linkurl, | ||||||||||||||||||||
| 313 | {}, | ||||||||||||||||||||
| 314 | \%params, | ||||||||||||||||||||
| 315 | ); | ||||||||||||||||||||
| 316 | |||||||||||||||||||||
| 317 | 15 | return $href; | |||||||||||||||||||
| 318 | } | ||||||||||||||||||||
| 319 | |||||||||||||||||||||
| 320 | 25 | 1; | |||||||||||||||||||
| 321 | |||||||||||||||||||||
| 322 | __END__ | ||||||||||||||||||||