Module: ActiveScaffold::Helpers::ExportHelpers

Defined in:
lib/active_scaffold/helpers/export_helpers.rb

Overview

Helpers that assist with the rendering of a Export Column

Instance Method Summary collapse

Instance Method Details

#export_column_header_style(column, format) ⇒ Object

This helper can be overridden to change the style of the headers



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

def export_column_header_style(column, format)
  if format == :xlsx
    @default_style ||= {sz: 11, b: true, bg_color: "69B5EF", fg_color: "FF", alignment: {horizontal: :center}}
  end
end

#export_column_override(column) ⇒ Object



41
42
43
# File 'lib/active_scaffold/helpers/export_helpers.rb', line 41

def export_column_override(column)
  override_helper column, 'export_column'
end

#export_column_style(column, format) ⇒ Object



36
37
38
39
# File 'lib/active_scaffold/helpers/export_helpers.rb', line 36

def export_column_style(column, format)
  style = column.export_options&.dig(format)
  format = :xlsx && style.frozen? ? style.deep_dup : style
end

#format_export_column(raw_value, format) ⇒ Object



54
55
56
57
# File 'lib/active_scaffold/helpers/export_helpers.rb', line 54

def format_export_column(raw_value, format)
  method = "format_value_for_#{format}"
  respond_to?(method) ? send(method, raw_value) : raw_value
end

#format_export_column_header_name(column) ⇒ Object

This helper can be overridden to change the name of the headers For instance, you might want column.name.to_s.humanize



83
84
85
# File 'lib/active_scaffold/helpers/export_helpers.rb', line 83

def format_export_column_header_name(column)
  column.label
end

#format_plural_association_export_column(association_records, format) ⇒ Object



75
76
77
78
79
# File 'lib/active_scaffold/helpers/export_helpers.rb', line 75

def format_plural_association_export_column(association_records, format)
  firsts = association_records.first(4).collect { |v| v.to_label }
  firsts[3] = ' ' if firsts.length == 4
  format_value(firsts.join(','))
end

#format_singular_association_export_column(association_record, format) ⇒ Object



71
72
73
# File 'lib/active_scaffold/helpers/export_helpers.rb', line 71

def format_singular_association_export_column(association_record, format)
  format_value(association_record.to_label)
end

#format_value_for_csv(column_value) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/active_scaffold/helpers/export_helpers.rb', line 59

def format_value_for_csv(column_value)
  if column_empty?(column_value)
    active_scaffold_config.list.empty_field_text
  elsif column_value.is_a?(Time) || column_value.is_a?(Date)
    l(column_value, :format => :default)
  elsif [FalseClass, TrueClass].include?(column_value.class)
    as_(column_value.to_s.to_sym)
  else
    column_value.to_s
  end
end

#get_export_column_value(record, column, format) ⇒ Object

individual columns can be overridden by defining a helper method <column_name>_export_column(record) You can customize the output of all columns by overriding the following helper methods: format_export_column(raw_value) format_singular_association_export_column(association_record) format_plural_association_export_column(association_records)



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/active_scaffold/helpers/export_helpers.rb', line 12

def get_export_column_value(record, column, format)
  if (method = export_column_override(column))
    value, options = send(method, record, format)
    [value, options || export_column_style(column, format)]
  elsif column.list_ui && (method = override_export_ui(column.list_ui))
    value, options = send(method, record, column, format, ui_options: column.list_ui_options || column.options)
    [value, options || export_column_style(column, format)]
  else
    raw_value = record.send(column.name)

    value =
      if column.association.nil? or column_empty?(raw_value)
        format_export_column(raw_value, format)
      elsif column.association
        if column.association.collection?
          format_plural_association_export_column(raw_value, format)
        else
          format_singular_association_export_column(raw_value, format)
        end
      end
    [value, export_column_style(column, format)]
  end
end

#override_export_ui(list_ui) ⇒ Object Also known as: override_export_ui?

the naming convention for overriding column types with helpers



46
47
48
49
50
51
# File 'lib/active_scaffold/helpers/export_helpers.rb', line 46

def override_export_ui(list_ui)
  ActiveScaffold::Registry.cache :export_ui_overrides, list_ui do
    method = "active_scaffold_export_#{list_ui}"
    method if respond_to? method
  end
end