Schema/Result/Navigation.pm

filter: Statementstmt 94.9% 56 / 59 Branchbran 90.0% 9 / 10 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 81.8% 9 / 11 Podpod 100% 3 / 3 totaltotal 93.1% 82 / 88 CC 7 SCAR 7.2
CC
7
Cov
94%
generate_uri
3.0
insert
3.0
siblings_with_self
2.0
CRAP 2.1
« Test/Role/Fixtures.pm ↑ Summary Schema/Result/PriceModifier.pm »
1 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package Interchange6::Schema::Result::Navigation;
4  
5 =head1 NAME
6  
7 Interchange6::Schema::Result::Navigation
8  
9 =cut
10  
11 2 ▶use base 'Interchange6::Schema::Base::Attribute';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 use Interchange6::Schema::Candy -components =>
14 2 ▶ [qw(Tree::AdjacencyList InflateColumn::DateTime TimeStamp)];
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 =head1 DESCRIPTION
20  
21 Navigation is where all navigation, category and static page details are stored. In addition
22 information such as page title can be linked to these records as attributes.
23  
24 =over 4
25  
26 =item B<Attribute>
27  
28 Common attribute names for a Navigation records include these examples.
29  
30 meta_title
31 meta_description
32 meta_keywords
33 head_js
34 head_css
35  
36 =back
37  
38 =cut
39  
40 =head1 SYNOPSIS
41  
42 NOTE: with items such as head_css which may contain more than one record you must set the priority of the record.
43 This ensures each record has a unique value and also allows for proper ordering.
44  
45 $nav->add_attribute({name => 'head_css', priority => '1'}, '/css/main.css');
46 $nav->add_attribute({name => 'head_css', priority => '2'}, '/css/fancymenu.css');
47  
48 =head1 ACCESSORS
49  
50 =head2 navigation_id
51  
52 Primary key.
53  
54 =cut
55  
56 2 primary_column navigation_id => {
57 data_type => "integer",
58 is_auto_increment => 1,
59 sequence => "navigation_navigation_id_seq",
60 };
61  
62 =head2 uri
63  
64 URI.
65  
66 Unique constraint.
67  
68 See L</generate_uri> method for details of how L</uri> can be created
69 automatically based on the value of L</name>.
70  
71 =cut
72  
73 2 unique_column uri => {
74 data_type => "varchar",
75 default_value => "",
76 size => 255
77 };
78  
79 =head2 type
80  
81 Type, e.g.: nav, category.
82  
83 =cut
84  
85 2 column type =>
86 { data_type => "varchar", default_value => "", size => 32 };
87  
88 =head2 scope
89  
90 Scope, e.g.: menu-main, top-login.
91  
92 =cut
93  
94 2 column scope =>
95 { data_type => "varchar", default_value => "", size => 32 };
96  
97 =head2 name
98  
99 Name, e.g.: Hand Tools, Fly Fishing Rods.
100  
101 Defaults to empty string.
102  
103 =cut
104  
105 2 column name => {
106 data_type => "varchar",
107 default_value => "",
108 size => 255
109 };
110  
111 =head2 description
112  
113 Description, e.g.: All of our hand tools, Our collection of top fly fishing rods.
114  
115 Defaults to empty string.
116  
117 =cut
118  
119 2 column description => {
120 data_type => "varchar",
121 default_value => "",
122 size => 1024
123 };
124  
125 =head2 alias
126  
127 FK on L<Interchange6::Schema::Result::Navigation/navigation_id>.
128  
129 Can be used for things such as menus in different languages which link back
130 to the primary navigation menu.
131  
132 Is nullable.
133  
134 =cut
135  
136 2 column alias =>
137 { data_type => "integer", is_foreign_key => 1, is_nullable => 1 };
138  
139 =head2 parent_id
140  
141 Used by L<DBIx::Class::Tree::AdjacencyList> to setup parent/child relationships.
142  
143 Is nullable.
144  
145 =cut
146  
147 2 column parent_id => { data_type => "integer", is_nullable => 1 };
148  
149 =head2 priority
150  
151 Signed integer priority. We normally order descending.
152  
153 Defaults to 0.
154  
155 =cut
156  
157 2 column priority =>
158 { data_type => "integer", default_value => 0 };
159  
160 =head2 product_count
161  
162 Can be used to cache product counts.
163  
164 Default to 0.
165  
166 =cut
167  
168 2 column product_count =>
169 { data_type => "integer", default_value => 0 };
170  
171 =head2 created
172  
173 Date and time when this record was created returned as L<DateTime> object.
174 Value is auto-set on insert.
175  
176 =cut
177  
178 2 column created =>
179 { data_type => "datetime", set_on_create => 1 };
180  
181 =head2 last_modified
182  
183 Date and time when this record was last modified returned as L<DateTime> object.
184 Value is auto-set on insert and update.
185  
186 =cut
187  
188 2 column last_modified => {
189 data_type => "datetime",
190 set_on_create => 1,
191 set_on_update => 1,
192 };
193  
194 =head2 active
195  
196 Whether navigation is active and therefore should be displayed.
197  
198 Boolean defaults to true (1).
199  
200 =cut
201  
202 2 column active =>
203 { data_type => "boolean", default_value => 1 };
204  
205 =head1 METHODS
206  
207 Attribute methods are provided by the L<Interchange6::Schema::Base::Attribute> class.
208  
209 =head2 insert
210  
211 Override inherited method to call L</generate_uri> method in case L</name>
212 has been supplied as an argument but L</uri> has not.
213  
214 =cut
215  
216 sub insert {
217 139 ▶ my ( $self, @args ) = @_;
Subroutine insert: called CC=3 SCAR=11.0
Pod documented
218 139 ▶ if ( $self->name && !$self->uri ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->name and not $self->uri) 11 128 139
Condition100%3/3
$self->name and not $self->uri
!ll&&!rl&&r
212611
MC/DC100%2/2
$self->name and not $self->uri
$self->name not $self->uri
Truth table100%3/3
A and not(B)
A= $self->nameB= not $self->uri
ABresult
0X0
100
111
219 11 $self->generate_uri;
220 }
221 138 $self->next::method(@args);
222 137 return $self;
223 }
224  
225 =head2 generate_uri($attrs)
226  
227 Called by L</new> if no uri is given as an argument.
228  
229 The following steps are taken:
230  
231 =over
232  
233 1. Stash C<< $self->name >> in C<$uri> to allow manipulation via filters
234  
235 2. Remove leading and trailing spaces and replace remaining spaces and
236 C</> with C<->
237  
238 3. Search for all rows in L<Interchange6::Schema::Result::Setting> where
239 C<scope> is C<Navigation> and C<name> is <generate_uri_filter>
240  
241 4. For each row found eval C<< $row->value >>
242  
243 5. Finally set the value of column L</uri> to C<$uri>
244  
245 =back
246  
247 Filters stored in L<Interchange6::Schema::Result::Setting> are executed via
248 eval and have access to C<$uri> and also the navigation result held in
249 C<$self>
250  
251 Examples of filters stored in Setting might be:
252  
253 {
254 scope => 'Navigation',
255 name => 'generate_uri_filter',
256 value => '$uri =~ s/badstuff/goodstuff/gi',
257 },
258 {
259 scope => 'Navigation',
260 name => 'generate_uri_filter',
261 value => '$uri = lc($uri)',
262 },
263  
264 =cut
265  
266 sub generate_uri {
267 11 ▶ my $self = shift;
Subroutine generate_uri: called CC=3 SCAR=11.0
Pod documented
268  
269 11 my $uri = $self->name;
270  
271 # make sure we have clean utf8
272 try {
273 11 ▶ $uri = Encode::decode( 'UTF-8', $uri, Encode::FB_CROAK )
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::is_utf8($uri) 7 4 11
274 unless utf8::is_utf8($uri);
275 }
276 catch {
277 0 ▶ $self->throw_exception(
Subroutine __ANON__: not called CC=1 SCAR=6.9
278 "Navigation->generate_uri failed to decode UTF-8 text: $_" );
279 11 };
280  
281 11 $uri =~ s/^\s+//; # remove leading space
282 11 $uri =~ s/\s+$//; # remove trailing space
283 11 $uri =~ s{[\s/]+}{-}g; # change space and / to -
284  
285 11 my $filters = $self->result_source->schema->resultset('Setting')->search(
286 {
287 scope => 'Navigation',
288 name => 'generate_uri_filter',
289 },
290 );
291  
292 11 ▶ while ( my $filter = $filters->next ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $filter = $filters->next) 3 10 13
293 3 eval $filter->value;
294 3 ▶ $self->throw_exception("Navigation->generate_uri filter croaked: $@")
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 2 3
295 if $@;
296 }
297  
298 10 $self->uri($uri);
299 }
300  
301  
302 =head2 siblings_with_self
303  
304 Similar to the inherited L<siblings|DBIx::Class::Tree::AdjacencyList/siblings> method but also returns the object itself in the result set/list.
305  
306 =cut
307  
308 sub siblings_with_self {
309 1 ▶ my $self = shift;
Subroutine siblings_with_self: called CC=2 SCAR=7.0
Pod documented
310 1 my $rs = $self->result_source->resultset->search(
311 {
312 parent_id => $self->parent_id,
313 }
314 );
315 1 ▶ return $rs->all() if (wantarray());
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 1 1
316 1 return $rs;
317 }
318  
319 =head1 INHERITED METHODS
320  
321 =head2 DBIx::Class::Tree::AdjacencyList
322  
323 =over 4
324  
325 =item *
326  
327 L<parent|DBIx::Class::Tree::AdjacencyList/parent>
328  
329 =item *
330  
331 L<ancestors|DBIx::Class::Tree::AdjacencyList/ancestors>
332  
333 =item *
334  
335 L<has_descendant|DBIx::Class::Tree::AdjacencyList/has_descendant>
336  
337 =item *
338  
339 L<parents|DBIx::Class::Tree::AdjacencyList/parents>
340  
341 =item *
342  
343 L<children|DBIx::Class::Tree::AdjacencyList/children>
344  
345 =item *
346  
347 L<attach_child|DBIx::Class::Tree::AdjacencyList/attach_child>
348  
349 =item *
350  
351 L<siblings|DBIx::Class::Tree::AdjacencyList/siblings>
352  
353 =item *
354  
355 L<attach_sibling|DBIx::Class::Tree::AdjacencyList/attach_sibling>
356  
357 =item *
358  
359 L<is_leaf|DBIx::Class::Tree::AdjacencyList/is_leaf>
360  
361 =item *
362  
363 L<is_root|DBIx::Class::Tree::AdjacencyList/is_root>
364  
365 =item *
366  
367 L<is_branch|DBIx::Class::Tree::AdjacencyList/is_branch>
368  
369 =back
370  
371 =cut
372  
373 # define parent column
374  
375 2 __PACKAGE__->parent_column('parent_id');
376  
377 =head1 RELATIONS
378  
379 =head2 active_children
380  
381 Related object: L<Interchange6::Schema::Result::Navigation>
382  
383 Conditions: self.parent_id = foreign.navigation_id && foreign.active = 1
384  
385 =cut
386  
387 has_many
388 active_children => "Interchange6::Schema::Result::Navigation",
389 sub {
390 0 ▶ my $args = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
391  
392 return {
393 0 "$args->{foreign_alias}.parent_id" =>
394 { -ident => "$args->{self_alias}.navigation_id" },
395 "$args->{foreign_alias}.active" => 1,
396 };
397 2 };
398  
399 =head2 navigation_products
400  
401 Type: has_many
402  
403 Related object: L<Interchange6::Schema::Result::NavigationProduct>
404  
405 =cut
406  
407 2 has_many
408 navigation_products => "Interchange6::Schema::Result::NavigationProduct",
409 "navigation_id",
410 { cascade_copy => 0, cascade_delete => 0 };
411  
412 =head2 products
413  
414 Type: many_to_many
415  
416 Accessor to related product results ordered by priority and name.
417  
418 =cut
419  
420 2 many_to_many
421 products => "navigation_products",
422 "product", { order_by => [ 'product.priority', 'product.name' ] };
423  
424 =head2 navigation_attributes
425  
426 Type: has_many
427  
428 Related object: L<Interchange6::Schema::Result::NavigationAttribute>
429  
430 =cut
431  
432 2 has_many
433 navigation_attributes => "Interchange6::Schema::Result::NavigationAttribute",
434 "navigation_id",
435 { cascade_copy => 0, cascade_delete => 0 };
436  
437 =head2 attributes
438  
439 Type: many_to_many
440  
441 Accessor to related attribute results.
442  
443 =cut
444  
445 2 many_to_many
446 attributes => "navigation_attributes", "attribute";
447  
448  
449 2 1;
« Test/Role/Fixtures.pm ↑ Summary Schema/Result/PriceModifier.pm »