acts_as_translatable 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
@@ -1,50 +1,52 @@
|
|
1
|
+
require 'acts_as_translatable/instance_methods'
|
2
|
+
|
1
3
|
class ActiveRecord::Base
|
2
4
|
def self.acts_as_translatable_on(*fields)
|
3
|
-
|
4
|
-
self.after_save :save_translations
|
5
|
+
include ActsAsTranslatable::InstanceMethods
|
5
6
|
|
6
|
-
# get the fields
|
7
|
-
@translatable_fields = fields
|
8
|
-
|
9
|
-
# loop through fields
|
10
7
|
fields.each do |field|
|
11
|
-
eval "
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
@translations
|
8
|
+
eval "class ::#{name}
|
9
|
+
after_save :save_translations
|
10
|
+
|
11
|
+
def #{field}
|
12
|
+
translations[I18n.locale] ||= {}
|
13
|
+
translations[I18n.locale][\"#{field}\"]
|
14
|
+
end
|
15
|
+
|
16
|
+
def #{field}=(value)
|
17
|
+
translations[I18n.locale] ||= {}
|
18
|
+
translations[I18n.locale][\"#{field}\"] = value
|
19
|
+
end
|
20
|
+
|
21
|
+
def translations
|
22
|
+
unless @translations
|
23
|
+
@translations = {}
|
24
|
+
record_translations.each do |translation|
|
25
|
+
@translations[translation.locale.to_sym] ||= {}
|
26
|
+
@translations[translation.locale.to_sym][translation.translatable_field] = translation.content
|
27
|
+
end
|
27
28
|
end
|
29
|
+
@translations
|
30
|
+
end
|
31
|
+
|
32
|
+
def record_translations
|
33
|
+
@record_translations ||= RecordTranslation.where(:translatable_id => id, :translatable_type => self.class.name)
|
28
34
|
end
|
29
|
-
@translations
|
30
|
-
end
|
31
35
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
# create translation record
|
46
|
-
RecordTranslation.create :translatable_id => id, :translatable_type => self.class.name, :translatable_field => field, :locale => locale.to_s, :content => content unless content.blank?
|
47
|
-
end
|
36
|
+
def save_translations
|
37
|
+
# delete all previous record translations
|
38
|
+
record_translations.destroy_all
|
39
|
+
|
40
|
+
# loop through updated translations
|
41
|
+
translations.each_pair do |locale, fields|
|
42
|
+
fields.each_pair do |field, content|
|
43
|
+
# create translation record
|
44
|
+
RecordTranslation.create :translatable_id => id, :translatable_type => self.class.name, :translatable_field => field, :locale => locale.to_s, :content => content unless content.blank?
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end"
|
48
49
|
end
|
49
50
|
end
|
50
|
-
end
|
51
|
+
end
|
52
|
+
|
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: 27
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Lasse Bunk
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-05-
|
18
|
+
date: 2012-05-14 00:00:00 Z
|
19
19
|
dependencies: []
|
20
20
|
|
21
21
|
description: Ruby on Rails plugin for easy translation of database fields.
|
@@ -28,6 +28,7 @@ extra_rdoc_files: []
|
|
28
28
|
|
29
29
|
files:
|
30
30
|
- lib/acts_as_translatable/active_record.rb
|
31
|
+
- lib/acts_as_translatable/instance_methods.rb
|
31
32
|
- lib/acts_as_translatable.rb
|
32
33
|
- lib/generators/acts_as_translatable/acts_as_translatable_generator.rb
|
33
34
|
- lib/generators/acts_as_translatable/templates/migration.rb
|