| 1 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | |||||||||||||||||||||
| 4 | package Jifty::Action::Record::Create; | ||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | =head1 NAME | ||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | Jifty::Action::Record::Create - Automagic creation action | ||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | =head1 DESCRIPTION | ||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | This class is used as the base class for L<Jifty::Action>s that are | ||||||||||||||||||||
| 13 | merely creating L<Jifty::Record> objects. To use it, subclass it and | ||||||||||||||||||||
| 14 | override the C<record_class> method to return the name of the | ||||||||||||||||||||
| 15 | L<Jifty::Record> subclass that this action creates. | ||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | =cut | ||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | 1 | ▶ | use base qw/Jifty::Action::Record/; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | 1 | ▶ | use Hash::Merge; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | =head1 METHODS | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | =head2 arguments | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | Set the default value in each of the fields to whatever the default of | ||||||||||||||||||||
| 28 | the column is in the model | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | =cut | ||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | 0 | ▶ | sub arguments { | ||||||||||||||||||
|
Subroutine
arguments: not called CC=6 SCAR=33.7
Pod
documented
| |||||||||||||||||||||
| 33 | 0 | my $self = shift; | |||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | # Add default values to the arguments configured by Jifty::Action::Record | ||||||||||||||||||||
| 36 | 0 | my $args = $self->SUPER::arguments; | |||||||||||||||||||
| 37 | 0 | for my $arg ( keys %{$args} ) { | |||||||||||||||||||
| 38 | 0 | ▶ | unless ( $args->{$arg}->{default_value} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 39 | 0 | my $column = $self->record->column($arg); | |||||||||||||||||||
| 40 | 0 | ▶ | next if not $column; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 41 | 0 | $args->{$arg}{default_value} = $column->default; | |||||||||||||||||||
| 42 | } | ||||||||||||||||||||
| 43 | } | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | 0 | ▶ | if ( $self->can('PARAMS') ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 46 | 1 | ▶ | use Jifty::Param::Schema; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 47 | 0 | ▶ | return Jifty::Param::Schema::merge_params( | ||||||||||||||||||
|
Condition0%0/2
$self->PARAMS || {}
MC/DC0%0/1
$self->PARAMS || {}
$self->PARAMS
Truth table0%0/2
A
A= $self->PARAMS
| |||||||||||||||||||||
| 48 | $args, ($self->PARAMS || {}) | ||||||||||||||||||||
| 49 | ); | ||||||||||||||||||||
| 50 | } | ||||||||||||||||||||
| 51 | else { | ||||||||||||||||||||
| 52 | 0 | return $args; | |||||||||||||||||||
| 53 | } | ||||||||||||||||||||
| 54 | } | ||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | =head2 take_action | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | Overrides the virtual C<take_action> method on L<Jifty::Action> to call | ||||||||||||||||||||
| 59 | the appropriate C<Jifty::Record>'s C<create> method when the action is | ||||||||||||||||||||
| 60 | run, thus creating a new object in the database. | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | The C<id> of the new row is returned in the C<id> content of the | ||||||||||||||||||||
| 63 | L<Jifty::Result> for the action. You can use this in conjunction with | ||||||||||||||||||||
| 64 | L<request mapping|Jifty::Request::Mapper> in order to give later parts | ||||||||||||||||||||
| 65 | of the request access to the C<id>. | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | =cut | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | 0 | ▶ | sub take_action { | ||||||||||||||||||
|
Subroutine
take_action: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||
| 70 | 0 | my $self = shift; | |||||||||||||||||||
| 71 | 0 | my $record = $self->record; | |||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | 0 | my %values; | |||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | # Iterate through all that are set, except for the virtual ones | ||||||||||||||||||||
| 76 | 0 | ▶ | for (grep { defined $self->argument_value($_) && !$self->arguments->{$_}->{virtual} } $self->argument_names) { | ||||||||||||||||||
|
Condition0%0/3
defined $self->argument_value($_) && !$self->arguments->{$_}{'virtual'}
MC/DC0%0/2
defined $self->argument_value($_) && !$self->arguments->{$_}{'virtual'}
defined $self->argument_value($_)
!$self->arguments->{$_}{'virtual'}
Truth table0%0/3
A && not(B)
A= defined $self->argument_value($_)B= !$self->arguments->{$_}{'virtual'}
| |||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | # Prepare the hash to pass to create for each argument | ||||||||||||||||||||
| 79 | 0 | $values{$_} = $self->argument_value($_); | |||||||||||||||||||
| 80 | } | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | # Attempt creating the record | ||||||||||||||||||||
| 83 | 0 | my $id; | |||||||||||||||||||
| 84 | 0 | my $msg = $self->create_record(%values); | |||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | # Convert Class::ReturnValue to an id and message | ||||||||||||||||||||
| 87 | 0 | ▶ | if (ref($msg)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 88 | 0 | ($id,$msg) = $msg->as_array; | |||||||||||||||||||
| 89 | } | ||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | # If ID is 0/undef, the record didn't create, so we fail | ||||||||||||||||||||
| 92 | 0 | ▶ | if (! $record->id ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 93 | 0 | $self->log->warn(_("Create of %1 failed: %2", ref($record), $msg)); | |||||||||||||||||||
| 94 | 0 | ▶ | $self->result->error($msg || _("An error occurred. Try again later")); | ||||||||||||||||||
|
Condition0%0/3
$msg || _('An error occurred. Try again later')
MC/DC0%0/2
$msg || _('An error occurred. Try again later')
$msg
_('An error occurred. Try again later')
Truth table0%0/3
A || B
A= $msgB= _('An error occurred. Try again later')
| |||||||||||||||||||||
| 95 | } | ||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | # No errors! Report success | ||||||||||||||||||||
| 98 | else { | ||||||||||||||||||||
| 99 | # Return the id that we created | ||||||||||||||||||||
| 100 | 0 | $self->result->content(id => $self->record->id); | |||||||||||||||||||
| 101 | 0 | ▶ | $self->report_success if not $self->result->failure; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 102 | } | ||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | 0 | return ($self->record->id); | |||||||||||||||||||
| 105 | } | ||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | =head2 create_record | ||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | This method actually performs the call to C<< record->create >>. It receives as | ||||||||||||||||||||
| 110 | arguments the parameter hash and should return the message for the user, | ||||||||||||||||||||
| 111 | indicating success or failure. | ||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | =cut | ||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | 0 | ▶ | sub create_record { | ||||||||||||||||||
|
Subroutine
create_record: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 116 | 0 | my $self = shift; | |||||||||||||||||||
| 117 | 0 | my $record = $self->record; | |||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | 0 | return $record->create(@_); | |||||||||||||||||||
| 120 | } | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | =head2 report_success | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | Sets the L<Jifty::Result/message> to default success message, | ||||||||||||||||||||
| 125 | "Created". Override this if you want to report some other | ||||||||||||||||||||
| 126 | more user-friendly result. | ||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | =cut | ||||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | 0 | ▶ | sub report_success { | ||||||||||||||||||
|
Subroutine
report_success: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 131 | 0 | my $self = shift; | |||||||||||||||||||
| 132 | 0 | $self->result->message(_("Created")) | |||||||||||||||||||
| 133 | } | ||||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | =head2 possible_columns | ||||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | Create actions do not provide fields for columns marked as C<private> | ||||||||||||||||||||
| 138 | or C<protected>. | ||||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | =cut | ||||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | 0 | ▶ | sub possible_columns { | ||||||||||||||||||
|
Subroutine
possible_columns: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 143 | 0 | my $self = shift; | |||||||||||||||||||
| 144 | 0 | return grep {not $_->protected} $self->SUPER::possible_columns( @_ ); | |||||||||||||||||||
| 145 | } | ||||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | =head2 _extra_validator_args | ||||||||||||||||||||
| 148 | |||||||||||||||||||||
| 149 | Passes C<< for => 'create' >> to validators. | ||||||||||||||||||||
| 150 | |||||||||||||||||||||
| 151 | =cut | ||||||||||||||||||||
| 152 | |||||||||||||||||||||
| 153 | 0 | ▶ | sub _extra_validator_args { | ||||||||||||||||||
|
Subroutine
_extra_validator_args: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 154 | 0 | return { for => 'create' }; | |||||||||||||||||||
| 155 | } | ||||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | =head2 _extra_canonicalizer_args | ||||||||||||||||||||
| 158 | |||||||||||||||||||||
| 159 | Passes C<< for => 'create' >> to canonicalizers. | ||||||||||||||||||||
| 160 | |||||||||||||||||||||
| 161 | =cut | ||||||||||||||||||||
| 162 | |||||||||||||||||||||
| 163 | 0 | ▶ | sub _extra_canonicalizer_args { | ||||||||||||||||||
|
Subroutine
_extra_canonicalizer_args: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 164 | 0 | return { for => 'create' }; | |||||||||||||||||||
| 165 | } | ||||||||||||||||||||
| 166 | |||||||||||||||||||||
| 167 | =head2 _extra_autocompleter_args | ||||||||||||||||||||
| 168 | |||||||||||||||||||||
| 169 | Passes C<< for => 'create' >> to autocompleters. | ||||||||||||||||||||
| 170 | |||||||||||||||||||||
| 171 | =cut | ||||||||||||||||||||
| 172 | |||||||||||||||||||||
| 173 | 0 | ▶ | sub _extra_autocompleter_args { | ||||||||||||||||||
|
Subroutine
_extra_autocompleter_args: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 174 | 0 | return { for => 'create' }; | |||||||||||||||||||
| 175 | } | ||||||||||||||||||||
| 176 | |||||||||||||||||||||
| 177 | =head1 SEE ALSO | ||||||||||||||||||||
| 178 | |||||||||||||||||||||
| 179 | L<Jifty::Action::Record>, L<Jifty::Record> | ||||||||||||||||||||
| 180 | |||||||||||||||||||||
| 181 | =head1 LICENSE | ||||||||||||||||||||
| 182 | |||||||||||||||||||||
| 183 | Jifty is Copyright 2005-2010 Best Practical Solutions, LLC. | ||||||||||||||||||||
| 184 | Jifty is distributed under the same terms as Perl itself. | ||||||||||||||||||||
| 185 | |||||||||||||||||||||
| 186 | =cut | ||||||||||||||||||||
| 187 | |||||||||||||||||||||
| 188 | 1 | 1; | |||||||||||||||||||