Rails module names with abbreviations

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

If your config/initializers/inflections.rb

have the following:

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

      

Then when you create a directory under app/

, let's say app/services/vcloud/

you get two modules:

Vcloud #=> Vcloud
VCloud #=> VCloud

      

But if you create a directory with a higher nesting level, let's say app/services/vmware/vcloud/

you only get one module:

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

      

This is mistake?

+3


source to share


2 answers


I would be wrong with that. You can get around it with (inside 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 it will take a little longer to confirm that it won't break anything. There are many cases here.

+1


source


I wonder if I can reproduce this "problem".

Tried this rails console.

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

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



There are test cases for all kinds 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

0


source







All Articles