Module: Dbi18::ClassMethods

Defined in:
lib/dbi18/active_record_support.rb

Instance Method Summary collapse

Instance Method Details

#db_i18n(*attributes) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/dbi18/active_record_support.rb', line 11

def db_i18n(*attributes)
  include Del
  language = Dbi18.locale
  self.class_eval do
    alias :old_save :save
    attr_accessor :get_mark
  end
  self.send :define_method, "save" do
    if self.old_save
      language.each do |l_type|
        locale = l_type.to_s
          if ((Dbi18.model).where(:class_id => self.id, :class_name => self.class.name, :locale => locale).first).blank?
            model = (Dbi18.model).new
            model.hash_content = self.init_hash_content
          else
            model = (Dbi18.model).where(:class_id => self.id, :class_name => self.class.name, :locale => locale).first
          end
          hash_content = eval model.hash_content
          attributes.each do |attrs|
            attrs_locale = attrs.to_s + "_" + l_type.to_s
            attrs_locale_sym = attrs_locale.to_sym
            hash_content["#{attrs}"]= self.send attrs_locale_sym
          end
          model.class_id = self.id
          model.class_name = self.class.name
          model.locale = locale
          model.hash_content = hash_content.to_s
          model.save
        end
    end
  end

  init_attr_method = ("init_hash_content").to_sym
    self.send :define_method, init_attr_method do #initial obeject.dbi18_type.hash_content
      @count = 0
      attributes.each do |attrs|
        attrs = attrs.to_s
        @count += 1
        if @count == 1
          @str = "{"
        end
        if @count < attributes.length
          @str += "\"#{attrs}\"=>\"\","
        else
          @str += "\"#{attrs}\"=>\"\"}"
        end
      end
      return @str
    end

  init_get_mark = "init_get_mark".to_sym
  self.send :define_method,init_get_mark do
    if (!(self.id).blank?)&&((self.get_mark).blank?)
      self.get_mark = true
      models = (Dbi18.model).where(:class_id => self.id, :class_name => self.class.name)
      models.each do |model|
        attributes.each do |attrs|
          attrs_locale_set = attrs.to_s + "_" + model.locale+"="
          attrs_locale_set = attrs_locale_set.to_sym
          hash_content = eval model.hash_content
          self.send attrs_locale_set,hash_content["#{attrs}"]
        end
      end
      return true
    else
      return false
    end
  end

  attributes.each do |attrs|
    language.each do |l_type| 
      attrs_locale = attrs.to_s + "_" + l_type.to_s
      attrs_locale_set = attrs_locale + "="
      locale = l_type.to_s
          self.class_eval do
            attr_accessor attrs_locale.to_sym
          end
          get_method = attrs.to_s+"_"+l_type.to_s
          get_method = get_method.to_sym
          self.send :define_method, get_method do #get_value
           if !self.init_get_mark
             self.send attrs_locale_set,"" if (eval "@#{get_method}").blank?
           end
           return (eval "@#{get_method}.to_s")
          end
    end
    self.send :define_method, attrs do #get_value_with_i18n.locale
      locale = I18n.locale.to_s
        self.send  attrs.to_s+"_"+locale
    end
  end

end