module ActiveScaffold::Actions::Core

Public Instance Methods

render_field() click to toggle source
# File lib/active_scaffold/actions/core.rb, line 13
def render_field
  if params[:in_place_editing]
    render_field_for_inplace_editing
  else
    render_field_for_update_columns
  end
end

Protected Instance Methods

accepts?(*types) click to toggle source

Returns true if the client accepts one of the MIME types passed to it ex: accepts? :html, :xml

# File lib/active_scaffold/actions/core.rb, line 92
def accepts?(*types)
  for priority in request.accepts.compact
    if priority == Mime::ALL
      # Because IE always sends */* in the accepts header and we assume
      # that if you really wanted XML or something else you would say so
      # explicitly, we will assume */* to only ask for :html
      return types.include?(:html)
    elsif types.include?(priority.to_sym)
      return true
    end
  end
  false
end
after_render_field(record, column) click to toggle source

override this method if you want to do something after #render_field

# File lib/active_scaffold/actions/core.rb, line 65
def after_render_field(record, column); end
authorized_for?(options = {}) click to toggle source
# File lib/active_scaffold/actions/core.rb, line 67
def authorized_for?(options = {})
  active_scaffold_config.model.authorized_for?(options)
end
beginning_of_chain() click to toggle source

Overide this method on your controller to provide model with named scopes

# File lib/active_scaffold/actions/core.rb, line 152
def beginning_of_chain
  active_scaffold_config.model
end
clear_flashes() click to toggle source
# File lib/active_scaffold/actions/core.rb, line 71
def clear_flashes
  if request.xhr?
    flash.keys.each do |flash_key|
      flash[flash_key] = nil
    end
  end
end
conditions_for_collection() click to toggle source

Override this method on your controller to define conditions to be used when querying a recordset (e.g. for List). The return of this method should be any format compatible with the :conditions clause of ActiveRecord::Base’s find.

# File lib/active_scaffold/actions/core.rb, line 139
def conditions_for_collection
end
conditions_from_params() click to toggle source

Builds search conditions by search params for column names. This allows urls like “contacts/list?company_id=5”.

# File lib/active_scaffold/actions/core.rb, line 157
def conditions_from_params
  @conditions_from_params ||= begin
    conditions = {}
    params.reject {|key, value| [:controller, :action, :id, :page, :sort, :sort_direction].include?(key.to_sym)}.each do |key, value|
      next unless active_scaffold_config.model.columns_hash[key.to_s]
      next if active_scaffold_constraints[key.to_sym]
      next if nested? and nested.constrained_fields.include? key.to_sym
      conditions[key.to_sym] = value
    end
    conditions
  end
end
custom_finder_options() click to toggle source

Override this method on your controller to provide custom finder options to the find() call. The return of this method should be a hash.

# File lib/active_scaffold/actions/core.rb, line 147
def custom_finder_options
  {}
end
default_formats() click to toggle source
# File lib/active_scaffold/actions/core.rb, line 87
def default_formats
  [:html, :js, :json, :xml, :yaml]
end
each_marked_record(&block) click to toggle source
# File lib/active_scaffold/actions/core.rb, line 79
def each_marked_record(&block)
  active_scaffold_config.model.find(marked_records.to_a).each &block
end
embedded?() click to toggle source
# File lib/active_scaffold/actions/core.rb, line 22
def embedded?
  @embedded ||= params.delete(:embedded)
end
joins_for_collection() click to toggle source

Override this method on your controller to define joins to be used when querying a recordset (e.g. for List). The return of this method should be any format compatible with the :joins clause of ActiveRecord::Base’s find.

# File lib/active_scaffold/actions/core.rb, line 143
def joins_for_collection
end
marked_records() click to toggle source
# File lib/active_scaffold/actions/core.rb, line 83
def marked_records
  active_scaffold_session_storage[:marked_records] ||= Set.new
end
nested?() click to toggle source
# File lib/active_scaffold/actions/core.rb, line 26
def nested?
  false
end
new_model() click to toggle source
# File lib/active_scaffold/actions/core.rb, line 170
def new_model
  model = beginning_of_chain
  if nested? && nested.association && nested.association.collection? && model.columns_hash[column = model.inheritance_column]
    model_name = params.delete(column) # in new action inheritance_column must be in params
    model_name ||= params[:record].delete(column) unless params[:record].blank? # in create action must be inside record key
    model_name = model_name.camelize if model_name
    model_name ||= active_scaffold_config.model.name
    build_options = {column.to_sym => model_name} if model_name
  end
  model.respond_to?(:build) ? model.build(build_options || {}) : model.new
end
render_field_for_inplace_editing() click to toggle source
# File lib/active_scaffold/actions/core.rb, line 30
def render_field_for_inplace_editing
  @record = find_if_allowed(params[:id], :update)
  render :inline => "<%= active_scaffold_input_for(active_scaffold_config.columns[params[:update_column].to_sym]) %>"
end
render_field_for_update_columns() click to toggle source
# File lib/active_scaffold/actions/core.rb, line 35
def render_field_for_update_columns
  column = active_scaffold_config.columns[params[:column]]
  unless column.nil?
    @source_id = params.delete(:source_id)
    @columns = column.update_columns
    @scope = params[:scope]
    
    if column.send_form_on_update_column
      if @scope
        hash = @scope.gsub('[','').split(']').inject(params[:record]) do |hash, index|
          hash[index]
        end
        id = hash[:id]
      else
        hash = params[:record]
        id = params[:id]
      end
      @record = id ? find_if_allowed(id, :update) : new_model
      @record = update_record_from_params(@record, active_scaffold_config.send(@scope ? :subform : (id ? :update : :create)).columns, hash)
    else
      @record = new_model
      value = column_value_from_param_value(@record, column, params[:value])
      @record.send "#{column.name}=", value
    end
    
    after_render_field(@record, column)
  end
end
response_object() click to toggle source

API response object that will be converted to XML/YAML/JSON using to_xxx

# File lib/active_scaffold/actions/core.rb, line 115
def response_object
  @response_object = successful? ? (@record || @records) : @record.errors
end
response_status() click to toggle source
# File lib/active_scaffold/actions/core.rb, line 106
def response_status
  if successful?
    action_name == 'create' ? 201 : 200
  else
    422
  end
end
return_to_main() click to toggle source

Redirect to the main page (override if the ActiveScaffold is used as a component on another controllers page) for Javascript degradation

# File lib/active_scaffold/actions/core.rb, line 134
def return_to_main
  redirect_to main_path_to_return
end
successful=(val) click to toggle source
# File lib/active_scaffold/actions/core.rb, line 129
def successful=(val)
  @successful = (val) ? true : false
end
successful?() click to toggle source

Success is the existence of one or more model objects. Most actions circumvent this method by setting @success directly.

# File lib/active_scaffold/actions/core.rb, line 121
def successful?
  if @successful.nil?
    @record || @records
  else
    @successful
  end
end

Public Class Methods

included(base) click to toggle source
# File lib/active_scaffold/actions/core.rb, line 3
def self.included(base)
  base.class_eval do
    prepend_before_filter :register_constraints_with_action_columns, :unless => :nested?
    after_filter :clear_flashes
    rescue_from ActiveScaffold::RecordNotAllowed, ActiveScaffold::ActionNotAllowed, :with => :deny_access
  end
  base.helper_method :nested?
  base.helper_method :calculate
  base.helper_method :new_model
end