module ActiveAdmin::Axlsx::ResourceControllerExtension

Public Class Methods

included(base) click to toggle source
# File lib/active_admin/axlsx/resource_controller_extension.rb, line 4
def self.included(base)
  base.send :alias_method_chain, :per_page, :xlsx
  base.send :alias_method_chain, :index, :xlsx
  base.send :respond_to, :xlsx
end

Public Instance Methods

index_with_xlsx(options={}, &block) click to toggle source

patching the index method to allow the xlsx format.

# File lib/active_admin/axlsx/resource_controller_extension.rb, line 11
def index_with_xlsx(options={}, &block)
  index_without_xlsx(options) do |format|
     format.xlsx do
      xlsx = active_admin_config.xlsx_builder.serialize(collection)
      send_data xlsx, :filename => "#{xlsx_filename}", :type => Mime::Type.lookup_by_extension(:xlsx)
    end
  end
end
per_page_with_xlsx() click to toggle source

patching per_page to use the CSV record max for pagination when the format is xlsx

# File lib/active_admin/axlsx/resource_controller_extension.rb, line 21
def per_page_with_xlsx
    if request.format ==  Mime::Type.lookup_by_extension(:xlsx)
      return max_csv_records
    end
    per_page_without_xlsx
end
xlsx_filename() click to toggle source

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

# File lib/active_admin/axlsx/resource_controller_extension.rb, line 30
def xlsx_filename
  "#{resource_collection_name.to_s.gsub('_', '-')}-#{Time.now.strftime("%Y-%m-%d")}.xlsx"
end