Module: ActiveScaffold::Helpers::ControllerHelpers

Includes:
IdHelpers
Defined in:
lib/active_scaffold/helpers/controller_helpers.rb

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from IdHelpers

#action_iframe_id, #action_link_id, #active_scaffold_calculations_id, #active_scaffold_column_header_id, #active_scaffold_content_id, #active_scaffold_id, #active_scaffold_messages_id, #active_scaffold_tbody_id, #association_subform_id, #before_header_id, #controller_id, #element_cell_id, #element_form_id, #element_messages_id, #element_row_id, #empty_message_id, #id_from_controller, #loading_indicator_id, #nested_id, #nested_parent_id, #scope_id, #search_input_id, #sub_form_id, #sub_form_list_id, #sub_section_id

Class Method Details

+ (Object) included(controller)



4
5
6
# File 'lib/active_scaffold/helpers/controller_helpers.rb', line 4

def self.included(controller)
  controller.class_eval { helper_method :params_for, :conditions_from_params, :main_path_to_return, :render_parent?, :render_parent_options, :render_parent_action, :nested_singular_association?, :build_associated, :generate_temporary_id, :generated_id}
end

Instance Method Details

- (Object) build_associated(association, parent_record)

build an associated record for association



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/active_scaffold/helpers/controller_helpers.rb', line 90

def build_associated(association, parent_record)
  if association.options[:through]
    # build full chain, only check create_associated on initial parent_record
    parent_record = build_associated(association.through_reflection, parent_record)
    build_associated(association.source_reflection, parent_record).tap do |record|
      save_record_to_association(record, association.source_reflection.reverse, parent_record) # set inverse
    end
  elsif association.collection?
    parent_record.send(association.name).build
  elsif association.belongs_to? || parent_record.new_record? || parent_record.send(association.name).nil?
    # avoid use build_association in has_one when record is saved and had associated record
    # because associated record would be changed in DB
    parent_record.send("build_#{association.name}")
  else
    association.klass.new.tap do |record|
      save_record_to_association(record, association.reverse, parent_record) # set inverse
    end
  end
end

- (Object) generate_temporary_id(record = nil, generated_id = nil)



10
11
12
13
14
# File 'lib/active_scaffold/helpers/controller_helpers.rb', line 10

def generate_temporary_id(record = nil, generated_id = nil)
  (generated_id || (Time.now.to_f*1000).to_i.to_s).tap do |id|
    (@temporary_ids ||= {})[record.class.name] = id if record
  end
end

- (Object) generated_id(record)



16
17
18
# File 'lib/active_scaffold/helpers/controller_helpers.rb', line 16

def generated_id(record)
  @temporary_ids[record.class.name] if record && @temporary_ids
end

- (Object) main_path_to_return

Parameters to generate url to the main page (override if the ActiveScaffold is used as a component on another controllers page)



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/active_scaffold/helpers/controller_helpers.rb', line 36

def main_path_to_return
  if params[:return_to]
    params[:return_to]
  else
    exclude_parameters = [:utf8, :associated_id]
    parameters = {}
    if params[:parent_scaffold] && nested? && nested.singular_association?
      parameters[:controller] = params[:parent_scaffold]
      exclude_parameters.concat [nested.param_name, :association, :parent_scaffold]
      #parameters[:eid] = params[:parent_scaffold] # not neeeded anymore?
    end
    parameters.merge! nested.to_params if nested?
    if params[:parent_sti]
      parameters[:controller] = params[:parent_sti]
      #parameters[:eid] = nil # not neeeded anymore?
    end
    parameters[:action] = "index"
    parameters[:id] = nil
    params_for(parameters).except(*exclude_parameters)
  end
end

- (Boolean) nested_singular_association?

Returns:

  • (Boolean)


58
59
60
# File 'lib/active_scaffold/helpers/controller_helpers.rb', line 58

def nested_singular_association?
  nested? && (nested.belongs_to? || nested.has_one?)
end

- (Object) params_for(options = {})



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/active_scaffold/helpers/controller_helpers.rb', line 20

def params_for(options = {})
  # :adapter and :position are one-use rendering arguments. they should not propagate.
  # :sort, :sort_direction, and :page are arguments that stored in the session. they need not propagate.
  # and wow. no we don't want to propagate :record.
  # :commit is a special rails variable for form buttons
  blacklist = [:adapter, :position, :sort, :sort_direction, :page, :auto_pagination, :record, :commit, :_method, :authenticity_token, :iframe, :associated_id, :dont_close]
  unless @params_for
    @params_for = {}
    params.except(*blacklist).each {|key, value| @params_for[key.to_sym] = value.duplicable? ? value.clone : value}
    @params_for[:controller] = '/' + @params_for[:controller].to_s unless @params_for[:controller].to_s.first(1) == '/' # for namespaced controllers
    @params_for.delete(:id) if @params_for[:id].nil?
  end
  @params_for.merge(options)
end

- (Boolean) render_parent?

Returns:

  • (Boolean)


62
63
64
# File 'lib/active_scaffold/helpers/controller_helpers.rb', line 62

def render_parent?
  nested_singular_association? || params[:parent_sti]
end

- (Object) render_parent_action



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/active_scaffold/helpers/controller_helpers.rb', line 75

def render_parent_action
  begin
    @parent_action = :row
    if params[:parent_sti]
      parent_controller = "#{params[:parent_sti].to_s.camelize}Controller".constantize
      @parent_action = :index if action_name == 'create' && parent_controller.active_scaffold_config.actions.include?(:create) && parent_controller.active_scaffold_config.create.refresh_list == true
      @parent_action = :index if action_name == 'update' && parent_controller.active_scaffold_config.actions.include?(:update) && parent_controller.active_scaffold_config.update.refresh_list == true
      @parent_action = :index if action_name == 'destroy' && parent_controller.active_scaffold_config.actions.include?(:delete) && parent_controller.active_scaffold_config.delete.refresh_list == true
    end
  rescue ActiveScaffold::ControllerNotFound
  end if @parent_action.nil?
  @parent_action
end

- (Object) render_parent_options



66
67
68
69
70
71
72
73
# File 'lib/active_scaffold/helpers/controller_helpers.rb', line 66

def render_parent_options
  if nested_singular_association?
    {:controller => nested.parent_scaffold.controller_path, :action => :index, :id => nested.parent_id}
  elsif params[:parent_sti]
    options = params_for(:controller => params[:parent_sti], :action => render_parent_action, :parent_sti => nil)
    options.merge(:action => :index, :id => @record.to_param) if render_parent_action == :row
  end
end