class Para::Exporter::Base

Attributes

model[R]
name[R]
options[R]

Public Class Methods

params_whitelist() click to toggle source

Override in subclass to add a params whitelist that are fetched from the request before initializing the exporter

For example, if you want to export posts for a given category, you can add the ‘:category_id` param to your export link, and whitelist this param here with :

def self.params_whitelist
  [:category_id]
end

It will be passed from the controller to the importer so it can be used to scope resources before exporting.

Note that you’ll manually need to scope the resources by overriding the resources method.

If you need automatic scoping, please use the ‘:q` param that accepts ransack search params and applies it to the resources.

# File lib/para/exporter/base.rb, line 110
def self.params_whitelist
  []
end

Public Instance Methods

file() click to toggle source
# File lib/para/exporter/base.rb, line 34
def file
  @file ||= GlobalID::Locator.locate(store(:file_gid))
end
file_name() click to toggle source
# File lib/para/exporter/base.rb, line 38
def file_name
  @file_name ||= [name, extension].join
end
perform(model_name: nil, **options) click to toggle source
# File lib/para/exporter/base.rb, line 6
def perform(model_name: nil, **options)
  @model = model_name && model_name.constantize
  @options = options
  @name = model.try(:model_name).try(:route_key).try(:parameterize)

  # Render file and store it in a Library::File object, allowing us
  # to retrieve that file easily from the job and subsequent requests
  #
  file = Para::Library::File.new

  if file.respond_to?(:attachment?)
    file.attachment = render
  else
    file.attachment.attach(
      io: render,
      filename: file_name,
      content_type: mime_type
    )
  end

  file.save!

  store(:file_gid, file.to_global_id)

  # Ensure that `.perform_now` returns the exporter
  self
end

Private Instance Methods

binary?() click to toggle source

Default to writing string data to the exported file, allowing subclasses to write binary data if needed

# File lib/para/exporter/base.rb, line 58
def binary?
  false
end
encode(string) click to toggle source
# File lib/para/exporter/base.rb, line 86
def encode(string)
  string.presence && string.to_s.encode('UTF-8', invalid: :replace, undef: :replace, replace: '?')
end
generate() click to toggle source
# File lib/para/exporter/base.rb, line 52
def generate
  fail NotImplementedError
end
params() click to toggle source
# File lib/para/exporter/base.rb, line 114
def params
  @params ||= options.delete(:params)
end
render() click to toggle source
# File lib/para/exporter/base.rb, line 44
def render
  Tempfile.new([name, extension]).tap do |file|
    file.binmode if binary?
    file.write(generate)
    file.rewind
  end
end
resources() click to toggle source

Allow passing a ‘:resources` option or a ransack search hash to filter exported resources

If the provided :search option is a string, we consider it to be a query string and try parsing it with Rack::Utils#parse_nested_query

# File lib/para/exporter/base.rb, line 72
def resources
  @resources ||= if options[:resources]
    options[:resources]
  elsif options[:search]
    if options[:search].is_a?(String)
      options[:search] = Rack::Utils.parse_nested_query(options[:search])
    end

    model.ransack(options[:search]).result
  else
    model.all
  end
end
total_progress() click to toggle source
# File lib/para/exporter/base.rb, line 62
def total_progress
  resources.length
end