Giter VIP home page Giter VIP logo

administrate_exportable's Introduction

Installation

Add this line to your application's Gemfile:

gem 'administrate_exportable'

And then execute:

$ bundle

Or install it yourself as:

$ gem install administrate_exportable

Usage

For each resource you want to be exportable, add the following line to the their respective Administrate controller.

include AdministrateExportable::Exporter

and the following line in the config/routes.rb file, correctly nested on resources

get :export, on: :collection

Example:

namespace :admin do
  resources :organizations do
    get :export, on: :collection
  end
....

By default all the attributes from ATTRIBUTE_TYPES will be exported. If you want to exclude an attribute, you can use the option: export: false.

Example:

class BusinessDashboard < Administrate::BaseDashboard
  ATTRIBUTE_TYPES = {
    id: Field::Number.with_options(export: false),
    name: Field::String,
    description: Field::Text,
...

For the default field types from Administrate, we export the values you got from the partial views/fields/index. And if it is a custom field, we just run field.to_s. But if you want to specify the value to be exported, you can use the option transform_on_export, to pass a Proc that receives the field. Example:

ATTRIBUTE_TYPES = {
 created_at: Field::DateTime.with_options(transform_on_export: -> (field) { field.data.strftime("%F") })

By default the gem adds the Export button to the partial views/admin/application/_index_header.html.erb. But if you have your own Administrate index views or override that partial in your application you can add the link manually:

link_to('Export', [:export, namespace.to_sym, page.resource_name.to_s.pluralize.to_sym, sanitized_order_params(page, :id).to_h.merge(format: :csv)], class: 'button') if valid_action?(:export)

Example:

....
 <div>
    <%= link_to(
      t(
        "administrate.actions.new_resource",
        name: page.resource_name.titleize.downcase
      ),
      [:new, namespace.to_sym, page.resource_path.to_sym],
      class: "button",
    ) if valid_action?(:new) && show_action?(:new, new_resource) %>

    <%= link_to(
      'Export',
      [:export, namespace.to_sym, page.resource_name.to_s.pluralize.to_sym, sanitized_order_params(page, :id).to_h.merge(format: :csv)],
      class: 'button'
    ) if valid_action?(:export) %>
  </div>
....

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/SourceLabsLLC/administrate_exportable.

License

The gem is available as open source under the terms of the MIT License.

administrate_exportable's People

Contributors

andreibondarev avatar animista01 avatar artplan1 avatar dependabot[bot] avatar djdarkbeat avatar dmitryzuev avatar dwilkie avatar dyerc avatar jonatasrancan avatar michaljaneda avatar moveson avatar rassas avatar sebaherrera07 avatar sebashton avatar sedubois avatar yourivdlans avatar

Stargazers

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

Watchers

 avatar  avatar  avatar

administrate_exportable's Issues

Undefined method `collection_includes'

I have just been following the readme to setup administrate_exportable with an administrate 0.11.0 installation. I'm getting the following error after clicking the export button:

NoMethodError (undefined method `collection_includes' for #<PlayerDashboard:0x00007ffb7d0d93c0>
Did you mean?  collection_attributes):

administrate_exportable (0.1.2) lib/administrate_exportable/exporter_service.rb:84:in `collection'
administrate_exportable (0.1.2) lib/administrate_exportable/exporter_service.rb:19:in `block in csv'
/Users/Chris/.rbenv/versions/2.4.5/lib/ruby/2.4.0/csv.rb:1183:in `generate'
administrate_exportable (0.1.2) lib/administrate_exportable/exporter_service.rb:15:in `csv'
administrate_exportable (0.1.2) lib/administrate_exportable/exporter_service.rb:6:in `csv'
administrate_exportable (0.1.2) lib/administrate_exportable/exporter.rb:12:in `block in exportable'
actionpack (5.2.3) lib/action_controller/metal/basic_implicit_render.rb:6:in `send_action'

I have added the include line to my controller and my controller starts as follows:

class PlayersController < Admin::ApplicationController
    include AdministrateExportable::Exporter

Field::Text truncated to 50 characters on export

Thanks for all the hard work on this gem!

I noticed that 'Field::Text' attributes (and maybe others) are being truncated to 50 characters on export. Is this a feature or a bug, and if it's a feature, is there an option to download the whole string?

Ryan

Error: Please use symbols for polymorphic route arguments.

ArgumentError Please use symbols for polymorphic route arguments.

I get this error trying to manually add the button example code from your docs:

<div>
  <%= link_to(
    'Export',
    [:export, namespace, page.resource_name.to_s.pluralize, sanitized_order_params(page, :id).to_h.merge(format: :csv)],
    class: 'button'
  ) if valid_action?(:export) %>
</div>

I'm using:
administrate 0.17
administrate_exportable 0.5
rails 7.0.1
ruby 3.0.3

Error on export with Administrate 0.12.0

After setting up, I tried to export and got
NoMethodError: undefined method association_includes' for #PackageDashboard:0x00007f7fd3f67f98`

In searching, I found this reference in Administrate from October

thoughtbot/administrate@8d86f9f

Is it possible that there is a plugin related issue, or is there something else going on? My setup is pretty simple with the route and controller reference on a single model in Administrate.

Thanks for your time.

Error when exporting empty result

When you are on an index and you search for something that returns an empty table. When you try to export that it raises an error
uninitialized constant Class::ATTRIBUTE_TYPES

Export link you appear only if records > to 0

Interference with Turbolinks

Expected behaviour

When clicking the export button, the page stays the same but a file download popup appears asking where to save the CSV file.

Actual behaviour

When clicking the export button, the browser navigates to a new page where the CSV contents are rendered in a single line (no line breaks), as if a plain text/HTML response had been given.

Workarounds

  • After the initial rendering, hit Ctrl-R and the CSV download will be proposed
  • The issue disappears when removing turbolinks.start()

Using administrate 0.14.0, administrate_exportable 0.2.0, turbolinks 5.2.0 (node dependency).

Thanks for this helpful gem!

Export service does not handle date fields

Currently when we export an Administrate::Fields::Date field, the output looks like this:

Screen Shot 2021-02-08 at 12 54 54 PM

The exporter should handle Date fields in the same way it handles DateTime and Time fields.

Administrate has deprecated `valid_action?`

Issue

Using administrate_exportable with administrate v0.18.0 will cause a deprecation warning to get logged in test and development. They look like this:

DEPRECATION WARNING: The method `valid_action?` is deprecated. Please use `accessible_action?`
instead, or see the documentation for other options. (called from valid_action?
at <GEM_PATH>/administrate-0.18.0/app/controllers/administrate/application_controller.rb:125)

Apparently the practice of calling valid_action? and show_action? was replaced with a single call to a new method, accessible_action? in this PR: thoughtbot/administrate#1941

As far as I can tell, that PR is part of administrate v0.18.0, and unfortunately accessible_action? doesn't exist in versions before v0.18.0.

I'm guessing that making the deprecation warning go away at the cost of breaking compatibility with older versions isn't worth it, and neither is making the behavior conditional on e.g. Gem.loaded_specs["administrate"].version ... but I thought I'd make this issue for documentation value at least.

Workaround

The change, were it made, would result in the export button's link_to in app/views/admin/application/_index_header.html.erb becoming:

    <%= link_to(
      'Export',
      [:export, namespace, page.resource_name.to_s.pluralize.to_sym, sanitized_order_params(page, :id).to_h.merge(format: :csv)],
      class: 'button',
      target: '_blank'
    ) if accessible_action?(page.resource_name, :export) %>

Folks using the latest administrate who wish to avoid the warning can simply copy administrate_exportable's version of _index_header.html.erb into their project, and update the export button's link_to to the above. (Generating views in administrate didn't spit out the file when I tried it.)

Finally, it might be of some use to update the readme to provide both versions -- @andreibondarev I'm happy to make a PR with whatever change is desired, if a change is desired at all.

Generating several N+1 queries when adding multiple resources to an export thus slowing down query and timing out Heroku

When I am trying to export a resource which has two or three other related models data to be exported along with it, it is generating several N+1 queries and with couple of thousand rows I am facing server time out issues.
Upon installing bullet gem I get following on my bullet.log

USE eager loading detected
  Resource => [:sponsorships]
  Add to your query: .includes([:sponsorships])

GET /admin/resources/export.csv
USE eager loading detected
  Resource => [:organizations]
  Add to your query: .includes([:organizations])

Just for example my resource_dashboard contains following: 


```ATTRIBUTE_TYPES = {
    id: Field::Number,
    name: Field::String,
    resource_type: Field::Enumerate,
    organizations: Field::HasMany.with_options(
      transform_on_export: -> (field) { field.data&.map(&:name)&.join(", ")  if field.data }
    ),
    event_detail: Field::HasOne,
    questions: Field::NestedHasMany.with_options(
      skip: :resource,
      transform_on_export: -> (field) { field.data&.map(&:text)&.join(", ") if field.data }
    ),


Is there a way to speed up query or prevent eager loading?

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.