acts_as_translatable 0.2.6 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,108 @@
|
|
1
|
+
module ActsAsTranslatable
|
2
|
+
module ClassMethods
|
3
|
+
def acts_as_translatable_on(*fields)
|
4
|
+
after_initialize :translations
|
5
|
+
after_save :save_translations
|
6
|
+
has_many :record_translations, :foreign_key => :translatable_id, :conditions => { :translatable_type => name}, :dependent => :destroy
|
7
|
+
default_scope :include => :record_translations
|
8
|
+
|
9
|
+
# loop through fields to define methods such as "name" and "description"
|
10
|
+
fields.each do |field|
|
11
|
+
define_method "#{field}" do
|
12
|
+
get_field_content(I18n.locale, field)
|
13
|
+
end
|
14
|
+
|
15
|
+
define_method "#{field}?" do
|
16
|
+
!send("#{field}").blank?
|
17
|
+
end
|
18
|
+
|
19
|
+
define_method "#{field}=" do |content|
|
20
|
+
set_field_content(I18n.locale, field, content)
|
21
|
+
end
|
22
|
+
|
23
|
+
# loop through fields to define methods such as "name_en" and "name_es"
|
24
|
+
I18n.available_locales.each do |locale|
|
25
|
+
define_method "#{field}_#{locale}" do
|
26
|
+
get_field_content(locale, field)
|
27
|
+
end
|
28
|
+
|
29
|
+
define_method "#{field}_#{locale}?" do
|
30
|
+
!send("#{field}_#{locale}").blank?
|
31
|
+
end
|
32
|
+
|
33
|
+
define_method "#{field}_#{locale}=" do |content|
|
34
|
+
set_field_content(locale, field, content)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
define_method :translations do
|
40
|
+
# load translations
|
41
|
+
unless @translations
|
42
|
+
@translations = {}
|
43
|
+
I18n.available_locales.each do |locale|
|
44
|
+
@translations[locale] ||= {}
|
45
|
+
end
|
46
|
+
record_translations.each do |translation|
|
47
|
+
@translations[translation.locale.to_sym] ||= {}
|
48
|
+
@translations[translation.locale.to_sym][translation.translatable_field.to_sym] = translation.content
|
49
|
+
end
|
50
|
+
end
|
51
|
+
@translations
|
52
|
+
end
|
53
|
+
|
54
|
+
define_method :save_translations do
|
55
|
+
# delete all previous translations of this record
|
56
|
+
record_translations.destroy_all
|
57
|
+
|
58
|
+
# loop through updated translations
|
59
|
+
translations.each_pair do |locale, fields|
|
60
|
+
fields.each_pair do |field, content|
|
61
|
+
# create translation record
|
62
|
+
record_translations.create :translatable_field => field, :locale => locale.to_s, :content => content unless content.blank?
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
define_method :get_field_content do |locale, field|
|
68
|
+
# get I18n fallbacks
|
69
|
+
if self.class.enable_locale_fallbacks && I18n.respond_to?(:fallbacks)
|
70
|
+
locales = I18n.fallbacks[locale]
|
71
|
+
else
|
72
|
+
locales = [locale]
|
73
|
+
end
|
74
|
+
|
75
|
+
# content default
|
76
|
+
content = nil
|
77
|
+
|
78
|
+
# fallbacks
|
79
|
+
locales.each do |l|
|
80
|
+
if content = translations[l][field]
|
81
|
+
break
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
# return content
|
86
|
+
content
|
87
|
+
end
|
88
|
+
|
89
|
+
define_method :set_field_content do |locale, field, content|
|
90
|
+
# set field content
|
91
|
+
translations[locale.to_sym][field] = content
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
def enable_locale_fallbacks
|
96
|
+
unless @enable_locale_fallbacks_set
|
97
|
+
@enable_locale_fallbacks = true
|
98
|
+
@enable_locale_fallbacks_set = true
|
99
|
+
end
|
100
|
+
@enable_locale_fallbacks
|
101
|
+
end
|
102
|
+
|
103
|
+
def enable_locale_fallbacks=(enabled)
|
104
|
+
@enable_locale_fallbacks = enabled
|
105
|
+
@enable_locale_fallbacks_set = true
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
data/lib/acts_as_translatable.rb
CHANGED
@@ -1 +1,2 @@
|
|
1
|
-
require "acts_as_translatable/
|
1
|
+
require "acts_as_translatable/class_methods"
|
2
|
+
ActiveRecord::Base.extend ActsAsTranslatable::ClassMethods
|
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: 19
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 3
|
9
|
+
- 0
|
10
|
+
version: 0.3.0
|
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-27 00:00:00 Z
|
19
19
|
dependencies: []
|
20
20
|
|
21
21
|
description: Ruby on Rails plugin for easy translation of database fields.
|
@@ -27,7 +27,7 @@ extensions: []
|
|
27
27
|
extra_rdoc_files: []
|
28
28
|
|
29
29
|
files:
|
30
|
-
- lib/acts_as_translatable/
|
30
|
+
- lib/acts_as_translatable/class_methods.rb
|
31
31
|
- lib/acts_as_translatable.rb
|
32
32
|
- lib/generators/acts_as_translatable/acts_as_translatable_generator.rb
|
33
33
|
- lib/generators/acts_as_translatable/templates/migration.rb
|
@@ -1,118 +0,0 @@
|
|
1
|
-
# PLEASE HELP: I need help refactoring and improving these methods so that the methods are defined in a more decent way.
|
2
|
-
|
3
|
-
class ActiveRecord::Base
|
4
|
-
def self.acts_as_translatable_on(*fields)
|
5
|
-
field_symbols = fields.map { |f| ":#{f}" }.join(", ")
|
6
|
-
|
7
|
-
eval "class ::#{name}
|
8
|
-
after_initialize :translations
|
9
|
-
after_save :save_translations
|
10
|
-
after_destroy :destroy_record_translations
|
11
|
-
has_many :record_translations, :foreign_key => :translatable_id, :conditions => { :translatable_type => '#{name}'}
|
12
|
-
default_scope :include => :record_translations
|
13
|
-
|
14
|
-
def self.translatable_fields
|
15
|
-
[#{field_symbols}]
|
16
|
-
end
|
17
|
-
|
18
|
-
def translations
|
19
|
-
unless @translations
|
20
|
-
@translations = {}
|
21
|
-
I18n.available_locales.each do |locale|
|
22
|
-
@translations[locale] ||= {}
|
23
|
-
end
|
24
|
-
record_translations.each do |translation|
|
25
|
-
@translations[translation.locale.to_sym] ||= {}
|
26
|
-
@translations[translation.locale.to_sym][translation.translatable_field.to_sym] = translation.content
|
27
|
-
end
|
28
|
-
end
|
29
|
-
@translations
|
30
|
-
end
|
31
|
-
|
32
|
-
def save_translations
|
33
|
-
# delete all previous translations of this record
|
34
|
-
destroy_record_translations
|
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
|
-
record_translations.create :translatable_field => field, :locale => locale.to_s, :content => content unless content.blank?
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
def destroy_record_translations
|
46
|
-
# delete all translations of this record
|
47
|
-
record_translations.destroy_all
|
48
|
-
end
|
49
|
-
end"
|
50
|
-
|
51
|
-
localized_fields = ""
|
52
|
-
I18n.available_locales.each do |locale|
|
53
|
-
fields.each do |field|
|
54
|
-
localized_fields << "def #{field}_#{locale}
|
55
|
-
get_field_content(\"#{locale}\".to_sym, \"#{field}\".to_sym)
|
56
|
-
end
|
57
|
-
def #{field}_#{locale}=(content)
|
58
|
-
set_field_content(\"#{locale}\".to_sym, \"#{field}\".to_sym, content)
|
59
|
-
end
|
60
|
-
"
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
fields.each do |field|
|
65
|
-
eval "class ::#{name}
|
66
|
-
def self.enable_locale_fallbacks
|
67
|
-
unless @enable_locale_fallbacks_set
|
68
|
-
@enable_locale_fallbacks = true
|
69
|
-
@enable_locale_fallbacks_set = true
|
70
|
-
end
|
71
|
-
@enable_locale_fallbacks
|
72
|
-
end
|
73
|
-
|
74
|
-
def self.enable_locale_fallbacks=(value)
|
75
|
-
@enable_locale_fallbacks = value
|
76
|
-
@enable_locale_fallbacks_set = true
|
77
|
-
end
|
78
|
-
|
79
|
-
def #{field}
|
80
|
-
get_field_content(I18n.locale, \"#{field}\")
|
81
|
-
end
|
82
|
-
|
83
|
-
def #{field}?
|
84
|
-
!#{field}.blank?
|
85
|
-
end
|
86
|
-
|
87
|
-
def #{field}=(content)
|
88
|
-
set_field_content(I18n.locale, \"#{field}\", content)
|
89
|
-
end
|
90
|
-
|
91
|
-
#{localized_fields}
|
92
|
-
|
93
|
-
def get_field_content(locale, field)
|
94
|
-
# get I18n fallbacks
|
95
|
-
if self.class.enable_locale_fallbacks && I18n.respond_to?(:fallbacks)
|
96
|
-
locales = I18n.fallbacks[locale.to_sym]
|
97
|
-
else
|
98
|
-
locales = [locale.to_sym]
|
99
|
-
end
|
100
|
-
|
101
|
-
# fallbacks
|
102
|
-
locales.each do |l|
|
103
|
-
content = translations[l][field.to_sym]
|
104
|
-
return content if content
|
105
|
-
end
|
106
|
-
|
107
|
-
# none found
|
108
|
-
return nil
|
109
|
-
end
|
110
|
-
|
111
|
-
def set_field_content(locale, field, content)
|
112
|
-
translations[locale.to_sym][field.to_sym] = content
|
113
|
-
end
|
114
|
-
end"
|
115
|
-
end
|
116
|
-
end
|
117
|
-
end
|
118
|
-
|