module ActiveScaffold::Helpers::ViewHelpers

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

Constants

NESTED_PARAMS

Public Instance Methods

active_scaffold_config() click to toggle source

access to the configuration variable

# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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 ? content_tag(:li, msg) : msg
      end
    end
    error_messages =
      if options[:list_type] == :br
        error_messages.join('<br/>').html_safe
      else
        content_tag(options[:list_type], error_messages.join.html_safe)
      end

    contents = []
    contents << content_tag(options[:header_tag] || :h2, header_message) unless header_message.blank?
    contents << content_tag(:p, message) unless message.blank?
    contents << error_messages
    contents = contents.join.html_safe
    options[:container_tag] ? content_tag(options[:container_tag], contents, html) : contents
  end
end
add_query_string_to_cached_url(link, url) click to toggle source
# 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_class() click to toggle source
# 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
clean_class_name(name) click to toggle source
# File lib/active_scaffold/helpers/view_helpers.rb, line 549
def clean_class_name(name)
  name.underscore.gsub('/', '_')
end
clean_column_name(name) click to toggle source
# File lib/active_scaffold/helpers/view_helpers.rb, line 545
def clean_column_name(name)
  name.to_s.gsub('?', '')
end
column_attributes(column, record) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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
controller_path_for_activerecord(klass) click to toggle source

Uncategorized

# 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
display_dynamic_action_group(action_link, links, record_or_ul_options = nil, ul_options = nil) click to toggle source
# 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 = content_tag :ul, ul_options do
    links.map { |link| content_tag :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) click to toggle source
# 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 = "#{content_tag :div, l(Time.current, :format => format), :class => 'timestamp'} #{content_tag :div, message, :class => 'message-content'}".html_safe
  end
  message
end
empty_field_text() click to toggle source
# 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 = {}) click to toggle source
# 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) click to toggle source
# 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
is_sti_record?(record) click to toggle source
# 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
list_row_class(record) click to toggle source
# 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) click to toggle source
# 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) click to toggle source

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

# 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 = {}) click to toggle source

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

# 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) click to toggle source
# 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) click to toggle source

the naming convention for overriding with helpers

# 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
render_column_calculation(column) click to toggle source
# 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
template_exists?(template_name, partial = false) click to toggle source

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

# File lib/active_scaffold/helpers/view_helpers.rb, line 46
def template_exists?(template_name, partial = false)
  lookup_context.exists? template_name, '', partial
end