module ActiveScaffold::Actions::Create

Public Class Methods

included(base) click to toggle source
# File lib/active_scaffold/actions/create.rb, line 3
def self.included(base)
  base.before_filter :create_authorized_filter, :only => [:new, :create]
  base.verify :method => :post,
              :only => :create,
              :redirect_to => { :action => :index }
end

Public Instance Methods

create() click to toggle source
# File lib/active_scaffold/actions/create.rb, line 15
def create
  do_create
  respond_to_action(:create)
end
new() click to toggle source
# File lib/active_scaffold/actions/create.rb, line 10
def new
  do_new
  respond_to_action(:new)
end

Protected Instance Methods

after_create_save(record) click to toggle source

override this method if you want to do something after the save

# File lib/active_scaffold/actions/create.rb, line 125
def after_create_save(record); end
before_create_save(record) click to toggle source

override this method if you want to inject data in the record (or its associated objects) before the save

# File lib/active_scaffold/actions/create.rb, line 122
def before_create_save(record); end
create_authorized?() click to toggle source
# File lib/active_scaffold/actions/create.rb, line 134
def create_authorized?
  (!nested? || !nested.readonly?) && authorized_for?(:crud_type => :create)
end
create_ignore?() click to toggle source

The default security delegates to ActiveRecordPermissions. You may override the method to customize.

# File lib/active_scaffold/actions/create.rb, line 130
def create_ignore?
  nested? && active_scaffold_config.list.always_show_create
end
create_respond_to_html() click to toggle source
# File lib/active_scaffold/actions/create.rb, line 37
def create_respond_to_html
  if params[:iframe]=='true' # was this an iframe post ?
    responds_to_parent do
      render :action => 'on_create.js', :layout => false
    end
  else
    if successful?
      flash[:info] = as_(:created_model, :model => @record.to_label)
      if active_scaffold_config.create.edit_after_create
        redirect_to params_for(:action => "edit", :id => @record.id)
      elsif active_scaffold_config.create.persistent
        redirect_to params_for(:action => "new")
      else
        return_to_main
      end
    else
      if !nested? && active_scaffold_config.actions.include?(:list) && active_scaffold_config.list.always_show_create
        do_list
        render(:action => 'list')
      else
        render(:action => 'create')
      end
    end
  end
end
create_respond_to_js() click to toggle source
# File lib/active_scaffold/actions/create.rb, line 63
def create_respond_to_js
  if successful? && active_scaffold_config.create.refresh_list && !render_parent?
    do_search if respond_to? :do_search
    do_list
  end
  render :action => 'on_create'
end
create_respond_to_json() click to toggle source
# File lib/active_scaffold/actions/create.rb, line 75
def create_respond_to_json
  render :text => response_object.to_json(:only => active_scaffold_config.create.columns.names), :content_type => Mime::JSON, :status => response_status, :location => response_location
end
create_respond_to_xml() click to toggle source
# File lib/active_scaffold/actions/create.rb, line 71
def create_respond_to_xml
  render :xml => response_object.to_xml(:only => active_scaffold_config.create.columns.names), :content_type => Mime::XML, :status => response_status, :location => response_location
end
create_respond_to_yaml() click to toggle source
# File lib/active_scaffold/actions/create.rb, line 79
def create_respond_to_yaml
  render :text => Hash.from_xml(response_object.to_xml(:only => active_scaffold_config.create.columns.names)).to_yaml, :content_type => Mime::YAML, :status => response_status, :location => response_location
end
create_save() click to toggle source
# File lib/active_scaffold/actions/create.rb, line 112
def create_save
  before_create_save(@record)
  self.successful = [@record.valid?, @record.associated_valid?].all? {|v| v == true} # this syntax avoids a short-circuit
  if successful?
    @record.save! and @record.save_associated!
    after_create_save(@record)
  end
end
do_create() click to toggle source

A somewhat complex method to actually create a new record. The complexity is from support for subforms and associated records. If you want to customize this behavior, consider using the before_create_save and after_create_save callbacks.

# File lib/active_scaffold/actions/create.rb, line 97
def do_create
  begin
    active_scaffold_config.model.transaction do
      @record = update_record_from_params(new_model, active_scaffold_config.create.columns, params[:record])
      apply_constraints_to_record(@record, :allow_autosave => true)
      if nested?
        create_association_with_parent(@record) 
        register_constraints_with_action_columns(nested.constrained_fields)
      end
      create_save
    end
  rescue ActiveRecord::RecordInvalid
  end
end
do_new() click to toggle source

A simple method to find and prepare an example new record for the form May be overridden to customize the behavior (add default values, for instance)

# File lib/active_scaffold/actions/create.rb, line 85
def do_new
  @record = new_model
  apply_constraints_to_record(@record)
  if nested?
    create_association_with_parent(@record)
    register_constraints_with_action_columns(nested.constrained_fields)
  end
  @record
end
new_respond_to_html() click to toggle source
# File lib/active_scaffold/actions/create.rb, line 25
def new_respond_to_html
  if successful?
    render(:action => 'create')
  else
    return_to_main
  end
end
new_respond_to_js() click to toggle source
# File lib/active_scaffold/actions/create.rb, line 33
def new_respond_to_js
  render(:partial => 'create_form')
end
response_location() click to toggle source
# File lib/active_scaffold/actions/create.rb, line 21
def response_location
  url_for(params_for(:action => "show", :id => @record.id)) if successful?
end

Private Instance Methods

create_authorized_filter() click to toggle source
# File lib/active_scaffold/actions/create.rb, line 138
def create_authorized_filter
  link = active_scaffold_config.create.link || active_scaffold_config.create.class.link
  raise ActiveScaffold::ActionNotAllowed unless self.send(link.security_method)
end
create_formats() click to toggle source
# File lib/active_scaffold/actions/create.rb, line 145
def create_formats
  (default_formats + active_scaffold_config.formats + active_scaffold_config.create.formats).uniq
end
new_formats() click to toggle source
# File lib/active_scaffold/actions/create.rb, line 142
def new_formats
  (default_formats + active_scaffold_config.formats).uniq
end