Module: ActiveAdmin::Xls::ResourceControllerExtension

Defined in:
lib/active_admin/xls/resource_controller_extension.rb

Overview

Extends the resource controller to respond to xls requests

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.prepended(base) ⇒ Object



5
6
7
# File 'lib/active_admin/xls/resource_controller_extension.rb', line 5

def self.prepended(base)
  base.send :respond_to, :xls, only: :index
end

Instance Method Details

#indexObject

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



12
13
14
15
16
17
18
19
20
21
22
23
24
# 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) ⇒ Object

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



31
32
33
34
35
36
37
38
39
40
41
42
# 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_collectionObject

Returns the collection to use when generating an xls file.



54
55
56
# File 'lib/active_admin/xls/resource_controller_extension.rb', line 54

def xls_collection
  find_collection except: :pagination
end

#xls_filenameString

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

Returns:

  • (String)

    with default filename



48
49
50
51
# 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