Module: ActiveScaffold::Helpers::ViewHelpers

Overview

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

Constant Summary collapse

NESTED_PARAMS =
[:eid, :association, :parent_scaffold]

Instance Method Summary collapse

Methods included from HumanConditionHelpers

#active_scaffold_human_condition_boolean, #active_scaffold_human_condition_date, #active_scaffold_human_condition_for, #active_scaffold_human_condition_integer, #active_scaffold_human_condition_null, #active_scaffold_human_condition_select, #active_scaffold_human_condition_string, #format_human_condition, #override_human_condition, #override_human_condition?, #override_human_condition_column

Methods included from SearchColumnHelpers

#active_scaffold_search_boolean, #active_scaffold_search_date, #active_scaffold_search_datetime, #active_scaffold_search_for, #active_scaffold_search_multi_select, #active_scaffold_search_null, #active_scaffold_search_options, #active_scaffold_search_range, #active_scaffold_search_range_comparator_options, #active_scaffold_search_range_string?, #active_scaffold_search_select, #active_scaffold_search_text, #active_scaffold_search_time, #field_search_datetime_value, #field_search_params_range_values, #include_null_comparators?, #override_search, #override_search_field, #search_attribute, #search_column_label, #search_label_for, #searched_by?, #visibles_and_hiddens

Methods included from FormColumnHelpers

#active_scaffold_add_existing_input, #active_scaffold_add_existing_label, #active_scaffold_checkbox_list, #active_scaffold_checkbox_option, #active_scaffold_enum_options, #active_scaffold_grouped_options, #active_scaffold_input_boolean, #active_scaffold_input_carrierwave, #active_scaffold_input_checkbox, #active_scaffold_input_dragonfly, #active_scaffold_input_email, #active_scaffold_input_enum, #active_scaffold_input_file_column, #active_scaffold_input_for, #active_scaffold_input_number, #active_scaffold_input_options, #active_scaffold_input_paperclip, #active_scaffold_input_password, #active_scaffold_input_plural_association, #active_scaffold_input_radio, #active_scaffold_input_range, #active_scaffold_input_select, #active_scaffold_input_singular_association, #active_scaffold_input_telephone, #active_scaffold_input_text_options, #active_scaffold_input_textarea, #active_scaffold_input_url, #active_scaffold_input_virtual, #active_scaffold_plural_association_options, #active_scaffold_refresh_link, #active_scaffold_render_input, #active_scaffold_render_subform_column, #active_scaffold_subform_attributes, #active_scaffold_translate_select_options, #active_scaffold_translated_option, #column_renders_as, #column_scope, #column_show_add_existing, #column_show_add_new, #current_form_columns, #field_attributes, #form_attribute, #form_hidden_attribute, #in_subform?, #label_for, #numerical_constraints_for_column, #onsubmit, #override_form_field, #override_form_field_partial, #override_form_field_partial?, #override_input, #override_subform_partial, #override_subform_partial?, #render_column, #subform_label, #subform_partial_for_column, #update_columns_options

Methods included from ShowColumnHelpers

#active_scaffold_show_horizontal, #active_scaffold_show_text, #active_scaffold_show_vertical, #override_show_column_ui, #show_column_override, #show_column_value, #show_columns_for, #show_label

Methods included from ListColumnHelpers

#active_scaffold_column_carrierwave, #active_scaffold_column_checkbox, #active_scaffold_column_download_link, #active_scaffold_column_download_link_with_filename, #active_scaffold_column_dragonfly, #active_scaffold_column_fulltext, #active_scaffold_column_marked, #active_scaffold_column_paperclip, #active_scaffold_column_text, #active_scaffold_column_thumbnail, #active_scaffold_inplace_edit, #active_scaffold_inplace_edit_tag_options, #all_marked?, #cache_association, #clean_column_value, #column_heading_attributes, #column_heading_label, #column_heading_value, #column_override, #dragonfly_url_for_attachment, #format_association_value, #format_collection_association_value, #format_column_value, #format_number_value, #format_singular_association_value, #format_value, #get_column_method, #get_column_value, #inplace_edit?, #inplace_edit_cloning?, #inplace_edit_control, #inplace_edit_control_css_class, #inplace_edit_data, #mark_column_heading, #override_column_ui, #render_column_heading, #render_list_column

Methods included from PaginationHelpers

#pagination_ajax_link, #pagination_ajax_links, #pagination_url_options

Methods included from AssociationHelpers

#association_klass_scoped, #association_options_count, #association_options_find, #cache_association_options, #column_for_association, #options_for_association_conditions, #options_for_association_count, #record_select_params_for_add_existing, #sorted_association_options_find

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

Instance Method Details

Returns:

  • (Boolean)


88
89
90
91
92
93
94
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 88

def action_link_authorized?(link, *args)
  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)
  end
end


431
432
433
434
435
436
437
438
439
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 431

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


378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 378

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][:confirm] = link.confirm(h(record.try(:to_label))) if link.confirm?
  if 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?
  end

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

  html_options[:target] = '_blank' if link.popup?
  html_options[:id] = link_id
  html_options[:remote] = true unless link.page? || link.popup?
  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)


373
374
375
376
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 373

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


351
352
353
354
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 351

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



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

def action_link_to_inline_form(link, record)
  link = link.clone
  associated = record.send(link.column.association.name)
  if link.column.polymorphic_association?
    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


275
276
277
278
279
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 275

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]
  url
end


329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 329

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

#active_scaffold_configObject

access to the configuration variable



21
22
23
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 21

def active_scaffold_config
  controller.class.active_scaffold_config
end

#active_scaffold_config_for(*args) ⇒ Object



25
26
27
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 25

def active_scaffold_config_for(*args)
  controller.class.active_scaffold_config_for(*args)
end

#active_scaffold_controller_for(*args) ⇒ Object



29
30
31
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 29

def active_scaffold_controller_for(*args)
  controller.class.active_scaffold_controller_for(*args)
end

#active_scaffold_error_messages_for(*params) ⇒ Object



587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 587

def active_scaffold_error_messages_for(*params)
  options = params.extract_options!.symbolize_keys
  options.reverse_merge!(:container_tag => :div, :list_type => :ul)

  objects = Array.wrap(options.delete(:object) || params).map do |object|
    object = instance_variable_get("@#{object}") unless object.respond_to?(:to_model)
    object = convert_to_model(object)

    if object.class.respond_to?(:model_name)
      options[:object_name] ||= object.class.model_name.human.downcase
    end

    object
  end

  objects.compact!
  count = objects.inject(0) { |sum, object| sum + object.errors.count }

  if count.zero?
    ''
  else
    html = {}
    [:id, :class].each do |key|
      if options.include?(key)
        value = options[key]
        html[key] = value unless value.blank?
      else
        html[key] = 'errorExplanation'
      end
    end
    options[:object_name] ||= params.first

    header_message =
      if options.include?(:header_message)
        options[:header_message]
      else
        as_('errors.template.header', :count => count, :model => options[:object_name].to_s.gsub('_', ' '))
      end

    message = options.include?(:message) ? options[:message] : as_('errors.template.body')

    error_messages = objects.sum do |object|
      object.errors.full_messages.map do |msg|
        options[:list_type] != :br ? (:li, msg) : msg
      end
    end
    error_messages =
      if options[:list_type] == :br
        error_messages.join('<br/>').html_safe
      else
        (options[:list_type], error_messages.join.html_safe)
      end

    contents = []
    contents << (options[:header_tag] || :h2, header_message) unless header_message.blank?
    contents << (:p, message) unless message.blank?
    contents << error_messages
    contents = contents.join.html_safe
    options[:container_tag] ? (options[:container_tag], contents, html) : contents
  end
end

#add_query_string_to_cached_url(link, url) ⇒ Object



264
265
266
267
268
269
270
271
272
273
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 264

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

#as_main_div_classObject



509
510
511
512
513
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 509

def as_main_div_class
  classes = "active-scaffold active-scaffold-#{controller_id}  #{id_from_controller params[:controller]}-view #{active_scaffold_config.theme}-theme"
  classes << ' as_touch' if touch_device?
  classes
end

Returns:

  • (Boolean)


232
233
234
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 232

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

Returns:

  • (Boolean)


314
315
316
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 314

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


236
237
238
239
240
241
242
243
244
245
246
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 236

def cached_action_link_url(link, record)
  @action_links_urls ||= {}
  @action_links_urls[link.name_to_cache] || begin
    url_options = cached_action_link_url_options(link, record)
    if cache_action_link_url?(link, record)
      @action_links_urls[link.name_to_cache] = url_for(url_options)
    else
      url_for(params_for(url_options))
    end
  end
end


318
319
320
321
322
323
324
325
326
327
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 318

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

#clean_class_name(name) ⇒ Object



549
550
551
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 549

def clean_class_name(name)
  name.underscore.gsub('/', '_')
end

#clean_column_name(name) ⇒ Object



545
546
547
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 545

def clean_column_name(name)
  name.to_s.gsub('?', '')
end

#column_attributes(column, record) ⇒ Object



478
479
480
481
482
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 478

def column_attributes(column, record)
  method = override_helper column, 'column_attributes'
  return send(method, record) if method
  {}
end

#column_calculation(column) ⇒ Object



526
527
528
529
530
531
532
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 526

def column_calculation(column)
  if column.calculate.instance_of? Proc
    column.calculate.call(@records)
  else
    calculate_query.calculate(column.calculate, column.name)
  end
end

#column_class(column, column_value, record) ⇒ Object



484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 484

def column_class(column, column_value, record)
  @_column_classes ||= {}
  @_column_classes[column.name] ||= begin
    classes = "#{column.name}-column "
    classes << 'sorted ' if active_scaffold_config.actions.include?(:list) && active_scaffold_config.list.user.sorting.sorts_on?(column)
    classes << 'numeric ' if column.number?
    classes << column.css_class unless column.css_class.nil? || column.css_class.is_a?(Proc)
  end
  classes = "#{@_column_classes[column.name]} "
  classes << 'empty ' if column_empty? column_value
  classes << 'in_place_editor_field ' if inplace_edit?(record, column) || column.list_ui == :marked
  if column.css_class.is_a?(Proc)
    css_class = column.css_class.call(column_value, record)
    classes << css_class unless css_class.nil?
  end
  classes
end

#column_empty?(column_value) ⇒ Boolean

Returns:

  • (Boolean)


515
516
517
518
519
520
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 515

def column_empty?(column_value)
  empty = column_value.nil?
  empty ||= false != column_value && column_value.blank?
  empty ||= ['&nbsp;', empty_field_text].include? column_value if column_value.is_a? String
  empty
end

#column_heading_class(column, sorting) ⇒ Object



502
503
504
505
506
507
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 502

def column_heading_class(column, sorting)
  classes = "#{column.name}-column_heading "
  classes << "sorted #{sorting.direction_of(column).downcase} " if sorting.sorts_on? column
  classes << column.css_class unless column.css_class.nil? || column.css_class.is_a?(Proc)
  classes
end

Returns:

  • (Boolean)


210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 210

def column_link_authorized?(link, column, record, associated)
  if column.association
    associated_for_authorized =
      if column.plural_association? || (associated.respond_to?(:blank?) && associated.blank?)
        column.association.klass
      else
        associated
      end
    authorized = associated_for_authorized.authorized_for?(:crud_type => link.crud_type)
    authorized &&= record.authorized_for?(:crud_type => :update, :column => column.name) if link.crud_type == :create
    authorized
  else
    action_link_authorized?(link, record)
  end
end


181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 181

def configure_column_link(link, record, associated, actions = nil)
  actions ||= link.column.actions_for_association_links
  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 ||= as_(: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)
    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

#controller_path_for_activerecord(klass) ⇒ Object

Uncategorized



37
38
39
40
41
42
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 37

def controller_path_for_activerecord(klass)
  controller = active_scaffold_controller_for(klass)
  controller.controller_path
rescue ActiveScaffold::ControllerNotFound
  nil
end


133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 133

def display_action_link(link, content, record, options)
  if content
    html_classes = hover_via_click? ? 'hover_click ' : ''
    if options[:level] == 0
      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 if html_classes.present?), :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] == 0
  end
  content = (options[:level_0_tag], content, options[:options_level_0_tag]) if options[:level] == 0 && options[:level_0_tag]
  content
end


105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 105

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 = action_link_authorized?(link, *Array(options[:for]))
      next if !authorized && options[:skip_unauthorized]
      output << display_action_link(link, nil, record, options.merge(:authorized => authorized))
      options[:first_action] = false
    end
  end
  output
end

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



96
97
98
99
100
101
102
103
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 96

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
    links.map { |link|  :li, link }.join('').html_safe
  end
  raw "ActiveScaffold.display_dynamic_action_group('#{get_action_link_id action_link, record}', '#{escape_javascript html}');"
end

#display_message(message) ⇒ Object



574
575
576
577
578
579
580
581
582
583
584
585
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 574

def display_message(message)
  if (highlights = active_scaffold_config.highlight_messages)
    message = highlights.inject(message) do |msg, (phrases, highlighter)|
      highlight(msg, phrases, highlighter || {})
    end
  end
  if (format = active_scaffold_config.timestamped_messages)
    format = :short if format == true
    message = "#{ :div, l(Time.current, :format => format), :class => 'timestamp'} #{ :div, message, :class => 'message-content'}".html_safe
  end
  message
end

#empty_field_textObject



522
523
524
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 522

def empty_field_text
  active_scaffold_config.list.empty_field_text if active_scaffold_config.actions.include?(:list)
end

#form_remote_upload_tag(url_for_options = {}, options = {}) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 60

def form_remote_upload_tag(url_for_options = {}, options = {})
  options[:target] = action_iframe_id(url_for_options)
  options[:multipart] ||= true
  options[:class] = "#{options[:class]} as_remote_upload".strip
  output = ''
  output << form_tag(url_for_options, options)
  (output << "<iframe id='#{action_iframe_id(url_for_options)}' name='#{action_iframe_id(url_for_options)}' style='display:none'></iframe>").html_safe
end

#format_column_calculation(column, calculation) ⇒ Object



541
542
543
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 541

def format_column_calculation(column, calculation)
  "#{"#{as_(column.calculate)}: " unless column.calculate.is_a? Proc}#{format_column_value nil, column, calculation}"
end


415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 415

def get_action_link_id(link, record = nil, column = nil)
  column ||= link.column
  if column && column.plural_association?
    id = "#{column.association.name}-#{record.id}"
  elsif column && column.singular_association?
    if record.try(column.association.name.to_sym).present?
      id = "#{column.association.name}-#{record.send(column.association.name).id}-#{record.id}"
    else
      id = "#{column.association.name}-#{record.id}" unless record.nil?
    end
  end
  id ||= record.try(:id) || (nested? ? nested_parent_id : '')
  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

#is_sti_record?(record) ⇒ Boolean

Returns:

  • (Boolean)


226
227
228
229
230
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 226

def is_sti_record?(record)
  model = active_scaffold_config.model
  record && model.columns_hash.include?(model.inheritance_column) &&
    record[model.inheritance_column].present? && !record.instance_of?(model)
end

Creates a javascript-based link that toggles the visibility of some element on the page. By default, it toggles the visibility of the sibling after the one it’s nested in. You may pass custom javascript logic in options to change that, though. For example, you could say :of => ‘$(“my_div_id”)’. You may also flag whether the other element is visible by default or not, and the initial text will adjust accordingly.



77
78
79
80
81
82
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 77

def link_to_visibility_toggle(id, options = {})
  options[:default_visible] = true if options[:default_visible].nil?
  options[:hide_label] ||= as_(:hide)
  options[:show_label] ||= as_(:show_block)
  link_to options[:default_visible] ? options[:hide_label] : options[:show_label], '#', :data => {:show => options[:show_label], :hide => options[:hide_label], :toggable => id}, :style => 'display: none;', :class => 'as-js-button visibility-toggle'
end

#list_row_class(record) ⇒ Object



473
474
475
476
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 473

def list_row_class(record)
  class_override_helper = list_row_class_method(record)
  class_override_helper ? send(class_override_helper, record) : ''
end

#list_row_class_method(record) ⇒ Object



467
468
469
470
471
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 467

def list_row_class_method(record)
  return @_list_row_class_method if defined? @_list_row_class_method
  class_override_helper = "#{clean_class_name(record.class.name)}_list_row_class"
  @_list_row_class_method = (class_override_helper if respond_to?(class_override_helper))
end

#loading_indicator_tag(options) ⇒ Object

a general-use loading indicator (the “stuff is happening, please wait” feedback)



70
71
72
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 70

def loading_indicator_tag(options)
  image_tag 'active_scaffold/indicator.gif', :style => 'visibility:hidden;', :id => loading_indicator_id(options), :alt => 'loading indicator', :class => 'loading-indicator'
end

#option_tags_for(select_options, options = {}) ⇒ Object

Turns [[label, value]] into <option> tags Takes optional parameter of :include_blank



52
53
54
55
56
57
58
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 52

def option_tags_for(select_options, options = {})
  select_options.insert(0, [as_(:_select_), nil]) if options[:include_blank]
  select_options.collect do |option|
    label, value = option[0], option[1]
    value.nil? ? '<option value='">#{label}</option>" : "<option value=\"#{value}\">#{label}</option>"
  end
end

#override_helper(column, suffix) ⇒ Object



558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 558

def override_helper(column, suffix)
  hash = @_override_helpers ||= {}
  hash = hash[suffix] ||= {}
  hash = hash[column.active_record_class.name] ||= {}
  return hash[column.name] if hash.include? column.name
  hash[column.name] = begin
    method_with_class = override_helper_name(column, suffix, true)
    if respond_to?(method_with_class)
      method_with_class
    else
      method = override_helper_name(column, suffix)
      method if respond_to?(method)
    end
  end
end

#override_helper_name(column, suffix, class_prefix = false) ⇒ Object

the naming convention for overriding with helpers



554
555
556
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 554

def override_helper_name(column, suffix, class_prefix = false)
  "#{clean_class_name(column.active_record_class.name) + '_' if class_prefix}#{clean_column_name(column.name)}_#{suffix}"
end


281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 281

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 NESTED_PARAMS.include?(key) || conditions_from_params.include?(key) || (nested? && nested.param_name == key)
      non_nested_query_string_options[key] = value
    else
      query_string_options[key] = value
    end
  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


154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 154

def render_action_link(link, record = nil, options = {})
  if link.action.nil? || link.column.try(:polymorphic_association?)
    link = action_link_to_inline_form(link, record)
    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])
    action_link_html(link, nil, {:link => action_link_text(link, options), :class => "disabled #{link.action}#{" #{link.html_options[:class]}" unless link.html_options[:class].blank?}"}, 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

#render_column_calculation(column) ⇒ Object



534
535
536
537
538
539
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 534

def render_column_calculation(column)
  calculation = column_calculation(column)
  override_formatter = "render_#{column.name}_#{column.calculate.is_a?(Proc) ? :calculate : column.calculate}"
  calculation = send(override_formatter, calculation) if respond_to? override_formatter
  format_column_calculation(column, calculation)
end


248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 248

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.try(:singular_association?)
    child_id = record.send(link.column.association.name).try(: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


356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 356

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.try(:singular_association?)
    replacements['--CHILD_ID--'] = record.send(link.column.association.name).try(: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)


84
85
86
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 84

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

#template_exists?(template_name, partial = false) ⇒ Boolean

This is the template finder logic, keep it updated with however we find stuff in rails currently this very similar to the logic in ActionBase::Base.render for options file

Returns:

  • (Boolean)


46
47
48
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 46

def template_exists?(template_name, partial = false)
  lookup_context.exists? template_name, '', partial
end


441
442
443
444
445
446
447
448
449
450
451
452
453
454
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 441

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


456
457
458
459
460
461
462
463
464
465
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 456

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