module VulgataCallbacks extend ActiveSupport::Concern included do if Vulgata::Helpers.vulgata_table_exists? after_create :init_vulgata_states, unless: :vlg_skip_init after_update :pendify_translation_states after_destroy :destroy_translation_states else logger.warn("WARNING: 'vulgata_translation_states' doesn't exist, vulgata will not create translation states. Please create the table using vulgata's migration and then run the init script again.") end end 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 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 def destroy_translation_states Vulgata::TranslationState.destroy_all(item_type: self.vulgata_item_type, item_id: self.id) end private 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 end