Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
blainejohnson17 opened this issue Aug 3, 2023 · 1 comment
Labels
bug breakages in functionality that is implemented

Comments

@blainejohnson17
Copy link

blainejohnson17 commented Aug 3, 2023

  • 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)
@blainejohnson17 blainejohnson17 added the bug breakages in functionality that is implemented label Aug 3, 2023
@pablobm
Copy link
Collaborator

pablobm commented Aug 25, 2023

Your proposal sounds good to me. Go for it!

And thank you for looking into this 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug breakages in functionality that is implemented
Projects
None yet
Development

No branches or pull requests

2 participants