class I18nAdmin::Import::Base

Attributes

locale[R]

Public Class Methods

register(type, import) click to toggle source
# File lib/i18n_admin/import/base.rb, line 6
def self.register(type, import)
  Import.types[type] = import
end

Public Instance Methods

errors() click to toggle source
# File lib/i18n_admin/import/base.rb, line 10
def errors
  @errors ||= I18nAdmin::Errors::Collection.new
end

Private Instance Methods

save_updated_models() click to toggle source
# File lib/i18n_admin/import/base.rb, line 16
def save_updated_models
  # Save all updated model translations
  ActiveRecord::Base.transaction do
    updated_models.each do |key, resource|
      unless resource.save
        errors.add(:resource_invalid, key: key, resource: resource)
      end
    end
  end
end
update_model_translation(key, value) click to toggle source
# File lib/i18n_admin/import/base.rb, line 34
def update_model_translation(key, value)
  _, model_name, id, field = key.split('-')
  update_cache_key = [model_name, id].join('-')

  # Find resource from update cache, or in database
  unless (resource = updated_models[update_cache_key])
    model = model_name.constantize
    resource = model.where(id: id).first
  end

  # Only update found resources
  if resource
    resource.send(:"#{ field }=", value)
    updated_models[update_cache_key] ||= resource
  else
    errors.add(:resource_not_found, {
      key: update_cache_key, model_name: model_name, id: id
    })
  end
end
update_static_translation(key, value) click to toggle source
# File lib/i18n_admin/import/base.rb, line 55
def update_static_translation(key, value)
  I18n.backend.store_translations(locale, key => value)
end
update_translation(key, value) click to toggle source
# File lib/i18n_admin/import/base.rb, line 27
def update_translation(key, value)
  case key
  when /^models\-/ then update_model_translation(key, value)
  else update_static_translation(key, value)
  end
end
updated_models() click to toggle source
# File lib/i18n_admin/import/base.rb, line 59
def updated_models
  @updated_models ||= {}
end