class AdministrateExportable::ExporterService

Attributes

dashboard[R]
resource_class[R]
resources[R]
sanitizer[R]

Public Class Methods

csv(dashboard, resource_class, resources) click to toggle source
# File lib/administrate_exportable/exporter_service.rb, line 5
def self.csv(dashboard, resource_class, resources)
  new(dashboard, resource_class, resources).csv
end
new(dashboard, resource_class, resources) click to toggle source
# File lib/administrate_exportable/exporter_service.rb, line 9
def initialize(dashboard, resource_class, resources)
  @dashboard = dashboard
  @resource_class = resource_class
  @resources = resources
end

Public Instance Methods

csv() click to toggle source
# File lib/administrate_exportable/exporter_service.rb, line 15
def csv
  CSV.generate(headers: true) do |csv|

    csv << headers

    collection.find_each do |record|
      csv << attributes_to_export.map do |attribute_key, attribute_type|
        record_attribute(record, attribute_key, attribute_type)
      end
    end
  end
end

Private Instance Methods

attributes_to_export() click to toggle source
# File lib/administrate_exportable/exporter_service.rb, line 75
def attributes_to_export
  @attributes_to_export ||= begin
    dashboard.class::ATTRIBUTE_TYPES.select do |attribute_key, attribute_type|
      attribute_options = attribute_type.try(:options)

      !attribute_options || attribute_options[:export] != false
    end
  end
end
collection() click to toggle source
# File lib/administrate_exportable/exporter_service.rb, line 85
def collection
  resources
end
headers() click to toggle source
# File lib/administrate_exportable/exporter_service.rb, line 60
def headers
  attributes_to_export.map do |attribute_key, _|
    attr_key = attribute_key.to_s

    if attr_key.include?('_id')
      attr_key
    else
      I18n.t(
        "helpers.label.#{resource_class.name}.#{attr_key}",
        default: attr_key,
      ).titleize
    end
  end
end
record_attribute(record, attribute_key, attribute_type) click to toggle source
# File lib/administrate_exportable/exporter_service.rb, line 32
def record_attribute(record, attribute_key, attribute_type)
  field = attribute_type.new(attribute_key, record.send(attribute_key), 'index', resource: record)
  transform_on_export = attribute_type.respond_to?(:options) && attribute_type.options[:transform_on_export]

  if transform_on_export.is_a? Proc
    return transform_on_export.call(field)
  end

  case field.class.to_s
  when Administrate::Field::BelongsTo.to_s, Administrate::Field::HasOne.to_s, Administrate::Field::Polymorphic.to_s
    field.display_associated_resource if field.data
  when Administrate::Field::HasMany.to_s
    field.data.count if field.data
  when Administrate::Field::DateTime.to_s
    field.datetime if field.data
  when Administrate::Field::Date.to_s
    field.date if field.data
  when Administrate::Field::Email.to_s, Administrate::Field::Select.to_s
    field.data
  when Administrate::Field::Password.to_s, Administrate::Field::String.to_s, Administrate::Field::Text.to_s
    field.truncate
  when Administrate::Field::Time.to_s
    field.data.strftime("%I:%M%p").to_s if field.data
  else
    field.to_s
  end
end