3

Looks like inflections don't work for module names with the nesting level more than one.

If you have the following in your config/initializers/inflections.rb:

ActiveSupport::Inflector.inflections do |inflect|
  inflect.acronym 'VCloud'
end

Then when you create a directory under app/, say app/services/vcloud/ you will get two modules:

Vcloud #=> Vcloud
VCloud #=> VCloud

But if you create a directory with a higher nesting level, say app/services/vmware/vcloud/ you will get only one module:

Vmware::Vcloud #=> Vmware::Vcloud
Vmware::VCloud #=> NameError: uninitialized constant Vmware::VCloud

Is this a bug?

2
  • i did not know that inflection has anything to do with classes. i though it would handle string strictly?
    – phoet
    Oct 21, 2014 at 11:58
  • @phoet - Rails uses Inflector internally to resolve missing constants.
    – BroiSatse
    Oct 21, 2014 at 12:06

2 Answers 2

2

I would go with this is a bug. You can go around it with (within initializers):

module ActiveSupport::Inflector
  def underscore_with_acronym_fix(string)
    words = string.split('::')
    return words.map(&method(:underscore)).join('/') unless words.one?
    underscore_without_acronym_fix(string)
  end

  alias_method_chain :underscore, :acronym_fix
end

I'll make a pull request to fix this, however will need slightly more time to confirm it will not break anything. There are quite a lot of cases here.

0

I wonder if i could replicate this "issue".

Tried this running rails console.

> ActiveSupport::Inflector.camelize 'vcloud' => "Vcloud"

> ActiveSupport::Inflector.camelize 'v_cloud' => "VCloud"

There are test cases for all sorts of combinations below.

http://api.rubyonrails.org/classes/ActiveSupport/Inflector/Inflections.html#method-i-acronym

https://github.com/rails/rails/blob/master/activesupport/test/inflector_test_cases.rb

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.