carlosantoniodasilva / i18n_alchemy

I18n date/number parsing/localization - RMU Project
http://mendicantuniversity.org/
MIT License
159 stars 34 forks source link

Problem with `localize` method #23

Closed caironoleto closed 12 years ago

caironoleto commented 12 years ago

Hi mate!

In our application, we have an i18n_alchemy initializer and we do this:

ActiveSupport.on_load(:active_record) do
  include I18n::Alchemy
end

And all active record classes has i18n alchemy loaded.

But, with the merge of localize methods and custom parsers we get the following bug:

class MonthlyStatement < ActiveRecord::Base
  localize :initial_competence_date, :end_competence_date, :using => CompetenceParser
end

class FamilyWageType < ActiveRecord::Base
end
1.9.3p194 :001 > FamilyWageType.localized_methods
 => {} 
1.9.3p194 :002 > MonthlyStatement.localized_methods
 => {:initial_competence_date=>CompetenceParser, :end_competence_date=>CompetenceParser} 
1.9.3p194 :003 > FamilyWageType.localized_methods
 => {:initial_competence_date=>CompetenceParser, :end_competence_date=>CompetenceParser} 

Before the merge, we had two different methods, localized_methods and customized_parsers.

We don't use localize methods, only in attributes and now we using custom parsers.

I'm thinking this problem can occur with only localize methods too.

So, you have any solution for us?!

lucasmazza commented 12 years ago

I think it's not due the merge, but because class_attribute is shared among the subclasses of the one that includes I18n::Alchemy. I might work on something to handle that later.

caironoleto commented 12 years ago

I don't think so, with the 2d478150d258750269bf1b14af7ccb45268548a3 version:

1.9.3p194 :001 > ActiveRecord::Base.customized_parsers
 => {} 
1.9.3p194 :002 > MonthlyStatement.customized_parsers
 => {:initial_competence_date=>CompetenceParser, :end_competence_date=>CompetenceParser} 
1.9.3p194 :003 > ActiveRecord::Base.customized_parsers
 => {} 

I think the problem is in the piece of code in lib/i18n_alchemy.rb:

      def localize(*methods, options)
        parser  = options[:using]
        methods = methods.each_with_object(localized_methods) do |method_name, hash|
          hash[method_name] = parser
        end
        self.localized_methods = methods
      end
lucasmazza commented 12 years ago

Oh, the each_with_object(localized_methods) call is mutating the parent Hash, need to fix that :)

carlosantoniodasilva commented 12 years ago

Exactly, right on the spot, thanks guys.