Module: HasManyTranslations::Creation::ClassMethods

Defined in:
lib/has_many_translations/creation.rb

Overview

Class methods added to ActiveRecord::Base to facilitate the creation of new translations.

Instance Method Summary collapse

Instance Method Details

#prepare_translated_options_with_creation(options) ⇒ Object

Overrides the basal prepare_has_translations_options method defined in HasManyTranslations::Options to extract the :only and :except options into has_many_translations_options.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/has_many_translations/creation.rb', line 26

def prepare_translated_options_with_creation(options)
  self.columns.map{|c|c.type == :string || c.type == :text ? c.name : nil}.compact.each{|name|
    #alias_method "#{name}_before_translation", name.to_sym
    #unless try(name)
      define_method name, lambda { |*args|
        #
        unless self.translations.blank? || self.translations.first.origin_locale_code == self.hmt_locale || read_attribute(name.to_sym).nil?
          trans = self.translations.first(:conditions => {:locale_code => self.hmt_locale, :attribute => name})
          val = trans.nil? ? read_attribute(name.to_sym) : trans.value
          #self.hmt_locale
        else
          #self.id
          read_attribute(name.to_sym)
          #try(name)
        end
        #HasManyTranslations.fetch(args.first || self.class.locale || I18n.locale, name)
      }
    
    alias_method "#{name}_before_type_cast", name
    
  }
  
  result = prepare_translated_options_without_creation(options)

  self.has_many_translations_options[:only] = Array(options.delete(:only)).map(&:to_s).uniq if options[:only]
  self.has_many_translations_options[:except] = Array(options.delete(:except)).map(&:to_s).uniq if options[:except]
  #self.has_many_translations_options[:locales] = Array(options.delete(:locales)).map(&:to_s).uniq if options[:locales]
  result
end