Module: ActsAsLocalizable::AR::ClassMethods

Defined in:
lib/acts_as_localizable.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_localizable(default_locale = nil) ⇒ Object



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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/acts_as_localizable.rb', line 46

def acts_as_localizable(default_locale=nil)
  if default_locale == nil
    default_locale = I18n.default_locale
  end
  model_id = self.to_s.split('::').last.pluralize.singularize.underscore
  default_locale = 'en' unless default_locale
  @acts_as_localizable_config = ActsAsLocalizable::AR::Config.new(model_id,default_locale)
  self.superclass.class_eval do
    has_many :localized_fields, :as => :localized_object,:dependent => :destroy
  end      
  self.class_eval do
    has_many :localized_fields, :as => :localized_object,:dependent => :destroy
    # default_scope :include => [:localized_fields]
    attr_accessor :localized_cache
    after_save :save_localized_attributes
    private
    
    def save_localized_attributes
      return if self.localized_cache.blank?
      self.localized_cache.each_pair do |key,locale_object|
        locale_object.modified_attributes.each do  |modified_field|
          fields = self.localized_fields.select {|localized_field| localized_field.locale.downcase == key.to_s.downcase && localized_field.field_name == modified_field}
          if fields.blank?
            field = self.localized_fields.new(:locale => key.to_s.downcase,:field_name => modified_field,:value => locale_object.instance_variable_get("@#{modified_field}"))
            field.save
          else
            field = fields.first
            field.value = locale_object.instance_variable_get("@#{modified_field}")
            field.save
          end
        end
        
      end
    end
  end
  # self.superclass.send(:default_scope){{:include => [:localized_fields]}}
   # self.superclass.default_scoping << {:include => [:localized_fields]}
  include ActsAsLocalizable::AR::InstanceMethods
end

#acts_as_localizable_configObject



85
86
87
# File 'lib/acts_as_localizable.rb', line 85

def acts_as_localizable_config
  @acts_as_localizable_config || self.superclass.instance_variable_get('@acts_as_localizable_config')
end