Module: ActiveAdmin::Xls::ResourceControllerExtension

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



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

def self.included(base)
  base.send :alias_method_chain, :per_page, :xls
  base.send :alias_method_chain, :index, :xls
  base.send :alias_method_chain, :rescue_active_admin_access_denied, :xls
  base.send :respond_to, :xls
end

Instance Method Details

#index_with_xlsObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/active_admin/xls/resource_controller_extension.rb', line 11

def index_with_xls
  index_without_xls do |format|
    format.xls do
      xls_collection = if method(:find_collection).arity.zero?
                         collection
                       else
                         find_collection except: :pagination
                       end
      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

#per_page_with_xlsObject

patch per_page to use the CSV record max for pagination when the format is xls



47
48
49
50
51
52
53
54
# File 'lib/active_admin/xls/resource_controller_extension.rb', line 47

def per_page_with_xls
  if request.format == Mime::Type.lookup_by_extension(:xls)
    return max_per_page if respond_to?(:max_per_page, true)
    active_admin_config.max_per_page
  end

  per_page_without_xls
end

#rescue_active_admin_access_denied_with_xls(exception) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/active_admin/xls/resource_controller_extension.rb', line 32

def rescue_active_admin_access_denied_with_xls(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
    rescue_active_admin_access_denied_without_xls(exception)
  end
end

#xls_filenameObject

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



58
59
60
61
# File 'lib/active_admin/xls/resource_controller_extension.rb', line 58

def xls_filename
  timestamp = Time.now.strftime('%Y-%m-%d')
  "#{resource_collection_name.to_s.tr('_', '-')}-#{timestamp}.xls"
end