Module: Globalize::Automatic

Defined in:
lib/globalize/automatic/version.rb,
lib/globalize/automatic.rb

Defined Under Namespace

Modules: TranslatedExtension, TranslationExtension Classes: Translation, TranslationJob, Translator

Constant Summary collapse

VERSION =
'0.0.10'

Class Method Summary collapse

Class Method Details

.normalize_locales(locales) ⇒ Object



248
249
250
# File 'lib/globalize/automatic.rb', line 248

def normalize_locales(locales)
  [locales].flatten.compact.map(&:to_sym)
end

.parse_options(options) ⇒ Object



236
237
238
239
240
241
242
243
244
245
246
# File 'lib/globalize/automatic.rb', line 236

def parse_options(options)
  case options
    when Hash
      from_locales = normalize_locales(options[:from])
      to_locales = normalize_locales(options[:to])
    else
      from_locales = normalize_locales(options)
      to_locales = I18n.available_locales
  end
  {from: from_locales, to: to_locales}
end

.setup_automatic_translation!(klass, attr_names, options) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/globalize/automatic.rb', line 194

def setup_automatic_translation!(klass, attr_names, options)
  automatic_options = validate_options(parse_options(options))
  locales = (automatic_options[:from] + automatic_options[:to]).uniq
  klass.globalize_accessors locales: locales, attributes: attr_names
  unless klass.include?(Globalize::Automatic::TranslatedExtension)
    klass.include Globalize::Automatic::TranslatedExtension

    klass.has_many :automatic_translations,
                   dependent: :destroy,
                   autosave: true,
                   class_name: klass.automatic_translation_class.name,
                   foreign_key: klass.name.foreign_key,
                   inverse_of: :automatic_translated_model
    automatic_table_name = "#{klass.table_name.singularize}_automatic_translations"
    klass.automatic_translation_class.table_name = automatic_table_name
    klass.automatic_translation_class.define_singleton_method(:table_name) { automatic_table_name }
  end

  klass.add_automatic_translation_field_locales!(attr_names, automatic_options[:from])
  klass.add_automatic_translated_field_locales!(attr_names, automatic_options[:to])

  attr_names.each do |attr_name|
    locales.each do |locale|
      klass.class_eval(<<"EVAL")
      def #{klass.automatic_translation_attribute_name_for(attr_name, locale)}
        automatic_translation_for(#{locale.inspect}).#{attr_name}_automatically
      end

      def #{klass.automatic_translation_attribute_name_for(attr_name, locale)}=(automatically)
        automatic_translation_for(#{locale.inspect}).#{attr_name}_automatically = automatically
      end
 
      self.automatic_translation_attribute_names.push(:#{klass.automatic_translation_attribute_name_for(attr_name, locale)})
EVAL
    end
  end

  unless klass.translation_class.include?(Globalize::Automatic::TranslationExtension)
    klass.translation_class.include Globalize::Automatic::TranslationExtension
  end
end

.validate_options(options) ⇒ Object



252
253
254
255
256
257
258
259
260
# File 'lib/globalize/automatic.rb', line 252

def validate_options(options)
  if options[:from].empty?
    raise ArgumentError.new('missing :from option')
  elsif options[:to].empty?
    raise ArgumentError.new('missing :to option')
  else
    options
  end
end