lib/has_translations.rb in has_translations-0.3.3 vs lib/has_translations.rb in has_translations-0.3.4
- old
+ new
@@ -37,11 +37,11 @@
# translations :title, :text, :fallback => true, :writer => true, :nil => nil
#
# ===
#
# Configuration options:
- #
+ #
# * <tt>:fallback</tt> - if translation for the current locale not found.
# By default false. Set to true if you want to use reader fallback.
# Uses algorithm of fallback:
# 0) current translation (using I18n.locale);
# 1) default locale (using I18n.default_locale);
@@ -96,11 +96,11 @@
# Workaround to support Rails 2
scope_method = if ActiveRecord::VERSION::MAJOR < 3 then :named_scope else :scope end
# associations, validations and scope definitions
- has_many :translations, :class_name => translation_class_name, :dependent => :destroy
+ has_many :translations, :class_name => translation_class_name, :dependent => :destroy, :autosave => true
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} }
@@ -138,16 +138,21 @@
if options[:reader]
attrs.each do |name|
send :define_method, name do
translation = self.translation(I18n.locale)
- translation.nil? ? has_translations_options[:nil] : translation.send(name)
+ translation.try(name) || has_translations_options[:nil]
end
end
end
if options[:writer]
attrs.each do |name|
+ send :define_method, "#{name}_before_type_cast" do
+ translation = self.translation(I18n.locale, false)
+ translation.try(name)
+ end
+
send :define_method, "#{name}=" do |value|
translation = find_or_build_translation(I18n.locale)
translation.send(:"#{name}=", value)
end
end