Skip to content
Browse files

Check for the existence of the proxy class because it might exist al…

…ready.

 Due to rails lazy class loading using const_missing I try to load the proxy class,
 if that fails I create a new one. After that, append the globalize methods to the
 class.

 I did not find a way to reproduce this error with a unit test. So here is how
 you can reproduce this:

 1. Have a model that needs translation (e.g Tag)
 2. Create a TagTranslation model and add a custom method to it (e.g slugname)
 3. ruby script/console
 4. Tag.new (Tag class is loaded and proxy class is being created)
 5. TagTranslation.new.respond_to?(:slugname) => false but should be true
  • Loading branch information...
1 parent 133da77 commit 52797c99d4499bb7c43e7430099553f9ba64f67a @phuesler phuesler committed with svenfuchs Oct 4, 2009
Showing with 15 additions and 4 deletions.
  1. +15 −4 lib/globalize/model/active_record.rb
View
19 lib/globalize/model/active_record.rb
@@ -12,17 +12,28 @@ def create_proxy_class(klass)
klass_name = module_names.pop
target = module_names.empty? ? Object : module_names.join('::').constantize
- target.const_set "#{klass_name}Translation", Class.new(::ActiveRecord::Base) {
- belongs_to "#{klass.name.underscore.gsub('/', '_')}".intern
+ proxy_class_name = "#{klass_name}Translation"
+ proxy_class = nil
+ begin
+ proxy_class = proxy_class_name.constantize
+ rescue NameError
+ proxy_class = target.const_set proxy_class_name, Class.new(::ActiveRecord::Base)
+ end
+ proxy_class.instance_eval do
+ belongs_to "#{klass.name.underscore.gsub('/', '_')}".intern
+ end
+ proxy_class.class_eval do
def locale
read_attribute(:locale).to_sym
end
def locale=(locale)
write_attribute(:locale, locale.to_s)
end
- }
+ end
+
+ return proxy_class
end
def define_accessors(klass, attr_names)
@@ -42,4 +53,4 @@ def define_accessors(klass, attr_names)
end
end
end
-end
+end

0 comments on commit 52797c9

Please sign in to comment.
Something went wrong with that request. Please try again.