module ActiveScaffold::Actions::Core

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
    after_filter :clear_flashes
  end
  base.helper_method :nested?
  base.helper_method :beginning_of_chain
  base.helper_method :new_model
end

Public Instance Methods

render_field() click to toggle source
# File lib/active_scaffold/actions/core.rb, line 11
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 67
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 48
def after_render_field(record, column); end
association_columns(columns) click to toggle source
# File lib/active_scaffold/actions/core.rb, line 172
def association_columns(columns)
  columns.select {|col| active_scaffold_config.model.reflect_on_association(col)}
end
authorized_for?(options = {}) click to toggle source
# File lib/active_scaffold/actions/core.rb, line 50
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 139
def beginning_of_chain
  active_scaffold_config.model
end
clear_flashes() click to toggle source
# File lib/active_scaffold/actions/core.rb, line 54
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 126
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 144
def conditions_from_params
  conditions = nil
  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.column_names.include?(key)
    if value.is_a?(Array)
      conditions = merge_conditions(conditions, ["#{active_scaffold_config.model.table_name}.#{key.to_s} in (?)", value])
    else
      conditions = merge_conditions(conditions, ["#{active_scaffold_config.model.table_name}.#{key.to_s} = ?", value])
    end
  end
  conditions
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 134
def custom_finder_options
  {}
end
default_formats() click to toggle source
# File lib/active_scaffold/actions/core.rb, line 62
def default_formats
  [:html, :js, :json, :xml, :yaml]
end
error_object_attributes() click to toggle source
# File lib/active_scaffold/actions/core.rb, line 102
def error_object_attributes
  [:errors]
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 130
def joins_for_collection
end
nested?() click to toggle source
# File lib/active_scaffold/actions/core.rb, line 21
def nested?
  false
end
new_model() click to toggle source
# File lib/active_scaffold/actions/core.rb, line 157
def new_model
  model = beginning_of_chain
  if model.columns_hash[model.inheritance_column]
    build_options = {model.inheritance_column.to_sym => active_scaffold_config.model_id} if nested? && nested.association && nested.association.collection?
    params = self.params # in new action inheritance_column must be in params
    params = params[:record] || {} unless params[model.inheritance_column] # in create action must be inside record key
    model = params.delete(model.inheritance_column).camelize.constantize if params[model.inheritance_column]
  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 25
def render_field_for_inplace_editing
  register_constraints_with_action_columns(nested.constrained_fields, active_scaffold_config.update.hide_nested_column ? [] : [:update]) if nested?
  @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 31
def render_field_for_update_columns
  @record = new_model
  column = active_scaffold_config.columns[params[:column]]
  unless column.nil?
    if column.send_form_on_update_column
      @record = update_record_from_params(@record, active_scaffold_config.update.columns, params[:record])
    else
      value = column_value_from_param_value(@record, column, params[:value])
      @record.send "#{column.name}=", value
    end
    after_render_field(@record, column)
    source_id = params.delete(:source_id)
    render :partial => "render_field", :collection => Array(params[:update_columns]), :content_type => 'text/javascript', :locals => {:source_id => source_id}
  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 90
def response_object
  @response_object = if successful?
                       (@record || @records)
                     else
                       unless flash[:error].nil?
                         @record.errors.add(:base, flash[:error])
                         flash.discard(:error)
                       end
                       {:errors => @record.errors}
                     end
end
response_status() click to toggle source
# File lib/active_scaffold/actions/core.rb, line 81
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 121
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 116
def successful=(val)
  @successful = (val) ? true : false
end
successful?() click to toggle source

Success is the existence of certain variables and the absence of errors (when applicable). Success can also be defined.

# File lib/active_scaffold/actions/core.rb, line 108
def successful?
  if @successful.nil?
    @records or (@record and @record.errors.count == 0 and @record.no_errors_in_associated?)
  else
    @successful
  end
end
virtual_columns(columns) click to toggle source
# File lib/active_scaffold/actions/core.rb, line 168
def virtual_columns(columns)
  columns.reject {|col| active_scaffold_config.model.columns_hash[col.to_s] || active_scaffold_config.model.reflect_on_association(col)}
end

Private Instance Methods

action_formats() click to toggle source
# File lib/active_scaffold/actions/core.rb, line 189
def action_formats
  @action_formats ||= if respond_to? "#{action_name}_formats", true
    send("#{action_name}_formats")
  else
    (default_formats + active_scaffold_config.formats).uniq
  end
end
respond_to_action(action) click to toggle source
# File lib/active_scaffold/actions/core.rb, line 177
def respond_to_action(action)
  respond_to do |type|
    action_formats.each do |format|
      type.send(format) do
        if respond_to?(method_name = "#{action}_respond_to_#{format}", true)
          send(method_name)
        end
      end
    end
  end
end
response_code_for_rescue(exception) click to toggle source
Calls superclass method
# File lib/active_scaffold/actions/core.rb, line 197
def response_code_for_rescue(exception)
  case exception
    when ActiveScaffold::RecordNotAllowed
      "403 Record Not Allowed"
    when ActiveScaffold::ActionNotAllowed
      "403 Action Not Allowed"
    else
      super
  end
end