Module: ActiveScaffold::Helpers::ActionLinkHelpers

Included in:
ViewHelpers
Defined in:
lib/active_scaffold/helpers/action_link_helpers.rb

Overview

All extra helpers that should be included in the View. Also a dumping ground for uncategorized helpers.

Constant Summary collapse

NESTED_PARAMS =

params which mustn’t be copying to nested links

%i[eid embedded association parent_scaffold].freeze

Instance Method Summary collapse

Instance Method Details

Returns:

  • (Boolean)


13
14
15
16
17
18
19
20
21
# File 'lib/active_scaffold/helpers/action_link_helpers.rb', line 13

def action_link_authorized?(link, *args)
  auth, reason =
    if link.security_method_set? || controller.respond_to?(link.security_method, true)
      controller.send(link.security_method, *args)
    else
      args.empty? ? true : args.first.authorized_for?(:crud_type => link.crud_type, :action => link.action, :reason => true)
    end
  [auth, reason]
end


389
390
391
392
393
394
395
396
397
# File 'lib/active_scaffold/helpers/action_link_helpers.rb', line 389

def action_link_html(link, url, html_options, record)
  label = html_options.delete(:link)
  label ||= link.label
  if url.nil?
    (:a, label, html_options)
  else
    link_to(label, url, html_options)
  end
end


329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# File 'lib/active_scaffold/helpers/action_link_helpers.rb', line 329

def action_link_html_options(link, record, options)
  link_id = get_action_link_id(link, record)
  html_options = link.html_options.merge(:class => [link.html_options[:class], link.action.to_s].compact.join(' '))
  html_options[:link] = action_link_text(link, options)

  # Needs to be in html_options to as the adding _method to the url is no longer supported by Rails
  html_options[:method] = link.method if link.method != :get

  html_options[:data] ||= {}
  html_options[:data] = html_options[:data].deep_dup if html_options[:data].frozen?
  html_options[:data][:confirm] = link.confirm(h(record&.to_label)) if link.confirm?
  if !options[:page] && !options[:popup] && (options[:inline] || link.inline?)
    html_options[:class] << ' as_action'
    html_options[:data][:position] = link.position if link.position
    html_options[:data][:action] = link.action
    html_options[:data][:cancel_refresh] = true if link.refresh_on_close
    html_options[:data][:keep_open] = true if link.keep_open?
    html_options[:remote] = true
  end

  if link.toggle
    html_options[:class] << ' toggle'
    html_options[:class] << ' active' if action_link_selected?(link, record)
  end

  if !options[:page] && !options[:inline] && (options[:popup] || link.popup?)
    html_options[:target] = '_blank'
    html_options[:rel] = [html_options[:rel], 'noopener noreferrer'].compact.join(' ')
  end
  html_options[:id] = link_id
  if link.dhtml_confirm?
    unless link.inline?
      html_options[:class] << ' as_action'
      html_options[:page_link] = 'true'
    end
    html_options[:dhtml_confirm] = link.dhtml_confirm.value
    html_options[:onclick] = link.dhtml_confirm.onclick_function(controller, link_id)
  end
  html_options
end

Returns:

  • (Boolean)


323
324
325
326
327
# File 'lib/active_scaffold/helpers/action_link_helpers.rb', line 323

def action_link_selected?(link, record)
  missing_options, url_options = replaced_action_link_url_options(link, record)
  safe_params = params.to_unsafe_h
  (url_options - safe_params.to_a).blank? && missing_options.all? { |k, _| params[k].nil? }
end


301
302
303
304
# File 'lib/active_scaffold/helpers/action_link_helpers.rb', line 301

def action_link_text(link, options)
  text = image_tag(link.image[:name], :size => link.image[:size], :alt => options[:link] || link.label, :title => options[:link] || link.label) if link.image
  text || options[:link]
end

setup the action link to inline form



99
100
101
102
103
104
105
106
107
108
# File 'lib/active_scaffold/helpers/action_link_helpers.rb', line 99

def action_link_to_inline_form(link, record)
  link = link.dup
  associated = record.send(link.column.association.name)
  if link.column.association&.polymorphic? || link.controller.nil?
    link.controller = controller_path_for_activerecord(associated.class)
    return link if link.controller.nil?
  end
  link = configure_column_link(link, record, associated) if link.action.nil?
  link
end


208
209
210
211
212
# File 'lib/active_scaffold/helpers/action_link_helpers.rb', line 208

def action_link_url(link, record)
  url = replace_id_params_in_action_link_url(link, record, cached_action_link_url(link, record))
  url = add_query_string_to_cached_url(link, url) if @action_links_urls[link.name_to_cache.to_s]
  url
end


279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/active_scaffold/helpers/action_link_helpers.rb', line 279

def action_link_url_options(link, record)
  url_options = {:action => link.action}
  url_options[:id] = '--ID--' unless record.nil?
  url_options[:controller] = link.controller.to_s if link.controller
  url_options.merge! link.parameters if link.parameters
  if link.dynamic_parameters.is_a?(Proc)
    if record.nil?
      url_options.merge! instance_exec(&link.dynamic_parameters)
    else
      url_options.merge! instance_exec(record, &link.dynamic_parameters)
    end
  end
  if link.nested_link?
    url_options_for_nested_link(link.column, record, link, url_options)
  elsif nested?
    url_options[nested.param_name] = '--CHILD_ID--'
  end
  url_options_for_sti_link(link.column, record, link, url_options) unless record.nil? || active_scaffold_config.sti_children.nil?
  url_options[:_method] = link.method if !link.confirm? && link.inline? && link.method != :get
  url_options
end

#add_query_string_to_cached_url(link, url) ⇒ Object



197
198
199
200
201
202
203
204
205
206
# File 'lib/active_scaffold/helpers/action_link_helpers.rb', line 197

def add_query_string_to_cached_url(link, url)
  query_string, non_nested_query_string = query_string_for_action_links(link)
  nested_params = (!link.nested_link? && non_nested_query_string)
  if query_string || nested_params
    url << (url.include?('?') ? '&' : '?')
    url << query_string if query_string
    url << non_nested_query_string if nested_params
  end
  url
end

Returns:

  • (Boolean)


164
165
166
# File 'lib/active_scaffold/helpers/action_link_helpers.rb', line 164

def cache_action_link_url?(link, record)
  active_scaffold_config.user.cache_action_link_urls && link.type == :member && !link.dynamic_parameters.is_a?(Proc) && !sti_record?(record)
end

Returns:

  • (Boolean)


264
265
266
# File 'lib/active_scaffold/helpers/action_link_helpers.rb', line 264

def cache_action_link_url_options?(link, record)
  active_scaffold_config.user.cache_action_link_urls && (link.type == :collection || !link.dynamic_parameters.is_a?(Proc)) && !sti_record?(record)
end


168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/active_scaffold/helpers/action_link_helpers.rb', line 168

def cached_action_link_url(link, record)
  @action_links_urls ||= {}
  @action_links_urls[link.name_to_cache.to_s] || begin
    url_options = cached_action_link_url_options(link, record)
    if cache_action_link_url?(link, record)
      @action_links_urls[link.name_to_cache.to_s] = url_for(url_options)
    else
      url_options.merge! eid: nil, embedded: nil if link.nested_link?
      url_for(params_for(url_options))
    end
  end
end


268
269
270
271
272
273
274
275
276
277
# File 'lib/active_scaffold/helpers/action_link_helpers.rb', line 268

def cached_action_link_url_options(link, record)
  @action_links_url_options ||= {}
  @action_links_url_options[link.name_to_cache.to_s] || begin
    options = action_link_url_options(link, record)
    if cache_action_link_url_options?(link, record)
      @action_links_url_options[link.name_to_cache.to_s] = options
    end
    options
  end
end

#column_in_params_conditions?(key) ⇒ Boolean

Returns:

  • (Boolean)


214
215
216
217
218
219
220
# File 'lib/active_scaffold/helpers/action_link_helpers.rb', line 214

def column_in_params_conditions?(key)
  if key.match?(/!$/)
    conditions_from_params[1..-1].any? { |node| node.left.name.to_s == key[0..-2] }
  else
    conditions_from_params[0].include?(key)
  end
end

Returns:

  • (Boolean)


139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/active_scaffold/helpers/action_link_helpers.rb', line 139

def column_link_authorized?(link, column, record, associated)
  if column.association
    associated_for_authorized =
      if column.association.collection? || associated.nil?
        column.association.klass
      else
        associated
      end
    authorized, reason = associated_for_authorized.authorized_for?(:crud_type => link.crud_type, :reason => true)
    if link.crud_type == :create && authorized
      authorized, reason = record.authorized_for?(:crud_type => :update, :column => column.name, :reason => true)
    end
    [authorized, reason]
  else
    action_link_authorized?(link, record)
  end
end


110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/active_scaffold/helpers/action_link_helpers.rb', line 110

def configure_column_link(link, record, associated, actions = nil)
  actions ||= link.controller_actions || []
  if column_empty?(associated) # if association is empty, we only can link to create form
    if actions.include?(:new)
      link.action = 'new'
      link.crud_type = :create
      link.label ||= :create_new
    end
  elsif actions.include?(:edit)
    link.action = 'edit'
    link.crud_type = :update
  elsif actions.include?(:show)
    link.action = 'show'
    link.crud_type = :read
  elsif actions.include?(:list)
    link.action = 'index'
    link.crud_type = :read
  end

  unless column_link_authorized?(link, link.column, record, associated)[0]
    link.action = nil
    # if action is edit and is not authorized, fallback to show if it's enabled
    if link.crud_type == :update && actions.include?(:show)
      link = configure_column_link(link, record, associated, [:show])
    end
  end
  link
end


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

def display_action_link(link, content, record, options)
  if content
    html_classes = hover_via_click? ? 'hover_click ' : ''
    if (options[:level]).zero?
      html_classes << 'action_group'
      group_tag = :div
    else
      html_classes << 'top' if options[:first_action]
      group_tag = :li
    end
    content = (group_tag, :class => html_classes.presence, :onclick => ('' if hover_via_click?)) do
      (:div, as_(link.label), :class => link.name.to_s.downcase) << (:ul, content)
    end
  else
    content = render_action_link(link, record, options)
    content = (:li, content, :class => ('top' if options[:first_action])) unless (options[:level]).zero?
  end
  content = (options[:level_0_tag], content, options[:options_level_0_tag]) if (options[:level]).zero? && options[:level_0_tag]
  content
end


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

def display_action_links(action_links, record, options, &block)
  options[:level_0_tag] ||= nil
  options[:options_level_0_tag] ||= nil
  options[:level] ||= 0
  options[:first_action] = true
  output = ActiveSupport::SafeBuffer.new

  action_links.each(:reverse => options.delete(:reverse), :groups => true) do |link|
    if link.is_a? ActiveScaffold::DataStructures::ActionLinks
      unless link.empty?
        options[:level] += 1
        content = display_action_links(link, record, options, &block)
        options[:level] -= 1
        if content.present?
          output << display_action_link(link, content, record, options)
          options[:first_action] = false
        end
      end
    elsif !skip_action_link?(link, *Array(options[:for]))
      authorized, reason = action_link_authorized?(link, *Array(options[:for]))
      next if !authorized && options[:skip_unauthorized]
      output << display_action_link(link, nil, record, options.merge(:authorized => authorized, :not_authorized_reason => reason))
      options[:first_action] = false
    end
  end
  output
end

#display_dynamic_action_group(action_link, links, record_or_ul_options = nil, ul_options = nil) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/active_scaffold/helpers/action_link_helpers.rb', line 23

def display_dynamic_action_group(action_link, links, record_or_ul_options = nil, ul_options = nil)
  ul_options = record_or_ul_options if ul_options.nil? && record_or_ul_options.is_a?(Hash)
  record = record_or_ul_options unless record_or_ul_options.is_a?(Hash)
  html =  :ul, ul_options do
    safe_join(links.map { |link|  :li, link })
  end
  raw "ActiveScaffold.display_dynamic_action_group('#{get_action_link_id action_link, record}', '#{escape_javascript html}');" # rubocop:disable Rails/OutputSafety
end


370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# File 'lib/active_scaffold/helpers/action_link_helpers.rb', line 370

def get_action_link_id(link, record = nil)
  column = link.column
  if column&.association && record
    associated = record.send(column.association.name) unless column.association.collection?
    id =
      if associated
        "#{column.association.name}-#{associated.id}-#{record.id}"
      else
        "#{column.association.name}-#{record.id}"
      end
  end
  id ||= record&.id&.to_s || (nested? ? nested_parent_id.to_s : '')
  action_link_id = ActiveScaffold::Registry.cache :action_link_id, link.name_to_cache.to_s do
    action_id = "#{id_from_controller("#{link.controller}-") if params[:parent_controller] || (link.controller && link.controller != controller.controller_path)}#{link.action}"
    action_link_id(action_id, '--ID--')
  end
  action_link_id.sub('--ID--', id)
end

#ignore_param_for_nested?(key) ⇒ Boolean

Returns:

  • (Boolean)


222
223
224
# File 'lib/active_scaffold/helpers/action_link_helpers.rb', line 222

def ignore_param_for_nested?(key)
  NESTED_PARAMS.include?(key) || column_in_params_conditions?(key) || (nested? && nested.param_name == key)
end


226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/active_scaffold/helpers/action_link_helpers.rb', line 226

def query_string_for_action_links(link)
  if defined?(@query_string) && link.parameters.none? { |k, _| @query_string_params.include? k }
    return [@query_string, @non_nested_query_string]
  end
  keep = true
  @query_string_params ||= Set.new
  query_string_options = {}
  non_nested_query_string_options = {}

  params_for.except(:controller, :action, :id).each do |key, value|
    @query_string_params << key
    if link.parameters.include? key
      keep = false
      next
    end
    if ignore_param_for_nested?(key)
      non_nested_query_string_options[key] = value
    else
      query_string_options[key] = value
    end
  end
  if nested_singular_association? && action_name == 'index'
    # pass current path as return_to, for nested listing on singular association, so forms doesn't return to parent listing
    @query_string_params << :return_to
    non_nested_query_string_options[:return_to] = request.fullpath
  end

  query_string = query_string_options.to_query if query_string_options.present?
  if non_nested_query_string_options.present?
    non_nested_query_string = "#{'&' if query_string}#{non_nested_query_string_options.to_query}"
  end
  if keep
    @query_string = query_string
    @non_nested_query_string = non_nested_query_string
  end
  [query_string, non_nested_query_string]
end


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/active_scaffold/helpers/action_link_helpers.rb', line 81

def render_action_link(link, record = nil, options = {})
  if link.action.nil? || link.column&.association&.polymorphic?
    link = action_link_to_inline_form(link, record) if link.column&.association
    options[:authorized] = false if link.action.nil? || link.controller.nil?
    options.delete :link if link.crud_type == :create
  end
  if link.action.nil? || (link.type == :member && options.key?(:authorized) && !options[:authorized])
    html_class = "disabled #{link.action}#{" #{link.html_options[:class]}" if link.html_options[:class].present?}"
    html_options = {:link => action_link_text(link, options), :class => html_class, :title => options[:not_authorized_reason]}
    action_link_html(link, nil, html_options, record)
  else
    url = action_link_url(link, record)
    html_options = action_link_html_options(link, record, options)
    action_link_html(link, url, html_options, record)
  end
end


181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/active_scaffold/helpers/action_link_helpers.rb', line 181

def replace_id_params_in_action_link_url(link, record, url)
  url = record ? url.sub('--ID--', record.to_param.to_s) : url.clone
  if link.column&.association&.singular?
    child_id = record.send(link.column.association.name)&.to_param
    if child_id.present?
      url.sub!('--CHILD_ID--', child_id)
    else
      url.sub!(/\w+=--CHILD_ID--&?/, '')
      url.sub!(/\?$/, '')
    end
  elsif nested?
    url.sub!('--CHILD_ID--', params[nested.param_name].to_s)
  end
  url
end


306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'lib/active_scaffold/helpers/action_link_helpers.rb', line 306

def replaced_action_link_url_options(link, record)
  url = cached_action_link_url_options(link, record)
  url[:controller] ||= params[:controller]
  missing_options, url_options = url.partition { |_, v| v.nil? }
  replacements = {}
  replacements['--ID--'] = record.id.to_s if record
  if link.column&.association&.singular?
    replacements['--CHILD_ID--'] = record.send(link.column.association.name)&.id.to_s
  elsif nested?
    replacements['--CHILD_ID--'] = params[nested.param_name].to_s
  end
  url_options.collect! do |k, v|
    [k.to_s, replacements[v] || v]
  end
  [missing_options, url_options]
end

#skip_action_link?(link, *args) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/active_scaffold/helpers/action_link_helpers.rb', line 9

def skip_action_link?(link, *args)
  !link.ignore_method.nil? && controller.respond_to?(link.ignore_method, true) && controller.send(link.ignore_method, *args)
end

#sti_record?(record) ⇒ Boolean

Returns:

  • (Boolean)


157
158
159
160
161
162
# File 'lib/active_scaffold/helpers/action_link_helpers.rb', line 157

def sti_record?(record)
  return unless active_scaffold_config.active_record?
  model = active_scaffold_config.model
  record && model.columns_hash.include?(model.inheritance_column) &&
    record[model.inheritance_column].present? && !record.instance_of?(model)
end


399
400
401
402
403
404
405
406
407
408
409
410
411
# File 'lib/active_scaffold/helpers/action_link_helpers.rb', line 399

def url_options_for_nested_link(column, record, link, url_options)
  if column&.association
    url_options[:parent_scaffold] = controller_path
    url_options[column.model.name.foreign_key.to_sym] = url_options.delete(:id)
    url_options[:id] = if column.association.singular? && url_options[:action].to_sym != :index
                         '--CHILD_ID--'
                       end
  elsif link.parameters&.dig(:named_scope)
    url_options[:parent_scaffold] = controller_path
    url_options[active_scaffold_config.model.name.foreign_key.to_sym] = url_options.delete(:id)
    url_options[:id] = nil
  end
end


413
414
415
416
417
418
419
420
421
422
# File 'lib/active_scaffold/helpers/action_link_helpers.rb', line 413

def url_options_for_sti_link(column, record, link, url_options)
  # need to find out controller of current record type and set parameters
  # it's quite difficult to detect an sti link
  # if link.column.nil? we are sure that it isn't a singular association inline autolink
  # however that will not work if a sti parent is a singular association inline autolink
  return unless link.column.nil?
  return if (sti_controller_path = controller_path_for_activerecord(record.class)).nil?
  url_options[:controller] = sti_controller_path
  url_options[:parent_sti] = controller_path
end