module ActiveScaffold::Helpers::ListColumnHelpers

Helpers that assist with the rendering of a List Column

Helpers that assist with the rendering of a List Column

Public Instance Methods

active_scaffold_column_carrierwave(record, column) click to toggle source
# File lib/active_scaffold/bridges/carrierwave/list_ui.rb, line 4
def active_scaffold_column_carrierwave(record, column)
  carrierwave = record.send("#{column.name}")
  return nil if carrierwave.file.blank?
  thumbnail_style = ActiveScaffold::Bridges::Carrierwave::CarrierwaveBridgeHelpers.thumbnail_style
  content =
    if carrierwave.versions.keys.include?(thumbnail_style)
      image_tag(carrierwave.url(thumbnail_style), :border => 0).html_safe
    else
      record.send(record.send(:_mounter, column.name).send(:serialization_column))
    end
  link_to(content, carrierwave.url, :target => '_blank')
end
active_scaffold_column_checkbox(record, column) click to toggle source
# File lib/active_scaffold/helpers/list_column_helpers.rb, line 81
def active_scaffold_column_checkbox(record, column)
  options = {:disabled => true, :id => nil, :object => record}
  options.delete(:disabled) if inplace_edit?(record, column)
  check_box(:record, column.name, options)
end
active_scaffold_column_dragonfly(record, column) click to toggle source
# File lib/active_scaffold/bridges/dragonfly/list_ui.rb, line 4
def active_scaffold_column_dragonfly(record, column)
  attachment = record.send("#{column.name}")
  return nil unless attachment.present?
  content =
    if attachment.image?
      image_tag(attachment.thumb(column.options[:thumb] || ActiveScaffold::Bridges::Dragonfly::DragonflyBridgeHelpers.thumbnail_style).url, :border => 0)
    else
      attachment.name
    end
  link_to(content, dragonfly_url_for_attachment(attachment, record, column), :target => '_blank')
end
active_scaffold_column_fulltext(record, column) click to toggle source
# File lib/active_scaffold/helpers/list_column_helpers.rb, line 72
def active_scaffold_column_fulltext(record, column)
  clean_column_value(record.send(column.name))
end
active_scaffold_column_marked(record, column) click to toggle source
# File lib/active_scaffold/helpers/list_column_helpers.rb, line 76
def active_scaffold_column_marked(record, column)
  options = {:id => nil, :object => record}
  content_tag(:span, check_box(:record, column.name, options), :class => 'in_place_editor_field', :data => {:ie_id => record.to_param})
end
active_scaffold_column_paperclip(record, column) click to toggle source
# File lib/active_scaffold/bridges/paperclip/list_ui.rb, line 4
def active_scaffold_column_paperclip(record, column)
  paperclip = record.send("#{column.name}")
  return nil unless paperclip.file?
  content =
    if paperclip.styles.include?(ActiveScaffold::Bridges::Paperclip::PaperclipBridgeHelpers.thumbnail_style)
      image_tag(paperclip.url(ActiveScaffold::Bridges::Paperclip::PaperclipBridgeHelpers.thumbnail_style), :border => 0)
    else
      paperclip.original_filename
    end
  link_to(content, paperclip.url, :target => '_blank')
end
active_scaffold_column_text(record, column) click to toggle source

Overrides

# File lib/active_scaffold/helpers/list_column_helpers.rb, line 67
def active_scaffold_column_text(record, column)
  # `to_s` is necessary to convert objects in serialized columns to string before truncation.
  clean_column_value(truncate(record.send(column.name).to_s, :length => column.options[:truncate] || 50))
end
active_scaffold_column_thumbnail(record, column) click to toggle source
# File lib/active_scaffold/bridges/file_column/list_ui.rb, line 16
def active_scaffold_column_thumbnail(record, column)
  return nil if record.send(column.name).nil?
  link_to(
    image_tag(url_for_file_column(record, column.name.to_s, 'thumb'), :border => 0),
    url_for_file_column(record, column.name.to_s),
    :popup => true)
end
active_scaffold_inplace_edit(record, column, options = {}) click to toggle source
# File lib/active_scaffold/helpers/list_column_helpers.rb, line 223
def active_scaffold_inplace_edit(record, column, options = {})
  formatted_column = options[:formatted_column] || format_column_value(record, column)
  content_tag(:span, as_(:inplace_edit_handle), :class => 'handle') <<
    content_tag(:span, formatted_column, active_scaffold_inplace_edit_tag_options(record, column))
end
active_scaffold_inplace_edit_tag_options(record, column) click to toggle source
# File lib/active_scaffold/helpers/list_column_helpers.rb, line 215
def active_scaffold_inplace_edit_tag_options(record, column)
  id_options = {:id => record.id.to_s, :action => 'update_column', :name => column.name.to_s}
  tag_options = {:id => element_cell_id(id_options), :class => 'in_place_editor_field',
                 :title => as_(:click_to_edit), :data => {:ie_id => record.to_param}}
  tag_options[:data][:ie_update] = column.inplace_edit if column.inplace_edit != true
  tag_options
end
all_marked?() click to toggle source
# File lib/active_scaffold/helpers/list_column_helpers.rb, line 274
def all_marked?
  if active_scaffold_config.mark.mark_all_mode == :page
    @page.items.detect { |record| !marked_records.include?(record.id) }.nil?
  else
    marked_records.length >= @page.pager.count.to_i
  end
end
cache_association(association, column, size) click to toggle source
# File lib/active_scaffold/helpers/list_column_helpers.rb, line 189
def cache_association(association, column, size)
  # we are not using eager loading, cache firsts records in order not to query the database for whole association in a future
  if column.associated_limit.nil?
    logger.warn "ActiveScaffold: Enable eager loading for #{column.name} association to reduce SQL queries"
  elsif column.associated_limit > 0
    # load at least one record more, is needed to display '...'
    association.target = association.reader.limit(column.associated_limit + 1).select(column.select_associated_columns || "#{association.klass.quoted_table_name}.*").to_a
  elsif @cache_associations
    association.target = []
  end
end
clean_column_value(v) click to toggle source

There are two basic ways to clean a column's value: h() and sanitize(). The latter is useful when the column contains valid html data, and you want to just disable any scripting. People can always use field overrides to clean data one way or the other, but having this override lets people decide which way it should happen by default.

Why is it not a configuration option? Because it seems like a somewhat rare request. But it could eventually be an option in config.list (and config.show, I guess).

# File lib/active_scaffold/helpers/list_column_helpers.rb, line 60
def clean_column_value(v)
  h(v)
end
column_heading_attributes(column, sorting, sort_direction) click to toggle source
# File lib/active_scaffold/helpers/list_column_helpers.rb, line 290
def column_heading_attributes(column, sorting, sort_direction)
  {:id => active_scaffold_column_header_id(column), :class => column_heading_class(column, sorting), :title => strip_tags(column.description).presence}
end
column_heading_label(column) click to toggle source
# File lib/active_scaffold/helpers/list_column_helpers.rb, line 325
def column_heading_label(column)
  column.label
end
column_heading_value(column, sorting, sort_direction) click to toggle source
# File lib/active_scaffold/helpers/list_column_helpers.rb, line 307
def column_heading_value(column, sorting, sort_direction)
  if column.name == :as_marked
    mark_column_heading
  elsif column.sortable?
    options = {:id => nil, :class => 'as_sort',
               'data-page-history' => controller_id,
               :remote => true, :method => :get}
    url_options = params_for(:action => :index, :page => 1,
                             :sort => column.name, :sort_direction => sort_direction)
    unless active_scaffold_config.store_user_settings
      url_options.merge!(:search => search_params) if search_params.present?
    end
    link_to column_heading_label(column), url_options, options
  else
    content_tag(:p, column_heading_label(column))
  end
end
column_override(column) click to toggle source
# File lib/active_scaffold/helpers/list_column_helpers.rb, line 87
def column_override(column)
  override_helper column, 'column'
end
Also aliased as: column_override?
column_override?(column)
Alias for: column_override
dragonfly_url_for_attachment(attachment, record, column) click to toggle source
# File lib/active_scaffold/bridges/dragonfly/list_ui.rb, line 16
def dragonfly_url_for_attachment(attachment, record, column)
  url_method = column.options[:private_store] ? :url : :remote_url
  attachment.send(url_method)
end
format_association_value(value, column, size) click to toggle source
# File lib/active_scaffold/helpers/list_column_helpers.rb, line 164
def format_association_value(value, column, size)
  method = column.options[:label_method] || :to_label
  value =
    if column.association.collection?
      format_collection_association_value(value, column, method, size)
    elsif value
      format_singular_association_value(value, column, method)
    end
  format_value value
end
format_collection_association_value(value, column, label_method, size) click to toggle source
# File lib/active_scaffold/helpers/list_column_helpers.rb, line 141
def format_collection_association_value(value, column, label_method, size)
  if column.associated_limit.nil?
    firsts = value.collect(&label_method)
  elsif column.associated_limit == 0
    size if column.associated_number?
  else
    firsts = value.first(column.associated_limit)
    firsts.collect!(&label_method)
    firsts << '…' if value.size > column.associated_limit
    text = firsts.join(h(active_scaffold_config.list.association_join_text)).html_safe
    text << " (#{size})" if column.associated_number? && column.associated_limit && value.size > column.associated_limit
    text
  end
end
format_column_value(record, column, value = nil) click to toggle source

Formatting

# File lib/active_scaffold/helpers/list_column_helpers.rb, line 104
def format_column_value(record, column, value = nil)
  value ||= record.send(column.name) unless record.nil?
  if column.association.nil?
    if [:select, :radio].include?(column.form_ui) && column.options[:options]
      text, val = column.options[:options].find { |text, val| (val.nil? ? text : val).to_s == value.to_s }
      value = active_scaffold_translated_option(column, text, val).first if text
    end
    if value.is_a? Numeric
      format_number_value(value, column.options)
    else
      format_value(value, column.options)
    end
  else
    if column.plural_association?
      associated_size = value.size if column.associated_number? # get count before cache association
      cache_association(record.association(column.name), column, associated_size) unless value.loaded?
    end
    format_association_value(value, column, associated_size)
  end
end
format_number_value(value, options = {}) click to toggle source
# File lib/active_scaffold/helpers/list_column_helpers.rb, line 125
def format_number_value(value, options = {})
  value = case options[:format]
    when :size
      number_to_human_size(value, options[:i18n_options] || {})
    when :percentage
      number_to_percentage(value, options[:i18n_options] || {})
    when :currency
      number_to_currency(value, options[:i18n_options] || {})
    when :i18n_number
      send("number_with_#{value.is_a?(Integer) ? 'delimiter' : 'precision'}", value, options[:i18n_options] || {})
    else
      value
  end
  clean_column_value(value)
end
format_singular_association_value(value, column, label_method) click to toggle source
# File lib/active_scaffold/helpers/list_column_helpers.rb, line 156
def format_singular_association_value(value, column, label_method)
  if column.polymorphic_association?
    "#{value.class.model_name.human}: #{value.send(label_method)}"
  else
    value.send(label_method)
  end
end
format_value(column_value, options = {}) click to toggle source
# File lib/active_scaffold/helpers/list_column_helpers.rb, line 175
def format_value(column_value, options = {})
  value =
    if column_empty?(column_value)
      empty_field_text
    elsif column_value.is_a?(Time) || column_value.is_a?(Date)
      l(column_value, :format => options[:format] || :default)
    elsif [FalseClass, TrueClass].include?(column_value.class)
      as_(column_value.to_s.to_sym)
    else
      column_value.to_s
    end
  clean_column_value(value)
end
get_column_method(record, column) click to toggle source
# File lib/active_scaffold/helpers/list_column_helpers.rb, line 15
def get_column_method(record, column)
  # check for an override helper
  column.list_method ||= begin
    if (method = column_override(column))
      # we only pass the record as the argument. we previously also passed the formatted_value,
      # but mike perham pointed out that prohibited the usage of overrides to improve on the
      # performance of our default formatting. see issue #138.
      method
    # second, check if the dev has specified a valid list_ui for this column
    elsif column.list_ui && (method = override_column_ui(column.list_ui))
      method
    elsif column.column && (method = override_column_ui(column.column.type))
      method
    else
      :format_column_value
    end
  end
end
get_column_value(record, column) click to toggle source
# File lib/active_scaffold/helpers/list_column_helpers.rb, line 5
def get_column_value(record, column)
  method = get_column_method(record, column)
  value = send(method, record, column)
  value = '&nbsp;'.html_safe if value.nil? || value.blank? # fix for IE 6
  return value
rescue StandardError => e
  logger.error "#{e.class.name}: #{e.message} -- on the ActiveScaffold column = :#{column.name} in #{controller.class}, record: #{record.inspect}"
  raise e
end
inplace_edit?(record, column) click to toggle source

Inline Edit =

# File lib/active_scaffold/helpers/list_column_helpers.rb, line 205
def inplace_edit?(record, column)
  return unless column.inplace_edit
  editable = controller.send(:update_authorized?, record) if controller.respond_to?(:update_authorized?, true)
  editable || record.authorized_for?(:crud_type => :update, :column => column.name)
end
inplace_edit_cloning?(column) click to toggle source
# File lib/active_scaffold/helpers/list_column_helpers.rb, line 211
def inplace_edit_cloning?(column)
  column.inplace_edit != :ajax && (override_form_field?(column) || column.form_ui || (column.column && override_input?(column.column.type)))
end
inplace_edit_control(column) click to toggle source
# File lib/active_scaffold/helpers/list_column_helpers.rb, line 229
def inplace_edit_control(column)
  return unless inplace_edit?(active_scaffold_config.model, column) && inplace_edit_cloning?(column)
  old_record, @record = @record, active_scaffold_config.model.new # TODO: remove when relying on @record is removed
  column = column.clone
  column.options = column.options.clone
  column.form_ui = :select if column.association && column.form_ui.nil?
  options = active_scaffold_input_options(column).merge(:object => active_scaffold_config.model.new)
  options[:class] = "#{options[:class]} inplace_field"
  options[:"data-id"] = options[:id]
  options[:id] = nil
  content_tag(:div, active_scaffold_input_for(column, nil, options), :style => 'display:none;', :class => inplace_edit_control_css_class).tap do
    @record = old_record # TODO: remove when relying on @record is removed
  end
end
inplace_edit_control_css_class() click to toggle source
# File lib/active_scaffold/helpers/list_column_helpers.rb, line 244
def inplace_edit_control_css_class
  'as_inplace_pattern'
end
inplace_edit_data(column) click to toggle source
# File lib/active_scaffold/helpers/list_column_helpers.rb, line 248
def inplace_edit_data(column)
  data = {}
  data[:ie_url] = url_for(params_for(:action => 'update_column', :column => column.name, :id => '__id__'))
  data[:ie_cancel_text] = column.options[:cancel_text] || as_(:cancel)
  data[:ie_loading_text] = column.options[:loading_text] || as_(:loading)
  data[:ie_save_text] = column.options[:save_text] || as_(:update)
  data[:ie_saving_text] = column.options[:saving_text] || as_(:saving)
  data[:ie_rows] = column.options[:rows] || 5 if column.column.try(:type) == :text
  data[:ie_cols] = column.options[:cols] if column.options[:cols]
  data[:ie_size] = column.options[:size] if column.options[:size]
  data[:ie_use_html] = column.options[:use_html] if column.options[:use_html]

  if column.list_ui == :checkbox
    data[:ie_mode] = :inline_checkbox
  elsif inplace_edit_cloning?(column)
    data[:ie_mode] = :clone
  elsif column.inplace_edit == :ajax
    url = url_for(:controller => params_for[:controller], :action => 'render_field', :id => '__id__', :update_column => column.name)
    plural = column.plural_association? && !override_form_field?(column) && [:select, :record_select].include?(column.form_ui)
    data[:ie_render_url] = url
    data[:ie_mode] = :ajax
    data[:ie_plural] = plural
  end
  data
end
mark_column_heading() click to toggle source
# File lib/active_scaffold/helpers/list_column_helpers.rb, line 282
def mark_column_heading
  tag_options = {
    :id => "#{controller_id}_mark_heading",
    :class => 'mark_heading in_place_editor_field'
  }
  content_tag(:span, check_box_tag("#{controller_id}_mark_heading_span_input", '1', all_marked?), tag_options)
end
override_column_ui(list_ui) click to toggle source

the naming convention for overriding column types with helpers

# File lib/active_scaffold/helpers/list_column_helpers.rb, line 93
def override_column_ui(list_ui)
  @_column_ui_overrides ||= {}
  return @_column_ui_overrides[list_ui] if @_column_ui_overrides.include? list_ui
  method = "active_scaffold_column_#{list_ui}"
  @_column_ui_overrides[list_ui] = (method if respond_to? method)
end
Also aliased as: override_column_ui?
override_column_ui?(list_ui)
Alias for: override_column_ui
render_column_heading(column, sorting, sort_direction) click to toggle source
# File lib/active_scaffold/helpers/list_column_helpers.rb, line 294
def render_column_heading(column, sorting, sort_direction)
  tag_options = column_heading_attributes(column, sorting, sort_direction)
  if column.name == :as_marked
    tag_options[:data] = {
      :ie_mode => :inline_checkbox,
      :ie_url => url_for(params_for(:action => 'mark', :id => '__id__'))
    }
  else
    tag_options[:data] = inplace_edit_data(column) if column.inplace_edit
  end
  content_tag(:th, column_heading_value(column, sorting, sort_direction) + inplace_edit_control(column), tag_options)
end
render_list_column(text, column, record) click to toggle source

TODO: move empty_field_text and &nbsp; logic in here? TODO: we need to distinguish between the automatic links we create and the ones that the dev specified. some logic may not apply if the dev specified the link.

# File lib/active_scaffold/helpers/list_column_helpers.rb, line 36
def render_list_column(text, column, record)
  if column.link && !skip_action_link?(column.link, record)
    link = column.link
    associated = record.send(column.association.name) if column.association
    render_action_link(link, record, :link => text, :authorized => link.action.nil? || column_link_authorized?(link, column, record, associated))
  elsif inplace_edit?(record, column)
    active_scaffold_inplace_edit(record, column, :formatted_column => text)
  elsif active_scaffold_config.actions.include?(:list) && active_scaffold_config.list.wrap_tag
    content_tag active_scaffold_config.list.wrap_tag, text
  else
    text
  end
rescue StandardError => e
  logger.error "#{e.class.name}: #{e.message} -- on the ActiveScaffold column = :#{column.name} in #{controller.class}"
  raise e
end