thoughtbot / thoughtbot/administrate
Exception: ActionView::Template::Error: uninitialized constant for multi-word model dashboard with irregular inflection
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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