lib/has_translations.rb in has_translations-0.3.4 vs lib/has_translations.rb in has_translations-0.3.5
- old
+ new
@@ -76,30 +76,38 @@
# <tt>translation(locale)</tt> method finds translation with specified locale.
#
# <tt>all_translations</tt> method that returns all possible translations in
# ordered hash (useful when creating forms with nested attributes).
def self.translations(*attrs)
+ new_options = attrs.extract_options!
options = {
:fallback => false,
:reader => true,
:writer => false,
- :nil => ''
- }.merge(attrs.extract_options!)
+ :nil => '',
+ :autosave => new_options[:writer]
+ }.merge(new_options)
- options.assert_valid_keys([:fallback, :reader, :writer, :nil])
+ options.assert_valid_keys([:fallback, :reader, :writer, :nil, :autosave])
translation_class_name = "#{self.model_name}Translation"
translation_class = translation_class_name.constantize
belongs_to = self.model_name.demodulize.underscore.to_sym
- write_inheritable_attribute :has_translations_options, options
- class_inheritable_reader :has_translations_options
+ if ActiveRecord::VERSION::MAJOR < 3
+ write_inheritable_attribute :has_translations_options, options
+ class_inheritable_reader :has_translations_options
- # Workaround to support Rails 2
- scope_method = if ActiveRecord::VERSION::MAJOR < 3 then :named_scope else :scope end
+ scope_method = :named_scope
+ else
+ class_attribute :has_translations_options
+ self.has_translations_options = options
+ scope_method = :scope
+ end
+
# associations, validations and scope definitions
- has_many :translations, :class_name => translation_class_name, :dependent => :destroy, :autosave => true
+ has_many :translations, :class_name => translation_class_name, :dependent => :destroy, :autosave => options[:autosave]
translation_class.belongs_to belongs_to
translation_class.validates_presence_of :locale
translation_class.validates_uniqueness_of :locale, :scope => :"#{belongs_to}_id"
send scope_method, :translated, lambda { |locale| {:conditions => ["#{translation_class.table_name}.locale = ?", locale.to_s], :joins => :translations} }