translatable 0.3.1 → 1.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +1 -1
- data/Gemfile +6 -8
- data/Gemfile.lock +110 -103
- data/README.rdoc +30 -46
- data/VERSION +1 -1
- data/lib/generators/translatable/model_generator.rb +11 -7
- data/lib/generators/translatable/translation_generator.rb +1 -1
- data/lib/translatable.rb +12 -10
- data/lib/translatable/base.rb +74 -0
- data/lib/translatable/orm/active_record.rb +156 -0
- data/test/cases/{translatable_test.rb → active_record_test.rb} +142 -90
- data/test/cases/base_test.rb +104 -0
- data/test/generators/model_generator_test.rb +27 -15
- data/test/generators/translation_generator_test.rb +5 -5
- data/test/support/active_record.rb +1 -0
- data/test/support/models/messages.rb +13 -14
- data/test/support/models/news.rb +7 -9
- data/test/support/models/posts.rb +10 -10
- data/test/test_helper.rb +1 -17
- data/translatable.gemspec +15 -11
- metadata +41 -27
- data/.document +0 -5
- data/lib/translatable/active_record.rb +0 -254
metadata
CHANGED
@@ -1,60 +1,75 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: translatable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
5
|
-
prerelease:
|
4
|
+
version: 1.2.2
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- E-Max
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2014-01-31 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: activerecord
|
16
|
-
requirement:
|
17
|
-
none: false
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
18
16
|
requirements:
|
19
17
|
- - ! '>='
|
20
18
|
- !ruby/object:Gem::Version
|
21
|
-
version:
|
19
|
+
version: !binary |-
|
20
|
+
NC4wLjA=
|
22
21
|
type: :runtime
|
23
22
|
prerelease: false
|
24
|
-
version_requirements:
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ! '>='
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: !binary |-
|
28
|
+
NC4wLjA=
|
25
29
|
- !ruby/object:Gem::Dependency
|
26
30
|
name: activesupport
|
27
|
-
requirement:
|
28
|
-
none: false
|
31
|
+
requirement: !ruby/object:Gem::Requirement
|
29
32
|
requirements:
|
30
33
|
- - ! '>='
|
31
34
|
- !ruby/object:Gem::Version
|
32
35
|
version: '0'
|
33
36
|
type: :runtime
|
34
37
|
prerelease: false
|
35
|
-
version_requirements:
|
38
|
+
version_requirements: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - ! '>='
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
36
43
|
- !ruby/object:Gem::Dependency
|
37
44
|
name: i18n
|
38
|
-
requirement:
|
39
|
-
none: false
|
45
|
+
requirement: !ruby/object:Gem::Requirement
|
40
46
|
requirements:
|
41
47
|
- - ! '>='
|
42
48
|
- !ruby/object:Gem::Version
|
43
49
|
version: '0'
|
44
50
|
type: :runtime
|
45
51
|
prerelease: false
|
46
|
-
version_requirements:
|
52
|
+
version_requirements: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ! '>='
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
47
57
|
- !ruby/object:Gem::Dependency
|
48
58
|
name: jeweler
|
49
|
-
requirement:
|
50
|
-
none: false
|
59
|
+
requirement: !ruby/object:Gem::Requirement
|
51
60
|
requirements:
|
52
61
|
- - ~>
|
53
62
|
- !ruby/object:Gem::Version
|
54
|
-
version:
|
63
|
+
version: !binary |-
|
64
|
+
MS44LjA=
|
55
65
|
type: :development
|
56
66
|
prerelease: false
|
57
|
-
version_requirements:
|
67
|
+
version_requirements: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ~>
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: !binary |-
|
72
|
+
MS44LjA=
|
58
73
|
description: This game was build to make whole proccess of working with translation
|
59
74
|
for DM to be almost invisble. That was THE AIM.
|
60
75
|
email: max@studentify.nl
|
@@ -64,7 +79,8 @@ extra_rdoc_files:
|
|
64
79
|
- LICENSE.txt
|
65
80
|
- README.rdoc
|
66
81
|
files:
|
67
|
-
- .
|
82
|
+
- .ruby-gemset
|
83
|
+
- .ruby-version
|
68
84
|
- .travis.yml
|
69
85
|
- Gemfile
|
70
86
|
- Gemfile.lock
|
@@ -75,10 +91,12 @@ files:
|
|
75
91
|
- lib/generators/translatable/model_generator.rb
|
76
92
|
- lib/generators/translatable/translation_generator.rb
|
77
93
|
- lib/translatable.rb
|
78
|
-
- lib/translatable/
|
94
|
+
- lib/translatable/base.rb
|
79
95
|
- lib/translatable/engine.rb
|
80
96
|
- lib/translatable/generator_helper.rb
|
81
|
-
-
|
97
|
+
- lib/translatable/orm/active_record.rb
|
98
|
+
- test/cases/active_record_test.rb
|
99
|
+
- test/cases/base_test.rb
|
82
100
|
- test/generators/model_generator_test.rb
|
83
101
|
- test/generators/translation_generator_test.rb
|
84
102
|
- test/support/active_record.rb
|
@@ -91,29 +109,25 @@ files:
|
|
91
109
|
homepage: http://github.com/kot-begemot/translatable
|
92
110
|
licenses:
|
93
111
|
- MIT
|
112
|
+
metadata: {}
|
94
113
|
post_install_message:
|
95
114
|
rdoc_options: []
|
96
115
|
require_paths:
|
97
116
|
- lib
|
98
117
|
required_ruby_version: !ruby/object:Gem::Requirement
|
99
|
-
none: false
|
100
118
|
requirements:
|
101
119
|
- - ! '>='
|
102
120
|
- !ruby/object:Gem::Version
|
103
121
|
version: '0'
|
104
|
-
segments:
|
105
|
-
- 0
|
106
|
-
hash: -740240291
|
107
122
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
108
|
-
none: false
|
109
123
|
requirements:
|
110
124
|
- - ! '>='
|
111
125
|
- !ruby/object:Gem::Version
|
112
126
|
version: '0'
|
113
127
|
requirements: []
|
114
128
|
rubyforge_project:
|
115
|
-
rubygems_version:
|
129
|
+
rubygems_version: 2.2.1
|
116
130
|
signing_key:
|
117
|
-
specification_version:
|
131
|
+
specification_version: 4
|
118
132
|
summary: An esay way to manage the translations for datamapper
|
119
133
|
test_files: []
|
data/.document
DELETED
@@ -1,254 +0,0 @@
|
|
1
|
-
require 'active_record'
|
2
|
-
require 'i18n'
|
3
|
-
|
4
|
-
module Translatable
|
5
|
-
###
|
6
|
-
# In order to made the model Translatable, an additional fields should
|
7
|
-
# should be added first to it. Here is an example of it might be implemented:
|
8
|
-
#
|
9
|
-
# Examples:
|
10
|
-
#
|
11
|
-
# class Author < ActiveRecord::Base
|
12
|
-
# validates :name, :presence => true
|
13
|
-
# end
|
14
|
-
#
|
15
|
-
# class TranslatableNews < ActiveRecord::Base #
|
16
|
-
# attr_accessible :title, :content
|
17
|
-
# end
|
18
|
-
#
|
19
|
-
# class News < ActiveRecord::Base
|
20
|
-
#
|
21
|
-
# belongs_to :author
|
22
|
-
#
|
23
|
-
# translatable do
|
24
|
-
# translatable :title, :presence => true, :uniqueness => true
|
25
|
-
# translatable :content, :presence => true
|
26
|
-
# translatable_model "TranslatedNews"
|
27
|
-
# translatable_origin :origin_id
|
28
|
-
# end
|
29
|
-
#
|
30
|
-
# attr_accessible :author_id, :author
|
31
|
-
# end
|
32
|
-
#
|
33
|
-
# An example of application:
|
34
|
-
#
|
35
|
-
# news = News.create :translations_attributes => [{title: "Resent News", content: "That is where the text goes", locale: "en"}]
|
36
|
-
# news.translations.create title: "Заголовок", content: "Содержание",locale: "ru"
|
37
|
-
#
|
38
|
-
# news.content
|
39
|
-
# # => "That is where the text goes"
|
40
|
-
#
|
41
|
-
# ::I18n.locale = "ru"
|
42
|
-
# news.content
|
43
|
-
# # => "Сюди идет текст"
|
44
|
-
#
|
45
|
-
# ::I18n.locale = "de"
|
46
|
-
# news.content
|
47
|
-
# # => nil
|
48
|
-
#
|
49
|
-
# ::I18n.locale = ::I18n.default_locale
|
50
|
-
# news.content
|
51
|
-
# # => "That is where the text goes"
|
52
|
-
#
|
53
|
-
module ActiveRecord
|
54
|
-
|
55
|
-
def translatable
|
56
|
-
extend Translatable::ActiveRecord::ClassMethods
|
57
|
-
include Translatable::ActiveRecord::InstanceMethods
|
58
|
-
|
59
|
-
translatable_define_hash
|
60
|
-
yield
|
61
|
-
translatable_register
|
62
|
-
end
|
63
|
-
|
64
|
-
module ClassMethods
|
65
|
-
|
66
|
-
protected
|
67
|
-
|
68
|
-
###
|
69
|
-
# Fields that are translatable.
|
70
|
-
# Those fields should be defined in the original model including all the related params.
|
71
|
-
# Examples:
|
72
|
-
#
|
73
|
-
# translatable_property :title, String, required: true, unique: true
|
74
|
-
# translatable_property :content, Text
|
75
|
-
#
|
76
|
-
# NB! Will raise an error if there was no fields specified
|
77
|
-
#
|
78
|
-
def translatable *args
|
79
|
-
(@translatable[:properties] ||= []) << args
|
80
|
-
end
|
81
|
-
|
82
|
-
###
|
83
|
-
# Defines model that will be treated as translation handler.
|
84
|
-
# Model can be defined as String, Symbol or Constant.
|
85
|
-
# Examples:
|
86
|
-
#
|
87
|
-
# translated_model TranslatedNews
|
88
|
-
# translated_model "TranslatedNews"
|
89
|
-
# translated_model :TranslatedNews
|
90
|
-
#
|
91
|
-
# Default: Translatable<ModelName>
|
92
|
-
#
|
93
|
-
def translatable_model model_name
|
94
|
-
@translatable[:model] = translatable_model_prepared model_name
|
95
|
-
end
|
96
|
-
|
97
|
-
###
|
98
|
-
# Define the key that the translation will be used for belongs_to association,
|
99
|
-
# to communicate with original model
|
100
|
-
# Example:
|
101
|
-
#
|
102
|
-
# translatable_origin :news
|
103
|
-
#
|
104
|
-
# Default: :origin
|
105
|
-
#
|
106
|
-
def translatable_origin origin_key
|
107
|
-
@translatable[:origin] = translatable_origin_prepared origin_key
|
108
|
-
end
|
109
|
-
|
110
|
-
###
|
111
|
-
# Will not register the attributes as accessible.
|
112
|
-
# IMPORTANT: Translatable block will be evaluated on the model after it
|
113
|
-
# was loaded, so it will modify certain thing on final version. Hence this thing is needed.
|
114
|
-
# Examples:
|
115
|
-
#
|
116
|
-
# translatable_attr_protected
|
117
|
-
#
|
118
|
-
# Default: false
|
119
|
-
#
|
120
|
-
def translatable_attr_protected
|
121
|
-
@translatable[:attr_accessible] = false
|
122
|
-
end
|
123
|
-
|
124
|
-
###
|
125
|
-
# Will not register the attributes as accessible.
|
126
|
-
# IMPORTANT: Translatable block will be evaluated on the model after it
|
127
|
-
# was loaded, so it will modify certain thing on final version. Hence this thing is needed.
|
128
|
-
# Examples:
|
129
|
-
#
|
130
|
-
# translatable_attr_protected
|
131
|
-
#
|
132
|
-
# Default: false
|
133
|
-
#
|
134
|
-
def translatable_attr_accessible
|
135
|
-
@translatable[:attr_accessible] = true
|
136
|
-
end
|
137
|
-
|
138
|
-
###
|
139
|
-
# Define the key that the translation will be used for belongs_to association,
|
140
|
-
# to communicate with original model
|
141
|
-
# Example:
|
142
|
-
#
|
143
|
-
# translatable_origin :language
|
144
|
-
#
|
145
|
-
# Default: :locale
|
146
|
-
#
|
147
|
-
def translatable_locale locale_attr
|
148
|
-
@translatable[:locale] = translatable_locale_prepared locale_attr
|
149
|
-
end
|
150
|
-
|
151
|
-
###
|
152
|
-
# Returns Model as a constant that deals with translations
|
153
|
-
def translatable_model_prepared model_name = nil
|
154
|
-
model_constant = model_name
|
155
|
-
model_constant ||= "Translatable#{self.name}"
|
156
|
-
model_constant.to_s.constantize
|
157
|
-
end
|
158
|
-
|
159
|
-
|
160
|
-
def translatable_origin_prepared origin_key = nil
|
161
|
-
origin_key || "origin"
|
162
|
-
end
|
163
|
-
|
164
|
-
def translatable_locale_prepared locale = nil
|
165
|
-
locale || "locale"
|
166
|
-
end
|
167
|
-
|
168
|
-
###
|
169
|
-
# Define hash that contains all the translations
|
170
|
-
def translatable_define_hash
|
171
|
-
@translatable = {}
|
172
|
-
end
|
173
|
-
|
174
|
-
###
|
175
|
-
# Handles all the registring routine, defining methods,
|
176
|
-
# properties, and everything else
|
177
|
-
def translatable_register
|
178
|
-
raise ArgumentError.new("At least one property should be defined") if [nil, []].include?(@translatable[:properties])
|
179
|
-
[:model,:origin,:locale].each { |hash_key| @translatable[hash_key] ||= send "translatable_#{hash_key}_prepared" }
|
180
|
-
|
181
|
-
translatable_register_properties_for_origin
|
182
|
-
translatable_register_properties_for_translatable
|
183
|
-
end
|
184
|
-
|
185
|
-
###
|
186
|
-
# Handle the routine to define all th required stuff on the original maodel
|
187
|
-
def translatable_register_properties_for_origin
|
188
|
-
has_many :translations, :class_name => @translatable[:model].name, :foreign_key => :"#{@translatable[:origin]}_id"
|
189
|
-
accepts_nested_attributes_for :translations
|
190
|
-
attr_accessible :translations_attributes
|
191
|
-
|
192
|
-
@translatable[:properties].each do |p|
|
193
|
-
accessible_as = (p.last.delete(:as) || p.first rescue p.first)
|
194
|
-
self.module_eval <<-RUBY, __FILE__, __LINE__ + 1
|
195
|
-
def #{accessible_as}
|
196
|
-
current_translation.try(:#{p.first})
|
197
|
-
end
|
198
|
-
RUBY
|
199
|
-
end
|
200
|
-
|
201
|
-
self.module_eval <<-RUBY, __FILE__, __LINE__ + 1
|
202
|
-
def translatable_set_current(locale = ::I18n.locale)
|
203
|
-
locale = @translatable_locale = locale.to_s
|
204
|
-
@current_translation = if translations.loaded?
|
205
|
-
translations.select { |t| t.send(:"#{@translatable[:locale]}") == locale }
|
206
|
-
else
|
207
|
-
translations.where(:"#{@translatable[:locale]}" => locale)
|
208
|
-
end.first
|
209
|
-
end
|
210
|
-
alias_method :set_current_translation, :translatable_set_current
|
211
|
-
RUBY
|
212
|
-
end
|
213
|
-
|
214
|
-
def translatable_register_properties_for_translatable
|
215
|
-
@translatable[:model].module_eval <<-RUBY, __FILE__, __LINE__ + 1
|
216
|
-
validates :#{@translatable[:locale]}, :presence => true
|
217
|
-
validates :#{@translatable[:locale]}, :format => { :with => /[a-z]{2}/}, :if => Proc.new {|record| !record.#{@translatable[:locale]}.blank? }
|
218
|
-
validates :#{@translatable[:locale]}, :uniqueness => { :scope => :#{@translatable[:origin]}_id }
|
219
|
-
|
220
|
-
belongs_to :#{@translatable[:origin]}, :class_name => "#{self.name}"
|
221
|
-
RUBY
|
222
|
-
|
223
|
-
unless @translatable[:attr_accessible].nil?
|
224
|
-
@translatable[:model].module_eval <<-RUBY, __FILE__, __LINE__ + 1
|
225
|
-
attr_#{!!@translatable[:attr_accessible] ? "accessible" : "protected" } :#{@translatable[:locale]}, :#{@translatable[:origin]}_id
|
226
|
-
RUBY
|
227
|
-
end
|
228
|
-
|
229
|
-
@translatable[:properties].each do |p|
|
230
|
-
if p.size > 1
|
231
|
-
@translatable[:model].module_eval <<-RUBY, __FILE__, __LINE__ + 1
|
232
|
-
validates :#{p.first}, #{p.last.inspect}
|
233
|
-
RUBY
|
234
|
-
end
|
235
|
-
end
|
236
|
-
end
|
237
|
-
end
|
238
|
-
|
239
|
-
module InstanceMethods
|
240
|
-
def current_translation
|
241
|
-
update_current_translation unless @translatable_locale
|
242
|
-
@current_translation
|
243
|
-
end
|
244
|
-
|
245
|
-
def update_current_translation
|
246
|
-
translatable_set_current(@translatable_locale = ::I18n.locale.to_s)
|
247
|
-
end
|
248
|
-
|
249
|
-
def other_translations
|
250
|
-
translations - [current_translation]
|
251
|
-
end
|
252
|
-
end
|
253
|
-
end
|
254
|
-
end
|