Problem with `localize` method
Closed this issue · 4 comments
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?!
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.
I don't think so, with the 2d47815 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
Oh, the each_with_object(localized_methods)
call is mutating the parent Hash, need to fix that :)
Exactly, right on the spot, thanks guys.