SourceLabsLLC / administrate_exportable

Simple plugin to add CSV export feature to Administrate
https://www.sourcelabs.io/
MIT License
41 stars 19 forks source link

Administrate has deprecated `valid_action?` #48

Open johncip opened 1 year ago

johncip commented 1 year ago

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: https://github.com/thoughtbot/administrate/pull/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.

pablobm commented 6 months ago

Hello :wave: Deprecator of valid_action? here. Sorry about the change! A back-compatible option might be to do the following:

  1. Substitute valid_action? with existing_action?.
  2. Use metaprogramming to detect the existence of existing_action?.
    • If it doesn't exist, add your own implementation based on valid_action?. (This will support older versions).
  3. Silence the warning by overriding Administrate.warn_of_deprecated_authorization_method.

But none of this is supported, and generally it's recommended to use accessible_action?.