Sha256: 92482d9c165fb6a77bda3ca8de7f6cc5e7c5b737286588fd67311452145938c1

Contents?: true

Size: 1.21 KB

Versions: 2

Compression:

Stored size: 1.21 KB

Contents

module ActiveAdmin
  module Axlsx
    module ResourceControllerExtension
      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

      # patching the index method to allow the xlsx format.
      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.to_stream.read, :filename => "#{xlsx_filename}", :type => Mime::Type.lookup_by_extension(:xlsx)
          end
        end
      end

      # patching per_page to use the CSV record max for pagination when the format is xlsx
      def per_page_with_xlsx
          if request.format ==  Mime::Type.lookup_by_extension(:xlsx)
            return max_csv_records
          end
          per_page_without_xlsx
      end

      # Returns a filename for the xlsx file using the collection_name
      # and current date such as 'my-articles-2011-06-24.xlsx'.
      def xlsx_filename
        "#{resource_collection_name.to_s.gsub('_', '-')}-#{Time.now.strftime("%Y-%m-%d")}.xlsx"
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
activeadmin-axlsx-2.0.1 lib/active_admin/axlsx/resource_controller_extension.rb
activeadmin-axlsx-2.0.0 lib/active_admin/axlsx/resource_controller_extension.rb