railsadminteam / rails_admin

RailsAdmin is a Rails engine that provides an easy-to-use interface for managing your data
MIT License
7.87k stars 2.25k forks source link

Fix filter on enum value in list view #3647

Closed fuegas closed 1 month ago

fuegas commented 9 months ago

Filtering on enum (in list view) gave the following error:

ActionView::Template::Error (undefined method `[]' for nil:NilClass

              (bindings[:object] || abstract_model.model.new).send(enum_method)
                       ^^^^^^^^^):
    54: <div id="list">
    55:   <%= form_tag(index_path(params.except(*%w[page f query])), method: :get) do %>
    56:     <div class="card mb-3 p-3 bg-light">
    57:       <div class="row" data-options="<%= ordered_filter_options.to_json %>" id="filters_box"></div>
    58:       <hr class="filters_box" style="display:<%= ordered_filters.empty? ? 'none' : 'block' %>"/>
    59:       <div class="row">
    60:         <div class="col-sm-6">

rails_admin (3.1.2) lib/rails_admin/config/fields/types/enum.rb:31:in `block in <class:Enum>'
rails_admin (3.1.2) lib/rails_admin/config/configurable.rb:77:in `instance_eval'
rails_admin (3.1.2) lib/rails_admin/config/configurable.rb:77:in `block in register_instance_option'
rails_admin (3.1.2) lib/rails_admin/config/fields/types/enum.rb:14:in `block in <class:Enum>'
rails_admin (3.1.2) lib/rails_admin/config/configurable.rb:77:in `instance_eval'
rails_admin (3.1.2) lib/rails_admin/config/configurable.rb:77:in `block in register_instance_option'
rails_admin (3.1.2) lib/rails_admin/config/fields/base.rb:106:in `filter_options'
rails_admin (3.1.2) app/helpers/rails_admin/main_helper.rb:52:in `block in ordered_filter_options'
rails_admin (3.1.2) app/helpers/rails_admin/main_helper.rb:44:in `map'
rails_admin (3.1.2) app/helpers/rails_admin/main_helper.rb:44:in `ordered_filter_options'
rails_admin (3.1.2) app/views/rails_admin/main/index.html.erb:57
actionview (7.0.8) lib/action_view/helpers/capture_helper.rb:45:in `block in capture'
actionview (7.0.8) lib/action_view/helpers/capture_helper.rb:209:in `with_output_buffer'
actionview (7.0.8) lib/action_view/helpers/capture_helper.rb:45:in `capture'
actionview (7.0.8) lib/action_view/helpers/form_tag_helper.rb:78:in `form_tag'
rails_admin (3.1.2) app/views/rails_admin/main/index.html.erb:55

This is because bindings is nil and thus the hash lookup fails. By adding safe navigation we circumvent this error and use the second option namely abstract_model.model.new.

robotfelix commented 2 months ago

Is there anything stopping this simple fix being merged?

This issue is a big barrier to upgrading to Rails Admin 3.x, as for a long time the "legacy" enums were the only way to do enums in Rails Admin. The wiki implies that ActiveRecord enums are considered a replacement for the legacy enums, but they don't really provide equivalent functionality. ActiveRecord enums are explicitly for mapping values to the database, but the "legacy" Rails Admin enums can/could be used anywhere where you want to provide admins with a list of options to pick from.

cc @mshibuya would be amazing if you could look - I think you were the last person to change this code in https://github.com/railsadminteam/rails_admin/commit/be9a75e504edd9a754157a4ddba590e8a5d14149

mshibuya commented 2 months ago

Oh sorry, I just didn't have a chance to look at this. Could you add a RSpec test case for this fix?

robotfelix commented 1 month ago

I've added a test and fixed a similar error in #enum_method. Since I don't have write access to @fuegas's fork I've opened a new PR with the additions here: #3684

mshibuya commented 1 month ago

Thank you so much for the PR, but the fix was made as https://github.com/railsadminteam/rails_admin/commit/d62f604cc8d7d1434f7dfe0c5aca3aaf3dc2547b.