43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
# File 'lib/globalize/active_record.rb', line 43
def translates(*attr_names)
return if translates?
options = attr_names.
class_inheritable_accessor :translation_class, :translated_attribute_names
class_inheritable_writer :required_attributes
self.translation_class = ActiveRecord.build_translation_class(self, options)
self.translated_attribute_names = attr_names.map(&:to_sym)
include InstanceMethods
extend ClassMethods, Migration
after_save :save_translations!
has_many :translations, :class_name => translation_class.name,
:foreign_key => class_name.foreign_key,
:dependent => :delete_all,
:extend => HasManyExtensions
named_scope :with_translations, lambda { |locale|
conditions = required_attributes.map do |attribute|
"#{quoted_translation_table_name}.#{attribute} IS NOT NULL"
end
conditions << "#{quoted_translation_table_name}.locale = ?"
{ :include => :translations, :conditions => [conditions.join(' AND '), locale] }
}
attr_names.each { |attr_name| translated_attr_accessor(attr_name) }
end
|