Skip to content

Helper Overrides

Sergio Cambra edited this page Jul 22, 2024 · 4 revisions

The format of exported data depends on the requested format to export.

When exporting to xlsx, columns are exported without formatting, but exporting to csv requires to convert some columns to string, using format_value_for_csv to convert them, which receives just the value to format, and it converts with the following rules:

  • active_scaffold_config.list.empty_field_text when value is empty
  • I18n.l with :default format when the value is a Time or Date
  • true and false are translated
  • to_s is called for other classes

Exporting associations works in the same way for any format when there are associated records:

  • For singular associations, it calls to_label in the associated record. The helper format_singular_association_export_column can be overrided to change the default behaviour for singular associations, it gets two arguments: the associated record and the requested format.
  • For collection associations, it exports the first three associated records, calling to_label and joining with ,. The helper format_plural_association_export_column can be overrided to change the default behaviour for singular associations, it gets two arguments: the associated records and the requested format.

Column Helper

If you want to customize the exported value of a column, you can define a specially named method in your helper file. The format is #{class_name}_#{column_name}_export_column or #{column_name}_export_column. So, for example, to customize the :username column displayed on your UsersController, you would add a user_username_export_column method to your UsersHelper file, or username_export_column which would be used by any model with username column if not using clear_helpers in the ApplicationController. If you want to override username columns in all models, add a username_export_column method to your ApplicationHelper file.

This override method accepts two arguments: the entire record object and the exported format, e.g. :csv or :xlsx. The method must return a value, and optionally a Hash with format options, see Cell Format Options.

UI Helper

If the column is using a list_ui, it's possible to define a helper method for export action matching the list_ui name, active_scaffold_export_#{list_ui}. This helper gets three arguments, record, column and format, and a named argument ui_options with the options defined with list_ui, or column.options.

For example, defining a list_ui :accounting:

module ApplicationHelper
  def active_scaffold_column_accounting(record, column, ui_options: column.options)
    value = record.send(column.name)
    style = 'color: red' if value.negative?
    content_tag(:span, style: style) do
      number_to_currency(value, ui_options.merge(negative_format: "(%u%n)", format: "%u%n"))
    end
  end

Then it can be used in a column:

conf.columns[:total].list_ui = :accounting

And a export UI helper can be defined so exported value gets the same format:

module ApplicationHelper
  def active_scaffold_export_accounting(record, column, format, ui_options: column.options)
    value = record.send(column.name)
    if format == :xlsx
      [value, {num_fmt: 8}] # 8: $#,##0.00;[Red]($#,##0.00) # export as number with format for Excel
    else
      number_to_currency(value, ui_options.merge(negative_format: "(%u%n)", format: "%u%n"))
    end
  end

And the list UI helper can be defined to use the CSV formatting to reduce duplicated code:

module ApplicationHelper
  def active_scaffold_column_accounting(record, column, ui_options: column.options)
    style = 'color: red' if record.send(column.name).negative?
    content_tag(:span, style: style) do
      active_scaffold_export_accounting(record, column, :csv, ui_options: ui_options)
    end
  end

Header label

The header labels can be overrided, to use something different than column.label by defining format_export_column_header_name, which gets one argument, column:

def format_export_column_header_name(column)
  if column == :comments
    'Internal Comments'
  else
    super
  end
end
Clone this wiki locally