Module: ActiveScaffold::Helpers::ControllerHelpers

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

Constant Summary collapse

BLACKLIST_PARAMS =

These params should not propagate: :adapter and :position are one-use rendering arguments. :sort, :sort_direction, and :page are arguments that stored in the session, if store_user_settings is enabled and wow. no we don't want to propagate :record. :commit is a special rails variable for form buttons :_method is a special rails variable to simulate put, patch and delete actions. :dont_close is submit button which avoids closing form. :auto_pagination is used when all records are loaded automatically with multiple request. :iframe is used to simulate ajax forms loading form in iframe. :associated_id used in add_existing :authenticity_token is sent on some ajax requests :_added is sent on checkbox-list with update_columns :_removed is sent on checkbox-list with update_columns :_popstate sent when loading previous page from history, after using history.pushState :_ jQuery param added for GET requests with cache disabled

%i[adapter position sort sort_direction page record commit _method dont_close auto_pagination
iframe associated_id authenticity_token _added _removed _popstate _].freeze

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?, #nested_id, #nested_parent_id, #scope_id, #search_input_id, #sub_form_id, #sub_form_list_id, #sub_section_id

Class Method Details

.included(controller) ⇒ Object

[View source]

4
5
6
7
8
9
10
11
12
13
14
# File 'lib/active_scaffold/helpers/controller_helpers.rb', line 4

def self.included(controller)
  return unless controller.respond_to? :helper_method
  controller.class_eval do
    helper_method :params_for, :conditions_from_params, :render_parent?,
                  :main_path_to_return, :render_parent_options,
                  :render_parent_action, :nested_singular_association?,
                  :main_form_controller, :build_associated,
                  :generate_temporary_id, :generated_id,
                  :active_scaffold_config_for
  end
end

Instance Method Details

#active_scaffold_config_for(klass) ⇒ Object

[View source]

18
19
20
21
22
23
24
25
# File 'lib/active_scaffold/helpers/controller_helpers.rb', line 18

def active_scaffold_config_for(klass)
  config = self.class.active_scaffold_config_for(klass)
  if ActiveScaffold.threadsafe
    config.user || config.({}, {})
  else
    config
  end
end

#build_associated(association, parent_record) ⇒ Object

build an associated record for association

[View source]

159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/active_scaffold/helpers/controller_helpers.rb', line 159

def build_associated(association, parent_record)
  if association.through? && association.through_reflection.collection?
    # build full chain, only check create_associated on initial parent_record
    parent_record = build_associated(association.class.new(association.through_reflection), parent_record)
    source_assoc = association.class.new(association.source_reflection)
    build_associated(source_assoc, parent_record).tap do |record|
      save_record_to_association(record, source_assoc.reverse_association, parent_record) # set inverse
    end
  elsif association.through? # through belongs_to/has_one
    parent_record = parent_record.send(association.through_reflection.name)
    source_assoc = association.class.new(association.source_reflection)
    build_associated(source_assoc, parent_record)
  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_association, parent_record) # set inverse
    end
  end
end

#cache_generated_id(record, generated_id) ⇒ Object

[View source]

33
34
35
# File 'lib/active_scaffold/helpers/controller_helpers.rb', line 33

def cache_generated_id(record, generated_id)
  (@temporary_ids ||= {})[record.class.name] = generated_id if record && generated_id
end

#controller_requested(controller) ⇒ Object

[View source]

82
83
84
85
86
87
88
89
90
# File 'lib/active_scaffold/helpers/controller_helpers.rb', line 82

def controller_requested(controller)
  if controller.to_s.first(1) == '/'
    controller[1..-1]
  else
    path = controller_path.split('/')[0..-2]
    path << controller
    path.join('/')
  end
end

#copy_param(value) ⇒ Object

[View source]

92
93
94
95
96
97
98
# File 'lib/active_scaffold/helpers/controller_helpers.rb', line 92

def copy_param(value)
  if controller_params? value
    params_hash value
  else
    value.duplicable? ? value.clone : value
  end
end

#generate_temporary_id(record = nil, generated_id = nil) ⇒ Object

[View source]

27
28
29
30
31
# File 'lib/active_scaffold/helpers/controller_helpers.rb', line 27

def generate_temporary_id(record = nil, generated_id = nil)
  (generated_id || (Time.now.to_f * 1000).to_i.to_s).tap do |id|
    cache_generated_id record, id
  end
end

#generated_id(record) ⇒ Object

[View source]

37
38
39
# File 'lib/active_scaffold/helpers/controller_helpers.rb', line 37

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

#main_form_controllerObject

[View source]

127
128
129
# File 'lib/active_scaffold/helpers/controller_helpers.rb', line 127

def main_form_controller
  parent_controller_name.constantize if params[:parent_controller]
end

#main_path_to_returnObject

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

[View source]

101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/active_scaffold/helpers/controller_helpers.rb', line 101

def main_path_to_return
  if params[:return_to]
    params[:return_to]
  else
    exclude_parameters = %i[utf8 associated_id]
    parameters = {}
    if params[:parent_scaffold] && 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

#nested_singular_association?Boolean

Returns:

  • (Boolean)
[View source]

123
124
125
# File 'lib/active_scaffold/helpers/controller_helpers.rb', line 123

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

#params_for(options = {}) ⇒ Object

[View source]

59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/active_scaffold/helpers/controller_helpers.rb', line 59

def params_for(options = {})
  unless @params_for
    @params_for = {}
    params.except(*BLACKLIST_PARAMS).each do |key, value|
      @params_for[key.to_sym] = copy_param(value)
    end
    @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

  url_options = @params_for.merge(options)
  if !active_scaffold_config. && controller_requested(url_options[:controller]) == controller_path
    url_options[:search] ||= copy_param search_params if respond_to?(:search_params, true) && search_params.present?
    url_options[:page] ||= params[:page]
    if active_scaffold_config.actions.include?(:list) && active_scaffold_config.list.user.user_sorting?
      column, direction = active_scaffold_config.list.user.sorting.first
      url_options[:sort] ||= column.name
      url_options[:sort_direction] ||= direction
    end
  end
  url_options
end

#render_parent?Boolean

Returns:

  • (Boolean)
[View source]

131
132
133
# File 'lib/active_scaffold/helpers/controller_helpers.rb', line 131

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

#render_parent_actionObject

[View source]

145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/active_scaffold/helpers/controller_helpers.rb', line 145

def render_parent_action
  if @parent_action.nil?
    @parent_action = :row
    if parent_sti_controller
      parent_sti_config = parent_sti_controller.active_scaffold_config
      @parent_action = :index if action_name == 'create' && parent_sti_config.actions.include?(:create) && parent_sti_config.create.refresh_list == true
      @parent_action = :index if action_name == 'update' && parent_sti_config.actions.include?(:update) && parent_sti_config.update.refresh_list == true
      @parent_action = :index if action_name == 'destroy' && parent_sti_config.actions.include?(:delete) && parent_sti_config.delete.refresh_list == true
    end
  end
  @parent_action
end

#render_parent_optionsObject

[View source]

135
136
137
138
139
140
141
142
143
# File 'lib/active_scaffold/helpers/controller_helpers.rb', line 135

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

#save_record_to_association(record, association, value, reverse = nil) ⇒ Object

[View source]

184
185
186
187
188
189
190
191
192
193
# File 'lib/active_scaffold/helpers/controller_helpers.rb', line 184

def save_record_to_association(record, association, value, reverse = nil)
  return unless association
  if association.collection?
    record.association(association.name).target << value
  elsif reverse&.belongs_to?
    value.send("#{reverse.name}=", record)
  else
    record.send("#{association.name}=", value)
  end
end