| Jifty documentation | Contained in the Jifty distribution. |
Jifty::Action::Record::Create - Automagic creation action
This class is used as the base class for Jifty::Actions that are
merely creating Jifty::Record objects. To use it, subclass it and
override the record_class method to return the name of the
Jifty::Record subclass that this action creates.
Set the default value in each of the fields to whatever the default of the column is in the model
Overrides the virtual take_action method on Jifty::Action to call
the appropriate Jifty::Record's create method when the action is
run, thus creating a new object in the database.
The id of the new row is returned in the id content of the
Jifty::Result for the action. You can use this in conjunction with
request mapping in order to give later parts
of the request access to the id.
This method actually performs the call to record->create. It receives as
arguments the parameter hash and should return the message for the user,
indicating success or failure.
Sets the message in Jifty::Result to default success message, "Created". Override this if you want to report some other more user-friendly result.
Create actions do not provide fields for columns marked as private
or protected.
Passes for => 'create' to validators.
Passes for => 'create' to canonicalizers.
Passes for => 'create' to autocompleters.
Jifty is Copyright 2005-2010 Best Practical Solutions, LLC. Jifty is distributed under the same terms as Perl itself.
| Jifty documentation | Contained in the Jifty distribution. |
use warnings; use strict; package Jifty::Action::Record::Create;
use base qw/Jifty::Action::Record/; use Hash::Merge;
sub arguments { my $self = shift; # Add default values to the arguments configured by Jifty::Action::Record my $args = $self->SUPER::arguments; for my $arg ( keys %{$args} ) { unless ( $args->{$arg}->{default_value} ) { my $column = $self->record->column($arg); next if not $column; $args->{$arg}{default_value} = $column->default; } } if ( $self->can('PARAMS') ) { use Jifty::Param::Schema; return Jifty::Param::Schema::merge_params( $args, ($self->PARAMS || {}) ); } else { return $args; } }
sub take_action { my $self = shift; my $record = $self->record; # Build the event to be fired later my $event_info = $self->_setup_event_before_action(); my %values; # Iterate through all that are set, except for the virtual ones for (grep { defined $self->argument_value($_) && !$self->arguments->{$_}->{virtual} } $self->argument_names) { # Prepare the hash to pass to create for each argument $values{$_} = $self->argument_value($_); } # Attempt creating the record my $id; my $msg = $self->create_record(%values); # Convert Class::ReturnValue to an id and message if (ref($msg)) { ($id,$msg) = $msg->as_array; } # If ID is 0/undef, the record didn't create, so we fail if (! $record->id ) { $self->log->warn(_("Create of %1 failed: %2", ref($record), $msg)); $self->result->error($msg || _("An error occurred. Try again later")); } # No errors! Report success else { # Return the id that we created $self->result->content(id => $self->record->id); $self->report_success if not $self->result->failure; } # Publish the event, noting success or failure $self->_setup_event_after_action($event_info); return ($self->record->id); }
sub create_record { my $self = shift; my $record = $self->record; return $record->create(@_); }
sub report_success { my $self = shift; $self->result->message(_("Created")) }
sub possible_columns { my $self = shift; return grep {not $_->protected} $self->SUPER::possible_columns( @_ ); }
sub _extra_validator_args { return { for => 'create' }; }
sub _extra_canonicalizer_args { return { for => 'create' }; }
sub _extra_autocompleter_args { return { for => 'create' }; }
1;