module ActiveAdmin::Xls::ResourceControllerExtension

Extends the resource controller to respond to xls requests

Public Class Methods

prepended(base) click to toggle source
# File lib/active_admin/xls/resource_controller_extension.rb, line 5
def self.prepended(base)
  base.send :respond_to, :xls, only: :index
end

Public Instance Methods

index() { |format| ... } click to toggle source

Patches index to respond to requests with xls mime type by sending a generated xls document serializing the current collection

Calls superclass method
# File lib/active_admin/xls/resource_controller_extension.rb, line 12
def index
  super do |format|
    format.xls do
      xls = active_admin_config.xls_builder.serialize(xls_collection,
                                                      view_context)
      send_data(xls,
                filename: xls_filename,
                type: Mime::Type.lookup_by_extension(:xls))
    end

    yield(format) if block_given?
  end
end
rescue_active_admin_access_denied(exception) click to toggle source

Patches rescue_active_admin_access_denied to respond to xls mime type. Provides administrators information on how to configure activeadmin to respond propertly to xls requests

param exception [Exception] unauthorized access error

Calls superclass method
# File lib/active_admin/xls/resource_controller_extension.rb, line 31
def rescue_active_admin_access_denied(exception)
  if request.format == Mime::Type.lookup_by_extension(:xls)
    respond_to do |format|
      format.xls do
        flash[:error] = "#{exception.message} Review download_links in initializers/active_admin.rb"
        redirect_backwards_or_to_root
      end
    end
  else
    super(exception)
  end
end
xls_collection() click to toggle source

Returns the collection to use when generating an xls file.

# File lib/active_admin/xls/resource_controller_extension.rb, line 54
def xls_collection
  find_collection except: :pagination
end
xls_filename() click to toggle source

Returns a filename for the xls file using the collection_name and current date such as 'my-articles-2011-06-24.xls'.

@return [String] with default filename

# File lib/active_admin/xls/resource_controller_extension.rb, line 48
def xls_filename
  timestamp = Time.now.strftime('%Y-%m-%d')
  "#{resource_collection_name.to_s.tr('_', '-')}-#{timestamp}.xls"
end