globalize-automatic 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 735aec99e5cfd71d44624e9eddbc04f9c5f7946d
4
- data.tar.gz: 570d8ecb6a353dc1bebeeff4a43888d4b701b90d
3
+ metadata.gz: ab79d320f3c47fe42bb2842a6a406c11cf8d56b3
4
+ data.tar.gz: 8ba316c7d76c35892b6014351521ee46a90d6688
5
5
  SHA512:
6
- metadata.gz: f9f3068d5461d17d96fc53315cf17a875f052a3b1b567515982d5dcc1a5d56a735ea6dab3b05264775bb0068a55d87ce14bae8e1b1ee7c851bc1ecd9eceeb1e9
7
- data.tar.gz: 6ed69374b69a9564e233d4da95bc2d2cec43f86a7457a0bdb9856574dd1e21ed99a10f840ecdd54d67a0be7b765092cd3f2d74eed4fbcfad128c969bb2aace2a
6
+ metadata.gz: be513ece8570227e0f743e53d7f179438bffbe98febc11966f4dc169c8a8d8d56cd5ae7d25302eb8ad373803ef847b9247e53797da4d42f7bcfeceb3352470fa
7
+ data.tar.gz: 5168aa575c0d7b247c2d2e14b372ad33f1ca891a841c11276da74e12694a944d46d5e1e4eef3a405a3223b307cefa36c13549d6ebcdb518e720a97bf78fabf59
@@ -2,9 +2,6 @@
2
2
 
3
3
  require 'globalize'
4
4
  require 'globalize-accessors'
5
- require 'globalize/automatic/translation'
6
- require 'globalize/automatic/translation_job'
7
- require 'globalize/automatic/translator/easy_translate'
8
5
  require 'after_commit_action'
9
6
 
10
7
  module Globalize::Automatic
@@ -20,6 +17,8 @@ module Globalize::Automatic
20
17
  self.automatic_translation_attribute_names = []
21
18
  self.automatic_translation_field_locales = Hash.new { [] }
22
19
  self.automatic_translated_field_locales = Hash.new { [] }
20
+
21
+ after_update :update_automatic_translations
23
22
  end
24
23
 
25
24
  class_methods do
@@ -66,6 +65,11 @@ module Globalize::Automatic
66
65
  automatic_translated_field_locales[field].include?(locale)
67
66
  end
68
67
 
68
+ # 自動翻訳ON/OFF属性名
69
+ def automatic_translation_attribute_name_for(attr_name, locale)
70
+ "#{attr_name}_#{locale.to_s.underscore}_automatically"
71
+ end
72
+
69
73
  public
70
74
  def create_automatic_translation_table!(*fields)
71
75
  automatic_translation_class.create_table!(*fields)
@@ -82,8 +86,9 @@ module Globalize::Automatic
82
86
  end
83
87
 
84
88
  def automatic_translation_for(locale)
85
- automatic_translations.where(locale: locale).
86
- first_or_initialize(default_translation_automatically(locale))
89
+ automatic_translation_caches[locale] ||=
90
+ automatic_translations.where(locale: locale).
91
+ first_or_initialize(default_translation_automatically(locale))
87
92
  end
88
93
 
89
94
  # from_locale から attribute_names を自動翻訳
@@ -115,19 +120,48 @@ module Globalize::Automatic
115
120
  locales.first
116
121
  end
117
122
 
123
+ def reload(options = nil)
124
+ automatic_translation_caches.clear
125
+ automatic_translation_attribute_names.each { |attr_name| @attributes.reset(attr_name.to_s) }
126
+ super(options)
127
+ end
128
+
129
+ def initialize_dup(other)
130
+ @automatic_translation_caches = nil
131
+ super(other)
132
+ other.automatic_translation_attribute_names.each do |attr_name|
133
+ self.attributes = { attr_name => other.send(attr_name) }
134
+ end
135
+ end
136
+
118
137
  private
119
138
  def default_translation_automatically(locale)
120
139
  # 自動翻訳元指定されていなくて、
121
140
  # 自動翻訳先指定されているものを
122
141
  # デフォルトで自動翻訳ONにする
142
+ #
143
+ # 自動翻訳元指定されていて
144
+ # 自動翻訳先指定されているものは
145
+ # デフォルトで自動翻訳OFFにする 
146
+ #
147
+ # 自動翻訳先指定されていないものは対象外
123
148
  translated_attribute_names.inject({}) do |defaults, attr_name|
124
- defaults[:"#{attr_name}_automatically"] =
125
- self.class.translated_field_automatically?(attr_name, locale) &&
126
- !self.class.translate_field_automatically?(attr_name, locale)
149
+ if self.class.translated_field_automatically?(attr_name, locale)
150
+ defaults[:"#{attr_name}_automatically"] =
151
+ !self.class.translate_field_automatically?(attr_name, locale)
152
+ end
127
153
  defaults
128
154
  end
129
155
  end
130
156
 
157
+ def automatic_translation_caches
158
+ @automatic_translation_caches ||= {}
159
+ end
160
+
161
+ def update_automatic_translations
162
+ automatic_translation_caches.values.flatten.each(&:save)
163
+ end
164
+
131
165
  end
132
166
 
133
167
  module TranslationExtension
@@ -177,15 +211,15 @@ module Globalize::Automatic
177
211
  attr_names.each do |attr_name|
178
212
  locales.each do |locale|
179
213
  klass.class_eval(<<"EVAL")
180
- def #{attr_name}_#{locale.to_s.underscore}_automatically
214
+ def #{klass.automatic_translation_attribute_name_for(attr_name, locale)}
181
215
  automatic_translation_for(#{locale.inspect}).#{attr_name}_automatically
182
216
  end
183
217
 
184
- def #{attr_name}_#{locale.to_s.underscore}_automatically=(automatically)
218
+ def #{klass.automatic_translation_attribute_name_for(attr_name, locale)}=(automatically)
185
219
  automatic_translation_for(#{locale.inspect}).#{attr_name}_automatically = automatically
186
220
  end
187
221
 
188
- self.automatic_translation_attribute_names.push(:#{attr_name}_#{locale.to_s.underscore}_automatically)
222
+ self.automatic_translation_attribute_names.push(:#{klass.automatic_translation_attribute_name_for(attr_name, locale)})
189
223
  EVAL
190
224
  end
191
225
  end
@@ -221,6 +255,11 @@ EVAL
221
255
  end
222
256
  end
223
257
  end
258
+
259
+ require 'globalize/automatic/translation'
260
+ require 'globalize/automatic/translation_job'
261
+ require 'globalize/automatic/translator/easy_translate'
262
+
224
263
  Globalize::Automatic.asynchronously = false
225
264
  Globalize::Automatic.translator = Globalize::Automatic::Translator::EasyTranslate.new
226
265
 
@@ -238,4 +277,4 @@ Globalize::ActiveRecord::ActMacro.module_eval do
238
277
 
239
278
  alias_method_chain :translates, :automatic
240
279
 
241
- end
280
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Globalize
4
4
  module Automatic
5
- VERSION = '0.0.1'
5
+ VERSION = '0.0.2'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: globalize-automatic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yuichi Takeuchi