| 1 | 2 | ▶ | use utf8; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 2 | |||||||||||
| 3 | package Interchange6::Schema::Result::Message; | ||||||||||
| 4 | |||||||||||
| 5 | =head1 NAME | ||||||||||
| 6 | |||||||||||
| 7 | Interchange6::Schema::Result::Message | ||||||||||
| 8 | |||||||||||
| 9 | =cut | ||||||||||
| 10 | |||||||||||
| 11 | 2 | use Interchange6::Schema::Candy -components => | |||||||||
| 12 | 2 | ▶ | [qw(Tree::AdjacencyList InflateColumn::DateTime TimeStamp)]; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | 2 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | |||||||||||
| 16 | =head1 DESCRIPTION | ||||||||||
| 17 | |||||||||||
| 18 | Shared messages table for blog, order comments, reviews, bb, etc. | ||||||||||
| 19 | |||||||||||
| 20 | =head1 ACCESSORS | ||||||||||
| 21 | |||||||||||
| 22 | =head2 type | ||||||||||
| 23 | |||||||||||
| 24 | A short-cut accessor which takes a message type name (L<Interchange6::Schema::Result::MessageType/name>) as argument and sets L</message_types_id> to the appropriate value; | ||||||||||
| 25 | |||||||||||
| 26 | =cut | ||||||||||
| 27 | |||||||||||
| 28 | 2 | has type => ( is => 'rw', ); | |||||||||
| 29 | |||||||||||
| 30 | =head2 messages_id | ||||||||||
| 31 | |||||||||||
| 32 | Primary key. | ||||||||||
| 33 | |||||||||||
| 34 | =cut | ||||||||||
| 35 | |||||||||||
| 36 | 2 | primary_column messages_id => { | |||||||||
| 37 | data_type => "integer", | ||||||||||
| 38 | is_auto_increment => 1, | ||||||||||
| 39 | }; | ||||||||||
| 40 | |||||||||||
| 41 | =head2 title | ||||||||||
| 42 | |||||||||||
| 43 | The title of the message. | ||||||||||
| 44 | |||||||||||
| 45 | =cut | ||||||||||
| 46 | |||||||||||
| 47 | 2 | column title => { | |||||||||
| 48 | data_type => "varchar", | ||||||||||
| 49 | default_value => "", | ||||||||||
| 50 | size => 255 | ||||||||||
| 51 | }; | ||||||||||
| 52 | |||||||||||
| 53 | =head2 message_types_id | ||||||||||
| 54 | |||||||||||
| 55 | Foreign key constraint on L<Interchange6::Schema::Result::MessageType/message_types_id> | ||||||||||
| 56 | via L</message_type> relationship. | ||||||||||
| 57 | |||||||||||
| 58 | =cut | ||||||||||
| 59 | |||||||||||
| 60 | 2 | column message_types_id => { | |||||||||
| 61 | data_type => "integer", | ||||||||||
| 62 | is_foreign_key => 1, | ||||||||||
| 63 | }; | ||||||||||
| 64 | |||||||||||
| 65 | =head2 uri | ||||||||||
| 66 | |||||||||||
| 67 | The uri of the message data. | ||||||||||
| 68 | |||||||||||
| 69 | =cut | ||||||||||
| 70 | |||||||||||
| 71 | 2 | column uri => { | |||||||||
| 72 | data_type => "varchar", | ||||||||||
| 73 | is_nullable => 1, | ||||||||||
| 74 | size => 255 | ||||||||||
| 75 | }; | ||||||||||
| 76 | |||||||||||
| 77 | =head2 format | ||||||||||
| 78 | |||||||||||
| 79 | The format of the text held in L</content>, e.g. plain, html or markdown. | ||||||||||
| 80 | Defaults to 'plain'. | ||||||||||
| 81 | |||||||||||
| 82 | =cut | ||||||||||
| 83 | |||||||||||
| 84 | 2 | column format => { | |||||||||
| 85 | data_type => "varchar", | ||||||||||
| 86 | size => 32, | ||||||||||
| 87 | default_value => "plain", | ||||||||||
| 88 | }; | ||||||||||
| 89 | |||||||||||
| 90 | =head2 content | ||||||||||
| 91 | |||||||||||
| 92 | Content for the message. | ||||||||||
| 93 | |||||||||||
| 94 | =cut | ||||||||||
| 95 | |||||||||||
| 96 | 2 | column content => { | |||||||||
| 97 | data_type => "text" | ||||||||||
| 98 | }; | ||||||||||
| 99 | |||||||||||
| 100 | =head2 author_users_id | ||||||||||
| 101 | |||||||||||
| 102 | Foreign key constraint on L<Interchange6::Schema::Result::User/users_id> | ||||||||||
| 103 | via L</author> relationship. Is nullable. | ||||||||||
| 104 | |||||||||||
| 105 | =cut | ||||||||||
| 106 | |||||||||||
| 107 | 2 | column author_users_id => { | |||||||||
| 108 | data_type => "integer", | ||||||||||
| 109 | is_foreign_key => 1, | ||||||||||
| 110 | is_nullable => 1 | ||||||||||
| 111 | }; | ||||||||||
| 112 | |||||||||||
| 113 | =head2 rating | ||||||||||
| 114 | |||||||||||
| 115 | Numeric rating of the message by a user. | ||||||||||
| 116 | |||||||||||
| 117 | =cut | ||||||||||
| 118 | |||||||||||
| 119 | 2 | column rating => { | |||||||||
| 120 | data_type => "numeric", | ||||||||||
| 121 | default_value => 0, | ||||||||||
| 122 | size => [ 4, 2 ], | ||||||||||
| 123 | }; | ||||||||||
| 124 | |||||||||||
| 125 | =head2 recommend | ||||||||||
| 126 | |||||||||||
| 127 | Do you recommend the message? Default is no. Is nullable. | ||||||||||
| 128 | |||||||||||
| 129 | =cut | ||||||||||
| 130 | |||||||||||
| 131 | 2 | column recommend => { | |||||||||
| 132 | data_type => "boolean", | ||||||||||
| 133 | is_nullable => 1 | ||||||||||
| 134 | }; | ||||||||||
| 135 | |||||||||||
| 136 | =head2 public | ||||||||||
| 137 | |||||||||||
| 138 | Is this public viewable? Default is no. | ||||||||||
| 139 | |||||||||||
| 140 | =cut | ||||||||||
| 141 | |||||||||||
| 142 | 2 | column public => { | |||||||||
| 143 | data_type => "boolean", | ||||||||||
| 144 | default_value => 0, | ||||||||||
| 145 | }; | ||||||||||
| 146 | |||||||||||
| 147 | =head2 approved | ||||||||||
| 148 | |||||||||||
| 149 | Has this been approved by someone with proper rights? | ||||||||||
| 150 | |||||||||||
| 151 | =cut | ||||||||||
| 152 | |||||||||||
| 153 | 2 | column approved => { | |||||||||
| 154 | data_type => "boolean", | ||||||||||
| 155 | default_value => 0, | ||||||||||
| 156 | }; | ||||||||||
| 157 | |||||||||||
| 158 | =head2 approved_by_users_id | ||||||||||
| 159 | |||||||||||
| 160 | Foreign key constraint on L<Interchange6::Schema::Result::User/users_id> | ||||||||||
| 161 | via L</approved_by> relationship. Is nullable | ||||||||||
| 162 | |||||||||||
| 163 | =cut | ||||||||||
| 164 | |||||||||||
| 165 | 2 | column approved_by_users_id => { | |||||||||
| 166 | data_type => "integer", | ||||||||||
| 167 | is_foreign_key => 1, | ||||||||||
| 168 | is_nullable => 1 | ||||||||||
| 169 | }; | ||||||||||
| 170 | |||||||||||
| 171 | =head2 parent_id | ||||||||||
| 172 | |||||||||||
| 173 | For use by L<DBIx::Class::Tree::AdjacencyList> this defines the L</messages_id> | ||||||||||
| 174 | of the parent of this message (if any). | ||||||||||
| 175 | |||||||||||
| 176 | =cut | ||||||||||
| 177 | |||||||||||
| 178 | 2 | column parent_id => { data_type => "integer", is_nullable => 1 }; | |||||||||
| 179 | |||||||||||
| 180 | =head2 created | ||||||||||
| 181 | |||||||||||
| 182 | Date and time when this record was created returned as L<DateTime> object. | ||||||||||
| 183 | Value is auto-set on insert. | ||||||||||
| 184 | |||||||||||
| 185 | =cut | ||||||||||
| 186 | |||||||||||
| 187 | 2 | column created => { | |||||||||
| 188 | data_type => "datetime", | ||||||||||
| 189 | set_on_create => 1, | ||||||||||
| 190 | }; | ||||||||||
| 191 | |||||||||||
| 192 | =head2 last_modified | ||||||||||
| 193 | |||||||||||
| 194 | Date and time when this record was last modified returned as L<DateTime> object. | ||||||||||
| 195 | Value is auto-set on insert and update. | ||||||||||
| 196 | |||||||||||
| 197 | =cut | ||||||||||
| 198 | |||||||||||
| 199 | 2 | column last_modified => { | |||||||||
| 200 | data_type => "datetime", | ||||||||||
| 201 | set_on_create => 1, | ||||||||||
| 202 | set_on_update => 1, | ||||||||||
| 203 | }; | ||||||||||
| 204 | |||||||||||
| 205 | =head1 RELATIONS | ||||||||||
| 206 | |||||||||||
| 207 | =head2 author | ||||||||||
| 208 | |||||||||||
| 209 | Type: belongs_to | ||||||||||
| 210 | |||||||||||
| 211 | Related object: L<Interchange6::Schema::Result::User> | ||||||||||
| 212 | |||||||||||
| 213 | =cut | ||||||||||
| 214 | |||||||||||
| 215 | 2 | belongs_to | |||||||||
| 216 | author => 'Interchange6::Schema::Result::User', | ||||||||||
| 217 | { 'foreign.users_id' => 'self.author_users_id' }, | ||||||||||
| 218 | { join_type => 'left' }; | ||||||||||
| 219 | |||||||||||
| 220 | =head2 approved_by | ||||||||||
| 221 | |||||||||||
| 222 | Type: belongs_to | ||||||||||
| 223 | |||||||||||
| 224 | Related object: L<Interchange6::Schema::Result::User> | ||||||||||
| 225 | |||||||||||
| 226 | =cut | ||||||||||
| 227 | |||||||||||
| 228 | 2 | belongs_to | |||||||||
| 229 | approved_by => 'Interchange6::Schema::Result::User', | ||||||||||
| 230 | { 'foreign.users_id' => 'self.approved_by_users_id' }, | ||||||||||
| 231 | { join_type => 'left' }; | ||||||||||
| 232 | |||||||||||
| 233 | =head2 message_type | ||||||||||
| 234 | |||||||||||
| 235 | Type: belongs_to | ||||||||||
| 236 | |||||||||||
| 237 | Related object: L<Interchange6::Schema::Result::MessageType> | ||||||||||
| 238 | |||||||||||
| 239 | =cut | ||||||||||
| 240 | |||||||||||
| 241 | 2 | belongs_to | |||||||||
| 242 | message_type => 'Interchange6::Schema::Result::MessageType', | ||||||||||
| 243 | 'message_types_id'; | ||||||||||
| 244 | |||||||||||
| 245 | =head2 order_comment | ||||||||||
| 246 | |||||||||||
| 247 | Type: might_have | ||||||||||
| 248 | |||||||||||
| 249 | Related object: L<Interchange6::Schema::Result::OrderComment> | ||||||||||
| 250 | |||||||||||
| 251 | =cut | ||||||||||
| 252 | |||||||||||
| 253 | 2 | might_have | |||||||||
| 254 | order_comment => 'Interchange6::Schema::Result::OrderComment', | ||||||||||
| 255 | 'messages_id'; | ||||||||||
| 256 | |||||||||||
| 257 | =head2 orders | ||||||||||
| 258 | |||||||||||
| 259 | Type: many_to_many | ||||||||||
| 260 | |||||||||||
| 261 | Accessor to related Product results. | ||||||||||
| 262 | |||||||||||
| 263 | =cut | ||||||||||
| 264 | |||||||||||
| 265 | 2 | many_to_many orders => "order_comment", "order"; | |||||||||
| 266 | |||||||||||
| 267 | =head2 product_review | ||||||||||
| 268 | |||||||||||
| 269 | Type: might_have | ||||||||||
| 270 | |||||||||||
| 271 | Related object: L<Interchange6::Schema::Result::ProductReview> | ||||||||||
| 272 | |||||||||||
| 273 | =cut | ||||||||||
| 274 | |||||||||||
| 275 | 2 | might_have | |||||||||
| 276 | product_review => 'Interchange6::Schema::Result::ProductReview', | ||||||||||
| 277 | 'messages_id', | ||||||||||
| 278 | { join_type => 'inner' }; | ||||||||||
| 279 | |||||||||||
| 280 | =head2 products | ||||||||||
| 281 | |||||||||||
| 282 | Type: many_to_many | ||||||||||
| 283 | |||||||||||
| 284 | Accessor to related Product results. | ||||||||||
| 285 | |||||||||||
| 286 | =cut | ||||||||||
| 287 | |||||||||||
| 288 | 2 | many_to_many products => "product_review", "product"; | |||||||||
| 289 | |||||||||||
| 290 | =head1 INHERITED METHODS | ||||||||||
| 291 | |||||||||||
| 292 | =head2 DBIx::Class::Tree::AdjacencyList | ||||||||||
| 293 | |||||||||||
| 294 | =over 4 | ||||||||||
| 295 | |||||||||||
| 296 | =item * | ||||||||||
| 297 | |||||||||||
| 298 | L<parent|DBIx::Class::Tree::AdjacencyList/parent> | ||||||||||
| 299 | |||||||||||
| 300 | =item * | ||||||||||
| 301 | |||||||||||
| 302 | L<ancestors|DBIx::Class::Tree::AdjacencyList/ancestors> | ||||||||||
| 303 | |||||||||||
| 304 | =item * | ||||||||||
| 305 | |||||||||||
| 306 | L<has_descendant|DBIx::Class::Tree::AdjacencyList/has_descendant> | ||||||||||
| 307 | |||||||||||
| 308 | =item * | ||||||||||
| 309 | |||||||||||
| 310 | L<parents|DBIx::Class::Tree::AdjacencyList/parents> | ||||||||||
| 311 | |||||||||||
| 312 | =item * | ||||||||||
| 313 | |||||||||||
| 314 | L<children|DBIx::Class::Tree::AdjacencyList/children> | ||||||||||
| 315 | |||||||||||
| 316 | =item * | ||||||||||
| 317 | |||||||||||
| 318 | L<attach_child|DBIx::Class::Tree::AdjacencyList/attach_child> | ||||||||||
| 319 | |||||||||||
| 320 | =item * | ||||||||||
| 321 | |||||||||||
| 322 | L<siblings|DBIx::Class::Tree::AdjacencyList/siblings> | ||||||||||
| 323 | |||||||||||
| 324 | =item * | ||||||||||
| 325 | |||||||||||
| 326 | L<attach_sibling|DBIx::Class::Tree::AdjacencyList/attach_sibling> | ||||||||||
| 327 | |||||||||||
| 328 | =item * | ||||||||||
| 329 | |||||||||||
| 330 | L<is_leaf|DBIx::Class::Tree::AdjacencyList/is_leaf> | ||||||||||
| 331 | |||||||||||
| 332 | =item * | ||||||||||
| 333 | |||||||||||
| 334 | L<is_root|DBIx::Class::Tree::AdjacencyList/is_root> | ||||||||||
| 335 | |||||||||||
| 336 | =item * | ||||||||||
| 337 | |||||||||||
| 338 | L<is_branch|DBIx::Class::Tree::AdjacencyList/is_branch> | ||||||||||
| 339 | |||||||||||
| 340 | =back | ||||||||||
| 341 | |||||||||||
| 342 | =cut | ||||||||||
| 343 | |||||||||||
| 344 | # define parent column | ||||||||||
| 345 | |||||||||||
| 346 | 2 | __PACKAGE__->parent_column('parent_id'); | |||||||||
| 347 | |||||||||||
| 348 | =head1 METHODS | ||||||||||
| 349 | |||||||||||
| 350 | =head2 FOREIGNBUILDARGS | ||||||||||
| 351 | |||||||||||
| 352 | Remove L</type> attribute from call to parent class. | ||||||||||
| 353 | |||||||||||
| 354 | =cut | ||||||||||
| 355 | |||||||||||
| 356 | sub FOREIGNBUILDARGS { | ||||||||||
| 357 | 90 | ▶ | my ( $self, $attrs ) = @_; | ||||||||
|
Subroutine
FOREIGNBUILDARGS: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 358 | |||||||||||
| 359 | 90 | ▶ | if ( defined $attrs->{type} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 360 | 88 | delete $attrs->{type}; | |||||||||
| 361 | } | ||||||||||
| 362 | 90 | return $attrs; | |||||||||
| 363 | } | ||||||||||
| 364 | |||||||||||
| 365 | =head2 insert | ||||||||||
| 366 | |||||||||||
| 367 | Overload insert to set message_types_id if required. Throw exception if requested message type | ||||||||||
| 368 | is not active. See L<Interchange6::Schema::Result::MessageType/active>. | ||||||||||
| 369 | |||||||||||
| 370 | =cut | ||||||||||
| 371 | |||||||||||
| 372 | sub insert { | ||||||||||
| 373 | 90 | ▶ | my $self = shift; | ||||||||
|
Subroutine
insert: called CC=8 SCAR=21.8
Pod
documented
| |||||||||||
| 374 | |||||||||||
| 375 | 90 | my $rset_message_type = | |||||||||
| 376 | $self->result_source->schema->resultset("MessageType"); | ||||||||||
| 377 | |||||||||||
| 378 | 90 | ▶ | if ( defined $self->type ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 379 | |||||||||||
| 380 | 88 | my $name = $self->type; | |||||||||
| 381 | |||||||||||
| 382 | 88 | ▶ | if ( defined $self->message_types_id ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 383 | 0 | ▶ | $self->throw_exception("mismatched type settings") | ||||||||
|
Branch: 1 branch
| |||||||||||
| 384 | if $name ne $self->message_type->name; | ||||||||||
| 385 | } | ||||||||||
| 386 | else { | ||||||||||
| 387 | |||||||||||
| 388 | 88 | my $rset = $rset_message_type->search( { name => $self->type } ); | |||||||||
| 389 | |||||||||||
| 390 | 88 | ▶ | if ( $rset->has_rows ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 391 | 88 | my $result = $rset->next; | |||||||||
| 392 | 88 | $self->set_column( message_types_id => $result->id ); | |||||||||
| 393 | } | ||||||||||
| 394 | else { | ||||||||||
| 395 | 0 | $self->throw_exception( | |||||||||
| 396 | qq(MessageType with name "$name" does not exist)); | ||||||||||
| 397 | } | ||||||||||
| 398 | } | ||||||||||
| 399 | } | ||||||||||
| 400 | |||||||||||
| 401 | 90 | ▶ | if ( defined $self->message_types_id ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 402 | |||||||||||
| 403 | # make sure message type is active | ||||||||||
| 404 | |||||||||||
| 405 | 88 | my $rset = $rset_message_type->search( | |||||||||
| 406 | { message_types_id => $self->message_types_id } ); | ||||||||||
| 407 | |||||||||||
| 408 | 88 | ▶ | if ( $rset->has_rows ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 409 | 88 | my $result = $rset->next; | |||||||||
| 410 | 88 | ▶ | if ( $result->active ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 411 | 86 | return $self->next::method; | |||||||||
| 412 | } | ||||||||||
| 413 | else { | ||||||||||
| 414 | 2 | $self->throw_exception( q(MessageType with name ") | |||||||||
| 415 | . $result->name | ||||||||||
| 416 | . q(" is not active) ); | ||||||||||
| 417 | } | ||||||||||
| 418 | } | ||||||||||
| 419 | else { | ||||||||||
| 420 | 0 | $self->throw_exception( | |||||||||
| 421 | q(message_types_id value does not exist in MessageType)); | ||||||||||
| 422 | } | ||||||||||
| 423 | } | ||||||||||
| 424 | else { | ||||||||||
| 425 | 2 | $self->throw_exception("Cannot create message without type"); | |||||||||
| 426 | } | ||||||||||
| 427 | } | ||||||||||
| 428 | |||||||||||
| 429 | 2 | 1; | |||||||||