Giter Club home page Giter Club logo

activeadmin-xls's Introduction

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

activeadmin-xls's People

Contributors

cprodhomme avatar cpunion avatar ejaypcanaria avatar jundihuang avatar rewritten avatar shanser avatar thambley avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

activeadmin-xls's Issues

Security issue with xls_builder retaining data even if template render fails to an exception on a computed field

There seems to be a possibly high impact issue with the way the xls_builder handles rendering the column contents. Steps to reproduce:

  1. Have the following xls template for an ActiveAdmin resource
  xls do
    whitelist
    column :name do |key|
      custom_helper_that_raises_exception(key)
    end
  end
  1. Have the ActiveAdmin resource filtered in to view A and view B so that
  • Items from viewA raise no exception with the custom helper
  • Some item(s) from viewB raises an exception.
  1. First export viewB as xls and have the server throw an error. Now export viewA as xls.

Observed: viewA.xls contains data from viewB
Expected: Every export should be from a clean slate, so viewA.xls should only contain data from viewA.

Haven't had the chance to see if the issue is with my versions, this gem, or the underlying spreadsheet gem. This does pose a security issue with using custom helpers that might throw an uncaught error, thus leaking data between exports.

Versions used:
Ruby 2.3.7p456, Rails 5.0.7.1, ActiveAdmin 1.2.1, ActiveAdmin-xls 2.0.0, Spreadsheet 1.1.7

only_columns in addition to delete_columns

A nice feature would be the abilty to name the columns in the order they should appear in the file. As I understand it, it is only possible to delete columns, eg:

config.xls_builder.delete_columns :id, :created_at, :updated_at, :category_name, :epfl_id_inst, :epfl_id_transact, :epfl_result, :epfl_date_cr, :epfl_payment_cd, :epfl_paymode, :epfl_payment_status_cd, :

It would be nice to be able to also do this

 config.xls_builder.only_columns :first_name, :last_name, :organization, :email, :payment

ActiveAdmin::DatabaseHitDuringLoad when migrating the database.

The code at https://github.com/thambley/activeadmin-xls/blob/master/lib/active_admin/xls/builder.rb#L217 is trying to connect to the database even in situations where it isn't advisable (example: assets:precompile, db:migrate, db:schema:load)

Surrounding the corresponding part of the configuration (both with the DSL and with the explicit builder methods) and rescueing a ActiveRecord::StatementInvalid exception is a temporary workaround but it might be a problem in production.

This issue in ActiveAdmin is similar: activeadmin/activeadmin#2342

rails 5 deprecation alias_method_chain

DEPRECATION WARNING: alias_method_chain is deprecated. Please, use Module#prepend instead. From module, you can access the original method using super. (called from included at /bundle/bundler/gems/activeadmin-xls-a5a70018c626/lib/active_admin/xls/resource_controller_extension.rb:5)
DEPRECATION WARNING: alias_method_chain is deprecated. Please, use Module#prepend instead. From module, you can access the original method using super. (called from included at /bundle/bundler/gems/activeadmin-xls-a5a70018c626/lib/active_admin/xls/resource_controller_extension.rb:6)

Gemfile.lock


GIT
  remote: git://github.com/thambley/activeadmin-xls.git
  revision: a5a70018c626d1ad3cde9f763bad3d10fa68867a
  specs:
    activeadmin-xls (1.0.3)
      activeadmin
      spreadsheet

undefined local variable or method `max_per_page'

Hi, I am receiving the next error:
NameError (undefined local variable or methodmax_per_page' for #Admin::UsersController:0x000000065f4cd0):
vendor/bundle/ruby/2.2.0/bundler/gems/activeadmin-xls-87675360866b/lib/active_admin/xls/resource_controller_extension.rb:26:in per_page_with_xls'

Missing per-column localisation option.

I ran into a limitation with the way localising column names is implemented in the gem, as you can only decide to use, or dismiss localisation for the whole DSL block. My use case is mixed, requiring localising regular model attributes, and using key/value pairs that have been saved to the db directly for custom columns (skipping localisation for the key columns).

I did a quick patch that works for my needs, opting to only localise the columns if passed as a symbol, and when a scope exists. Strings are always titleized.

module ActiveAdmin
  module Xls
    class Builder
      class Column
        def localized_name(i18n_scope = nil)
          return name.to_s.titleize unless i18n_scope && (name.is_a? Symbol)
          I18n.t name, scope: i18n_scope
        end
      end
    end
  end
end

Hesitating to do a PR for this though, as it is in essence a breaking change to the way column localisation is handled. Perhaps a passable option for the column helper would be a better approach?

Rails 4.2 tests are broken

sprockets-rails says:

Expected to find a manifest file in app/assets/config/manifest.js (Sprockets::Railtie::ManifestNeededError)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.