acts_as_translatable 0.0.8 → 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/acts_as_translatable/active_record.rb +32 -31
- metadata +3 -3
@@ -1,46 +1,47 @@
|
|
1
1
|
class ActiveRecord::Base
|
2
2
|
def self.acts_as_translatable_on(*fields)
|
3
|
+
eval "class ::#{name}
|
4
|
+
after_save :save_translations
|
5
|
+
|
6
|
+
def translations
|
7
|
+
unless @translations
|
8
|
+
@translations = {}
|
9
|
+
record_translations.each do |translation|
|
10
|
+
@translations[translation.locale.to_sym] ||= {}
|
11
|
+
@translations[translation.locale.to_sym][translation.translatable_field] = translation.content
|
12
|
+
end
|
13
|
+
end
|
14
|
+
@translations
|
15
|
+
end
|
16
|
+
|
17
|
+
def record_translations
|
18
|
+
@record_translations ||= RecordTranslation.where(:translatable_id => id, :translatable_type => self.class.name)
|
19
|
+
end
|
20
|
+
|
21
|
+
def save_translations
|
22
|
+
# delete all previous record translations
|
23
|
+
record_translations.destroy_all
|
24
|
+
|
25
|
+
# loop through updated translations
|
26
|
+
translations.each_pair do |locale, fields|
|
27
|
+
fields.each_pair do |field, content|
|
28
|
+
# create translation record
|
29
|
+
RecordTranslation.create :translatable_id => id, :translatable_type => self.class.name, :translatable_field => field, :locale => locale.to_s, :content => content unless content.blank?
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end"
|
3
34
|
fields.each do |field|
|
4
35
|
eval "class ::#{name}
|
5
|
-
after_save :save_translations
|
6
|
-
|
7
36
|
def #{field}
|
8
37
|
translations[I18n.locale] ||= {}
|
9
|
-
translations[I18n.locale][\"#{field}\"] ||
|
38
|
+
translations[I18n.locale][\"#{field}\"] || \"\"
|
10
39
|
end
|
11
40
|
|
12
41
|
def #{field}=(value)
|
13
42
|
translations[I18n.locale] ||= {}
|
14
43
|
translations[I18n.locale][\"#{field}\"] = value
|
15
44
|
end
|
16
|
-
|
17
|
-
def translations
|
18
|
-
unless @translations
|
19
|
-
@translations = {}
|
20
|
-
record_translations.each do |translation|
|
21
|
-
@translations[translation.locale.to_sym] ||= {}
|
22
|
-
@translations[translation.locale.to_sym][translation.translatable_field] = translation.content
|
23
|
-
end
|
24
|
-
end
|
25
|
-
@translations
|
26
|
-
end
|
27
|
-
|
28
|
-
def record_translations
|
29
|
-
@record_translations ||= RecordTranslation.where(:translatable_id => id, :translatable_type => self.class.name)
|
30
|
-
end
|
31
|
-
|
32
|
-
def save_translations
|
33
|
-
# delete all previous record translations
|
34
|
-
record_translations.destroy_all
|
35
|
-
|
36
|
-
# loop through updated translations
|
37
|
-
translations.each_pair do |locale, fields|
|
38
|
-
fields.each_pair do |field, content|
|
39
|
-
# create translation record
|
40
|
-
RecordTranslation.create :translatable_id => id, :translatable_type => self.class.name, :translatable_field => field, :locale => locale.to_s, :content => content unless content.blank?
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
44
45
|
end"
|
45
46
|
end
|
46
47
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: acts_as_translatable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 13
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 9
|
10
|
+
version: 0.0.9
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Lasse Bunk
|