#828 ✓resolved
Tomoaki Hayasaka

``Hobo::Helper::Translations.ht`` raises ``NameError`` and ``NoMethodError``

Reported by Tomoaki Hayasaka | October 9th, 2010 @ 09:58 AM | in Hobo 1.3 (Rails 3)

When Hobo::Helper::Translations.ht is called with a multi-part translation key, constantize is called on the first model-like part, and if there is no corresponding constant to that model-like part, NameError is thrown:

>> Hobo::Helper::Translations.ht("modelish.fieldish")
NameError: uninitialized constant Modelish
    from /u0/home/hayasaka/work/misocream/misocream-meeting-hobo-1.3/vendor/bundler_gems/ruby/1.9.1/gems/activesupport-3.0.0/lib/active_support/inflector/methods.rb:124:in `block in constantize'
    from /u0/home/hayasaka/work/misocream/misocream-meeting-hobo-1.3/vendor/bundler_gems/ruby/1.9.1/gems/activesupport-3.0.0/lib/active_support/inflector/methods.rb:123:in `each'
    from /u0/home/hayasaka/work/misocream/misocream-meeting-hobo-1.3/vendor/bundler_gems/ruby/1.9.1/gems/activesupport-3.0.0/lib/active_support/inflector/methods.rb:123:in `constantize'
    from /u0/home/hayasaka/work/misocream/misocream-meeting-hobo-1.3/vendor/bundler_gems/ruby/1.9.1/gems/activesupport-3.0.0/lib/active_support/core_ext/string/inflections.rb:43:in `constantize'
    from /home/hayasaka/work/hobo/hobo/hobo/lib/hobo/helper/translations.rb:83:in `ht'

If a corresponding constant exists but it is not a model, NoMethodError is raised:

>> Hobo::Helper::Translations.ht("object.object")
NoMethodError: undefined method `human' for nil:NilClass
    from /u0/home/hayasaka/work/misocream/misocream-meeting-hobo-1.3/vendor/bundler_gems/ruby/1.9.1/gems/activesupport-3.0.0/lib/active_support/whiny_nil.rb:48:in `method_missing'
    from /home/hayasaka/work/hobo/hobo/hobo/lib/hobo/helper/translations.rb:83:in `ht'

Possible fix is:

diff --git a/hobo/lib/hobo/helper/translations.rb b/hobo/lib/hobo/helper/translations.rb
index 08755d1..6b603ff 100644
--- a/hobo/lib/hobo/helper/translations.rb
+++ b/hobo/lib/hobo/helper/translations.rb
@@ -69,18 +69,22 @@ module Hobo
         model = '' if model.eql?('hobo')
 
         unless model.blank?
+          klass = begin
+                    model.singularize.camelize.constantize
+                  rescue NameError
+                  end
           # add :"hobo.#{key}" as the first fallback
           options[:default].unshift("hobo.#{subkey}".to_sym)
           # set the count option in order to allow multiple pluralization
           if pluralized
-            options[:count] = model.singularize.camelize.constantize.try.count
+            options[:count] = klass.try.count
           end
           options[:count] ||= 1
           # translate the model
           # the singularize method is used because Hobo does not keep the ActiveRecord convention in its tags
           # no default needed because human_name defaults to the model name
           # try because Hobo class is not an ActiveRecord::Base subclass
-          translated_pluralized_model = model.singularize.camelize.constantize.try.model_name.human(:count=>options[:count])
+          translated_pluralized_model = klass.try.model_name.try.human(:count=>options[:count])
           options[:model] = translated_pluralized_model
         end
         options[:count] ||= 1

Comments and changes to this ticket

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

People watching this ticket

Tags

Referenced by

Pages