class Para::Exporter::Base
Attributes
Public Class Methods
Source
# File lib/para/exporter/base.rb, line 110 def self.params_whitelist [] end
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.
Public Instance Methods
Source
# File lib/para/exporter/base.rb, line 34 def file @file ||= GlobalID::Locator.locate(store(:file_gid)) end
Source
# File lib/para/exporter/base.rb, line 38 def file_name @file_name ||= [name, extension].join end
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
Source
# File lib/para/exporter/base.rb, line 58 def binary? false end
Default to writing string data to the exported file, allowing subclasses to write binary data if needed
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
Source
# File lib/para/exporter/base.rb, line 52 def generate fail NotImplementedError end
Source
# File lib/para/exporter/base.rb, line 114 def params @params ||= options.delete(:params) end
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
Source
# 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
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
Source
# File lib/para/exporter/base.rb, line 62 def total_progress resources.length end