| 1 | package HTML::FormFu::Constraint::DBIC::Unique; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 2 | our $VERSION = '2.03'; # VERSION | |||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Moose; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use MooseX::Attribute::Chained; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||
| 9 | 2 | extends 'HTML::FormFu::Constraint'; | |||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use Carp qw( carp croak ); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use HTML::FormFu::Util qw( DEBUG_CONSTRAINTS debug ); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||
| 15 | 2 | has model => ( is => 'rw', traits => ['Chained'] ); | |||||||||||||||||||||||||||||||
| 16 | 2 | has resultset => ( is => 'rw', traits => ['Chained'] ); | |||||||||||||||||||||||||||||||
| 17 | 2 | has column => ( is => 'rw', traits => ['Chained'] ); | |||||||||||||||||||||||||||||||
| 18 | 2 | has method_name => ( is => 'rw', traits => ['Chained'] ); | |||||||||||||||||||||||||||||||
| 19 | 2 | has self_stash_key => ( is => 'rw', traits => ['Chained'] ); | |||||||||||||||||||||||||||||||
| 20 | 2 | has others => ( is => 'rw', traits => ['Chained'] ); | |||||||||||||||||||||||||||||||
| 21 | 2 | has id_field => ( is => 'rw', traits => ['Chained'] ); | |||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||
| 23 | 16 | ▶ | sub constrain_value { | ||||||||||||||||||||||||||||||
|
Subroutine
constrain_value: called CC=27 SCAR=42.6
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 24 | 16 | my ( $self, $value ) = @_; | |||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||
| 26 | 16 | ▶ | return 1 if !defined $value || $value eq ''; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $value or $value eq ""
MC/DC0%0/2
not defined $value or $value eq ""
not defined $value
$value eq ""
Truth table33%1/3
not(A) or B
A= not defined $valueB= $value eq ""
| |||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||
| 28 | 16 | for (qw/ resultset /) { | |||||||||||||||||||||||||||||||
| 29 | 16 | ▶ | if ( !defined $self->$_ ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 30 | # warn and die, as errors are swallowed by HTML-FormFu | ||||||||||||||||||||||||||||||||
| 31 | 0 | carp "'$_' is not defined"; | |||||||||||||||||||||||||||||||
| 32 | 0 | croak "'$_' is not defined"; | |||||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||
| 36 | # get stash | ||||||||||||||||||||||||||||||||
| 37 | 16 | my $stash = $self->form->stash; | |||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||
| 39 | 16 | my $schema; | |||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||
| 41 | 16 | ▶ | if ( defined $stash->{schema} ) { | ||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||
| 42 | 16 | $schema = $stash->{schema}; | |||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||
| 44 | ▶ | elsif ( defined $stash->{context} && defined $self->model ) { | |||||||||||||||||||||||||||||||
|
Condition0%0/3
defined $stash->{"context"} and defined $self->model
MC/DC0%0/2
defined $stash->{"context"} and defined $self->model
defined $stash->{"context"}
defined $self->model
Truth table0%0/3
A and B
A= defined $stash->{"context"}B= defined $self->model
| |||||||||||||||||||||||||||||||||
| 45 | 0 | $schema = $stash->{context}->model( $self->model ); | |||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||
| 47 | elsif ( defined $stash->{context} ) { | ||||||||||||||||||||||||||||||||
| 48 | 0 | $schema = $stash->{context}->model; | |||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||
| 51 | 16 | ▶ | if ( !defined $schema ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 52 | # warn and die, as errors are swallowed by HTML-FormFu | ||||||||||||||||||||||||||||||||
| 53 | 0 | carp 'could not find DBIC schema'; | |||||||||||||||||||||||||||||||
| 54 | 0 | croak 'could not find DBIC schema'; | |||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||
| 57 | 16 | my $resultset = $schema->resultset( $self->resultset ); | |||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||
| 59 | 16 | ▶ | if ( !defined $resultset ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 60 | # warn and die, as errors are swallowed by HTML-FormFu | ||||||||||||||||||||||||||||||||
| 61 | 0 | carp 'could not find DBIC resultset'; | |||||||||||||||||||||||||||||||
| 62 | 0 | croak 'could not find DBIC resultset'; | |||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||
| 65 | 16 | ▶ | if ( my $method_name = $self->method_name ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 66 | # warn "using $method_name to look for $value"; | ||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||
| 68 | # need to be able to tell $method_name about record on the form stash | ||||||||||||||||||||||||||||||||
| 69 | 3 | my $pk_val; | |||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||
| 71 | 3 | ▶ | if ( defined( my $self_stash_key = $self->self_stash_key ) ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||
| 73 | 3 | ▶ | if ( defined( my $self_stash = $stash->{ $self_stash_key } ) ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||
| 75 | 3 | my ($pk) = $resultset->result_source->primary_columns; | |||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||
| 77 | 3 | $pk_val = $self_stash->$pk; | |||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||
| 81 | 3 | return $resultset->$method_name( $value, $pk_val ); | |||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||
| 83 | else { | ||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||
| 85 | 13 | ▶ | my $column = $self->column || $self->parent->name; | ||||||||||||||||||||||||||||||
|
Condition66%2/3
$self->column || $self->parent->name
MC/DC0%0/2
$self->column || $self->parent->name
$self->column
$self->parent->name
Truth table66%2/3
A || B
A= $self->columnB= $self->parent->name
| |||||||||||||||||||||||||||||||||
| 86 | 13 | my %others; | |||||||||||||||||||||||||||||||
| 87 | 13 | ▶ | if ( $self->others ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 88 | 0 | ▶ | my @others = ref $self->others ? @{ $self->others } | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 89 | : $self->others; | ||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||
| 91 | 2 | my $params = $self->form->input; | |||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||
| 93 | %others = | ||||||||||||||||||||||||||||||||
| 94 | grep { | ||||||||||||||||||||||||||||||||
| 95 | defined && length | ||||||||||||||||||||||||||||||||
| 96 | 4 | ▶ | } | ||||||||||||||||||||||||||||||
|
Condition33%1/3
defined $_ && length $_
MC/DC0%0/2
defined $_ && length $_
defined $_
length $_
Truth table33%1/3
A && B
A= defined $_B= length $_
| |||||||||||||||||||||||||||||||||
| 97 | map { | ||||||||||||||||||||||||||||||||
| 98 | 2 | $_ => $self->get_nested_hash_value( $params, $_ ) | |||||||||||||||||||||||||||||||
| 99 | } @others; | ||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||
| 103 | 13 | my $existing_row = eval { | |||||||||||||||||||||||||||||||
| 104 | 13 | $resultset->find( { %others, $column => $value } ); | |||||||||||||||||||||||||||||||
| 105 | }; | ||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||
| 107 | 13 | ▶ | if ( my $error = $@ ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 108 | # warn and die, as errors are swallowed by HTML-FormFu | ||||||||||||||||||||||||||||||||
| 109 | 0 | carp $error; | |||||||||||||||||||||||||||||||
| 110 | 0 | croak $error; | |||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||
| 113 | # if a row exists, first check whether it matches a known object on the | ||||||||||||||||||||||||||||||||
| 114 | # form stash | ||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||
| 116 | 13 | ▶ | if ( $existing_row && defined( my $self_stash_key = $self->self_stash_key ) ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
$existing_row and defined(my $self_stash_key = $self->self_stash_key)
MC/DC0%0/2
$existing_row and defined(my $self_stash_key = $self->self_stash_key)
$existing_row
defined(my $self_stash_key = $self->self_stash_key)
Truth table66%2/3
A and B
A= $existing_rowB= defined(my $self_stash_key = $self->self_stash_key)
| |||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||
| 118 | 0 | ▶ | if ( defined( my $self_stash = $stash->{ $self_stash_key } ) ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||
| 120 | 0 | my ($pk) = $resultset->result_source->primary_columns; | |||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||
| 122 | 0 | ▶ | if ( $existing_row->$pk eq $self_stash->$pk ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 123 | 0 | return 1; | |||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||
| 127 | ▶ | elsif ( $existing_row && defined (my $id_field = $self->id_field ) ) { | |||||||||||||||||||||||||||||||
|
Condition100%3/3
$existing_row and defined(my $id_field = $self->id_field)
MC/DC100%2/2
$existing_row and defined(my $id_field = $self->id_field)
$existing_row
defined(my $id_field = $self->id_field)
Truth table100%3/3
A and B
A= $existing_rowB= defined(my $id_field = $self->id_field)
| |||||||||||||||||||||||||||||||||
| 128 | 6 | my $value = $self->get_nested_hash_value( $self->form->input, $id_field ); | |||||||||||||||||||||||||||||||
| 129 | 6 | ▶ | if ( defined $value && length $value ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $value and length $value
MC/DC100%2/2
defined $value and length $value
defined $value
length $value
Truth table100%3/3
A and B
A= defined $valueB= length $value
| |||||||||||||||||||||||||||||||||
| 130 | 4 | my ($pk) = $resultset->result_source->primary_columns; | |||||||||||||||||||||||||||||||
| 131 | 4 | return ($existing_row->$pk eq $value); | |||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||
| 135 | 9 | return !$existing_row; | |||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||
| 140 | 1 | ▶ | after repeatable_repeat => sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.3
| |||||||||||||||||||||||||||||||||
| 141 | 1 | my ( $self, $repeatable, $new_block ) = @_; | |||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||
| 143 | # rename any 'id_field' fields | ||||||||||||||||||||||||||||||||
| 144 | 1 | ▶ | if ( my $id_field = $self->id_field ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 145 | 1 | my $block_fields = $new_block->get_fields; | |||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||
| 147 | 1 | my $field = $repeatable->get_field_with_original_name( $id_field, $block_fields ); | |||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||
| 149 | 1 | ▶ | if ( defined $field ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 150 | 1 | ▶ | DEBUG_CONSTRAINTS && debug( | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 151 | sprintf "Repeatable renaming constraint 'id_field' '%s' to '%s'", | ||||||||||||||||||||||||||||||||
| 152 | $id_field, | ||||||||||||||||||||||||||||||||
| 153 | $field->nested_name, | ||||||||||||||||||||||||||||||||
| 154 | ); | ||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||
| 156 | 1 | $self->id_field( $field->nested_name ); | |||||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||||||
| 159 | 2 | }; | |||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||
| 161 | 2 | 1; | |||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||
| 163 | __END__ | ||||||||||||||||||||||||||||||||