4
5
6
7
8
9
10
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
|
# File 'lib/ext/active_record_ext.rb', line 4
def has_translations *fields
class_eval do
unless respond_to?(:missing_translation)
has_many :translations, class_name: "RailsDbLocalize::Translation", as: :resource, dependent: :destroy
scope :__rails_db_translations_sub_query, lambda{ |lang|
adapter = Rails.configuration.database_configuration[Rails.env]["adapter"]
number_of_fields_to_translates = RailsDbLocalize.schema[self.to_s].count
ttable = RailsDbLocalize::Translation.arel_table.name
table = respond_to?(:klass) ? table.klass : self
table.select(:id).joins("INNER JOIN \"#{ttable}\"
ON (\"#{ttable}\".resource_id = \"#{arel_table.name}\".id
AND \"#{ttable}\".resource_type = '#{to_s}')")
.group(:resource_type, :resource_id, :id)
.having("COUNT(*) = #{number_of_fields_to_translates}")
.where(:"rails_db_localize_translations.lang" => lang)
}
scope :missing_translation, lambda{ |lang|
where("\"#{arel_table.name}\".id NOT IN (#{__rails_db_translations_sub_query(lang).to_sql})")
}
scope :having_translation, lambda{ |lang|
where("\"#{arel_table.name}\".id IN (#{__rails_db_translations_sub_query(lang).to_sql})")
}
def self.preload_translations
RailsDbLocalize::TranslationCache.instance.prefetch_collections(self)
self
end
end
fields.each do |field|
RailsDbLocalize::add_to_schema(self, field)
self.has_many :"#{field}_translations", -> { where(field: field) }, as: :resource
self.class_eval " def \#{field}_translated_exists?(lang=nil)\n lang ||= I18n.locale\n !!RailsDbLocalize::TranslationCache.instance.get_translation_for(self.class, self.id, \"\#{field}\", lang, nil )\n end\n\n def \#{field}_translated (lang=nil)\n lang ||= I18n.locale\n RailsDbLocalize::TranslationCache.instance.get_translation_for(self.class, self.id, \"\#{field}\", lang, self.\#{field} )\n end\n\n def \#{field}_translated= args\n if args.is_a?(Array)\n value, lang = args\n else\n value = args\n lang = I18n.locale\n end\n\n if self.id\n translated = RailsDbLocalize::Translation.where(\n resource_type: self.class.to_s, resource_id: self.id, field: \"\#{field}\", lang: lang\n ).first_or_create\n\n translated.content = value\n\n translated.save!\n else\n translations.build field: \"\#{field}\", lang: lang, content: value\n end\n end\n RUBY\n end\n\n end\n\nend\n", __FILE__, __LINE__ + 1
|