lib/Rapi/Blog/DB/Result/Section.pm untested

filter: Statementstmt 0.0% 0 / 108 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 197 CC 20 SCAR 24.9
CC
20
Cov
0%
_validate_depth
30.0
insert
20.0
update
20.0
CRAP 12.1
1 use utf8;
2 package Rapi::Blog::DB::Result::Section;
3  
4 # Created by DBIx::Class::Schema::Loader
5 # DO NOT MODIFY THE FIRST PART OF THIS FILE
6  
7 use strict;
8 use warnings;
9  
10 use Moose;
11 use MooseX::NonMoose;
12 use MooseX::MarkAsMethods autoclean => 1;
13 extends 'DBIx::Class::Core';
14 __PACKAGE__->load_components("InflateColumn::DateTime");
15 __PACKAGE__->table("section");
16 __PACKAGE__->add_columns(
17 "id",
18 { data_type => "integer", is_auto_increment => 1, is_nullable => 0 },
19 "name",
20 { data_type => "varchar", is_nullable => 0, size => 64 },
21 "description",
22 {
23 data_type => "varchar",
24 default_value => \"null",
25 is_nullable => 1,
26 size => 1024,
27 },
28 "parent_id",
29 {
30 data_type => "integer",
31 default_value => \"null",
32 is_foreign_key => 1,
33 is_nullable => 1,
34 },
35 );
36 __PACKAGE__->set_primary_key("id");
37 __PACKAGE__->add_unique_constraint("parent_id_name_unique", ["parent_id", "name"]);
38 __PACKAGE__->belongs_to(
39 "parent",
40 "Rapi::Blog::DB::Result::Section",
41 { id => "parent_id" },
42 {
43 is_deferrable => 0,
44 join_type => "LEFT",
45 on_delete => "CASCADE",
46 on_update => "CASCADE",
47 },
48 );
49 __PACKAGE__->has_many(
50 "posts",
51 "Rapi::Blog::DB::Result::Post",
52 { "foreign.section_id" => "self.id" },
53 { cascade_copy => 0, cascade_delete => 0 },
54 );
55 __PACKAGE__->has_many(
56 "sections",
57 "Rapi::Blog::DB::Result::Section",
58 { "foreign.parent_id" => "self.id" },
59 { cascade_copy => 0, cascade_delete => 0 },
60 );
61 __PACKAGE__->has_many(
62 "trk_section_posts",
63 "Rapi::Blog::DB::Result::TrkSectionPost",
64 { "foreign.section_id" => "self.id" },
65 { cascade_copy => 0, cascade_delete => 0 },
66 );
67 __PACKAGE__->has_many(
68 "trk_section_sections_sections",
69 "Rapi::Blog::DB::Result::TrkSectionSection",
70 { "foreign.section_id" => "self.id" },
71 { cascade_copy => 0, cascade_delete => 0 },
72 );
73 __PACKAGE__->has_many(
74 "trk_section_sections_subsections",
75 "Rapi::Blog::DB::Result::TrkSectionSection",
76 { "foreign.subsection_id" => "self.id" },
77 { cascade_copy => 0, cascade_delete => 0 },
78 );
79  
80  
81 # Created by DBIx::Class::Schema::Loader v0.07045 @ 2017-10-04 18:42:02
82 # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:9NPmvsrCunpxPOYQuwBskg
83  
84 use RapidApp::Util ':all';
85 use Rapi::Blog::Util;
86  
87 __PACKAGE__->load_components('+Rapi::Blog::DB::Component::SafeResult');
88  
89 sub schema { (shift)->result_source->schema }
90  
91 sub insert {
92 my $self = shift;
93 my $columns = shift;
94
95 if(my $User = Rapi::Blog::Util->get_User) {
96 die usererr "Create Section: PERMISSION DENIED" if ($User->id && !$User->admin);
97 }
98
99 $self->set_inflated_columns($columns) if $columns;
100
101 $self->_validate_depth;
102
103 $self->next::method;
104
105 $self->_track_as_subsection;
106
107 $self
108 }
109  
110 sub update {
111 my $self = shift;
112 my $columns = shift;
113
114 if(my $User = Rapi::Blog::Util->get_User) {
115 die usererr "Update Section: PERMISSION DENIED" if ($User->id && !$User->admin);
116 }
117
118 $self->set_inflated_columns($columns) if $columns;
119
120 $self->_validate_depth;
121
122 $self->next::method;
123
124 $self->_track_as_subsection;
125
126 $self
127 }
128  
129  
130 sub delete {
131 my $self = shift;
132
133 if(my $User = Rapi::Blog::Util->get_User) {
134 die usererr "Delete Section: PERMISSION DENIED" if ($User->id && !$User->admin);
135 }
136
137 $self->next::method(@_)
138 }
139  
140  
141 sub parents_name_list {
142 my $self = shift;
143 $self->parent ? ($self->parent->name, $self->parent->parents_name_list) : ()
144 }
145  
146 sub full_path_names {
147 my ($self, $delim) = @_;
148 my @parents = reverse $self->parents_name_list;
149 my @path = ('', @parents, $self->name);
150 return join($delim,@path) if ($delim);
151 return wantarray ? @path : \@path
152 }
153  
154 sub all_section_ids {
155 my $self = shift;
156 my @ids = ( $self->get_column('id') );
157 push @ids, $self->parent->all_section_ids if ($self->parent);
158 @ids
159 }
160  
161  
162 sub _validate_depth {
163 my $self = shift;
164 my $level = shift || 1;
165 my $seen = shift || {};
166
167 my $max_depth = 10;
168
169 if (my $id = $self->get_column('id')) { # may not have an id yet
170 die usererr "Circular references not allowed (cannot make a Section a child of itself)" if ($seen->{$id}++);
171 }
172
173 die usererr "Too many levels of Sections -- max allowed is $max_depth" if ($level > $max_depth);
174
175 $self->get_column('parent_id')
176 ? $self->parent->_validate_depth($level+1,$seen)
177 : 1
178 }
179  
180  
181 sub _track_as_subsection {
182 my $self = (shift)->get_from_storage;
183
184 # Clear all existing rows listing us as a subsection:
185 $self->trk_section_sections_subsections->delete_all;
186 return unless ($self->parent);
187
188 my @ids = $self->parent->all_section_ids;
189 my ($id, $depth) = ($self->get_column('id'), 0);
190  
191 $self->trk_section_sections_subsections->populate([
192 map {{
193 section_id => $_,
194 subsection_id => $id,
195 depth => $depth++
196 }} @ids
197 ])
198 }
199
200  
201 sub posts_count {
202 my $self = shift;
203 # In case the ResultSet has pre-loaded this value, don't do another query:
204 my $preload = try{$self->get_column('posts_count')};
205 defined $preload ? $preload : $self->trk_section_posts->count
206 }
207  
208 sub subsections_count {
209 my $self = shift;
210 # In case the ResultSet has pre-loaded this value, don't do another query:
211 my $preload = try{$self->get_column('subsections_count')};
212 defined $preload ? $preload : $self->trk_section_sections_sections->count
213 }
214  
215  
216 # You can replace this text with custom code or comments, and it will be preserved on regeneration
217 __PACKAGE__->meta->make_immutable;
218 1;
219  
220 __END__