module VulgataCallbacks

Public Instance Methods

destroy_translation_states() click to toggle source
# File lib/vulgata/concerns/vulgata_callbacks.rb, line 83
def destroy_translation_states
  Vulgata::TranslationState.destroy_all(item_type: self.vulgata_item_type, item_id: self.id)
end
init_vulgata_states(reload = true) click to toggle source
# File lib/vulgata/concerns/vulgata_callbacks.rb, line 14
def init_vulgata_states(reload = true)
  
  source_translation, translated_item_id = vulgata_source_and_translated_item_id

  # no source no states - translators cant translate without source
  return if source_translation.nil?

  source_attributes = {
    item_type: self.vulgata_item_type,
    item_id: translated_item_id,
    status: Vulgata::TranslationState.statuses[:source],
    locale: source_translation.locale
  }

  Vulgata::TranslationState.find_or_create_by(source_attributes) do |state|
    state.created_at = source_translation.created_at
    state.updated_at = source_translation.updated_at
  end

  not_source_locales = Vulgata::Helpers.available_locales - [source_translation.locale.to_sym]

  not_source_locales.each do |locale|
    state = Vulgata::TranslationState.find_or_initialize_by(item_type: self.vulgata_item_type, item_id: translated_item_id, locale: locale)
    state.priority = self.vulgata_priority
    translation = vlg_strategy.translation_data_with_meta state
    if translation.present?
      state.status = Vulgata::TranslationState.statuses[:approved]
      state.comment = "Initial"
      state.created_at = translation.created_at
      state.updated_at = translation.updated_at
    else
      state.status = Vulgata::TranslationState.statuses[:pending]
      state.comment = "Initial"
      state.created_at = source_translation.created_at
      state.updated_at = source_translation.updated_at
    end
    state.save
  end

  self.reload if reload
end
pendify_translation_states() click to toggle source
# File lib/vulgata/concerns/vulgata_callbacks.rb, line 56
def pendify_translation_states
  # after translation is created we should skip the update
  if self.vlg_skip_pendify
    self.vlg_skip_pendify = nil
    return true
  end

  # check if the update includes the attributes being translated
  return unless vlg_strategy.translation_data_changed? self

  source_translation, translated_item_id = vulgata_source_and_translated_item_id

  # no source no states - translators cant translate without source
  return if source_translation.nil?

  not_source_locales = Vulgata::Helpers.available_locales - [source_translation.locale.to_sym]

  # update all translations to pending
  not_source_locales.each do |locale|
    state = Vulgata::TranslationState.where(item_type: self.vulgata_item_type, item_id: translated_item_id, locale: locale).first_or_initialize
    state.status = :pending
    state.user = nil
    state.priority = self.vulgata_priority
    state.save
  end
end

Private Instance Methods

vulgata_source_and_translated_item_id() click to toggle source
# File lib/vulgata/concerns/vulgata_callbacks.rb, line 89
def vulgata_source_and_translated_item_id
  source_translation = self.vlg_strategy.source_translation(self)

  # some translation strategies use the same table with locale column and some have another translation table
  # we should handle both situations to find the coresponding id by checking the class of the translation
  translating_item_id = source_translation.class == self.class ? source_translation.id : self.id
  return source_translation, translating_item_id
end