thoughtbot / thoughtbot/administrate

Exception: ActionView::Template::Error: uninitialized constant for multi-word model dashboard with irregular inflection

Open
#2,409 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
JavaScript
Stars
6k
Forks
1.1k
PR merge metrics
No merged PRs in 30d

Description

  • What were you trying to do?
    Setup admin dashboard for model with multi-word irregular inflection.
    example:
    I have a dashboard class: class MerchantBonusDashboard < Administrate::BaseDashboard
    And inflector:
ActiveSupport::Inflector.inflections(:en) do |inflect|
  inflect.irregular 'merchant_bonus', 'merchant_bonuses'
end

during render of the dashboard I get error:

Rails -- Exception: ActionView::Template::Error: uninitialized constant MerchantBonu
Did you mean?  MerchantBonus

Relevant part of the stack trace:

activesupport (6.1.7.4) lib/active_support/inflector/methods.rb:273:in `const_get'
activesupport (6.1.7.4) lib/active_support/inflector/methods.rb:273:in `constantize'
activesupport (6.1.7.4) lib/active_support/core_ext/string/inflections.rb:74:in `constantize'
administrate (0.19.0) lib/administrate/base_dashboard.rb:25:in `model'
activesupport (6.1.7.4) lib/active_support/core_ext/object/try.rb:15:in `public_send'
activesupport (6.1.7.4) lib/active_support/core_ext/object/try.rb:15:in `try'
administrate (0.19.0) app/helpers/administrate/application_helper.rb:26:in `model_from_resource' 

Looking at lib/administrate/base_dashboard.rb:25:in 'model'
I see this impl:

      def model
        to_s.chomp(DASHBOARD_SUFFIX).classify.constantize
      end

to_s.chomp(DASHBOARD_SUFFIX) =>'MerchantBonus'
.classify => 'MerchantBonu'

Rails ActiveSupport Inflector method classify expects a underscore (snake_case) table name, but here we are giving it a CamelCase model name. classify does incidentally work when the table_name arg is CamelCase and doesn't have an multi-word irregular inflection.

Seems like we should do an underscore operation on the CamelCase model name before classifying. e.g.

      def model
        to_s.chomp(DASHBOARD_SUFFIX).underscore.classify.constantize
      end

I could also define an additional inflector for the CamelCase version inflect.irregular 'MerchantBonus', 'MerchantBonuses'. But that seems more of a hack that the proposed change above.

Happy to submit a PR if this seems like a reasonable fix or maybe I'm misunderstanding some aspect.

  • What versions are you running?
    • Rails - (6.1.7.4)
    • administrate - (0.19.0)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in lib/administrate/base_dashboard.rb:25 and compare the current model resolution with the reported MerchantBonusDashboard and irregular inflection example. Verify the behavior for a multi-word irregular model and confirm that rendering the dashboard no longer raises the reported uninitialized-constant error.

Written by the indexing model from the issue text.

Assessment

Tech stack
rails, ruby
Domain
backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.