/activeadmin-xls

ActiveAdmin plugin for adding Excel (xls) download links for your resources

Primary LanguageRubyMIT LicenseMIT

Active Admin Xls

Excel Spreadsheet Export for Active Admin

Version Travis CI Quality Coverage Inch CI

Synopsis

This gem provides xls downloads for Active Admin resources.

This gem borrows heavily from activeadmin-axlsx and to_xls.

Usage

Add the following to your Gemfile. All resource index views will now include a link for download directly to xls.

gem 'activeadmin-xls', '~>2.0.0'

For Active Admin 1.0 and above, you will also have to update config/initializers/active_admin.rb. Update the download_links setting to include xls:

config.download_links = %i[csv xml json xls]

Dependencies

This gem depends on spreadsheet to generate xls files.

Examples

Here are a few quick examples of things you can easily tweak.

Localize column headers

# app/admin/posts.rb
ActiveAdmin.register Post do
  config.xls_builder.i18n_scope = [:active_record, :models, :posts]
end

Use blocks for adding computed fields

# app/admin/posts.rb
ActiveAdmin.register Post do
  config.xls_builder.column('author_name') do |resource|
    resource.author.name
  end
end

Change the column header format

# app/admin/posts.rb
ActiveAdmin.register Post do
  config.xls_builder.header_format = { weight: :bold,
                                       color: :blue }
end

Remove columns

# app/admin/posts.rb
ActiveAdmin.register Post do
  config.xls_builder.delete_columns :id, :created_at, :updated_at
end

Restrict columns to a list

# app/admin/posts.rb
ActiveAdmin.register Post do
  config.xls_builder.only_columns :title, :author
end

Using the DSL

Everything that you do with the config's default builder can be done via the resource DSL.

Below is an example of the DSL

ActiveAdmin.register Post do

  # i18n_scope and header style are set via options
  xls(i18n_scope: [:active_admin, :xls, :post],
      header_format: { weight: :bold, color: :blue }) do

    # Specify that you want to white list column output.
    # whitelist

    # Do not serialize the header, only output data.
    # skip_header

    # restrict columns to a list without customization
    # only_columns :title, :author

    # deleting columns from the report
    delete_columns :id, :created_at, :updated_at

    # adding a column to the report with customization
    column(:author) { |post| "#{post.author.first_name} #{post.author.last_name}" }

    # inserting additional data with after_filter
    after_filter do |sheet|
      # todo
    end

    # inserting data with before_filter
    before_filter do |sheet|
      # todo
    end
  end
end

Testing

Running specs for this gem requires that you construct a rails application.

To execute the specs, navigate to the gem directory, run bundle install and run these to rake tasks:

Rails 4.2

bundle install --gemfile=gemfiles/rails_42.gemfile
BUNDLE_GEMFILE=gemfiles/rails_42.gemfile bundle exec rake setup
BUNDLE_GEMFILE=gemfiles/rails_42.gemfile bundle exec rake

Rails 5.2

bundle install --gemfile=gemfiles/rails_52.gemfile
BUNDLE_GEMFILE=gemfiles/rails_52.gemfile bundle exec rake setup
BUNDLE_GEMFILE=gemfiles/rails_52.gemfile bundle exec rake

Rails 6.0

bundle install --gemfile=gemfiles/rails_60.gemfile
BUNDLE_GEMFILE=gemfiles/rails_60.gemfile bundle exec rake setup
BUNDLE_GEMFILE=gemfiles/rails_60.gemfile bundle exec rake