require_dependency "vulgata/application_controller" module Vulgata class TranslationStatesController < ApplicationController before_action :authorize_admin_or_proofreader, only: [:update, :proofread] before_action :authorize_admin, only: [:index] before_action :set_translation_state, only: [:show, :edit, :update, :destroy] before_action :set_translators, only: [:index, :show, :edit] # GET /translation_states def index @translation_states = Vulgata::TranslationState.includes(:item).where.not(status: Vulgata::TranslationState.statuses[:source]) @translation_states = case params[:sort] when 'updated_at_desc' then @translation_states.order('updated_at DESC') when 'updated_at_asc' then @translation_states.order('updated_at ASC') when 'priority_desc' then @translation_states.order('priority DESC') when 'priority_asc' then @translation_states.order('priority ASC') else @translation_states.order(translation_queue_sort) end @translation_states = @translation_states.where('status = ?', Vulgata::TranslationState.statuses[params[:status]]) if (params[:status].present? && params[:status] != "all") @translation_states = @translation_states.page(params[:page]).per(10) end # GET /translation_states/1 def show @is_admin = vulgata_current_user.vulgata_admin? authorize_admin_proofreader_or_assigned_translator @translation_state set_translation_state_and_source_state end # GET /translation_states/1/edit def edit authorize_admin_proofreader_or_assigned_translator @translation_state end # PATCH/PUT /translation_states/1 def update if params[:stop_proofreading].present? stop_proofreading elsif translation_state_params[:status] == "rejected" reject_translation elsif translation_state_params[:status] == "approved" approve_translation else update_translation end end # GET /proofread def proofread set_user_locales @translation_state = dequeue_translation_state_for_proofreading return if @translation_state.nil? @state_changed_to_proofreading = true set_translation_state_and_source_state end private def stop_proofreading @translation_state.user = @translation_state.latest_translated_version.user @translation_state.status = Vulgata::TranslationState.statuses[:done] respond_to do |format| if @translation_state.save format.html { redirect_to root_path, notice: 'Successfully stopped proofreading.' } else format.html { redirect_to translation_state_path(id: @translation_state), notice: 'Error stopping proofreading' } end end end def update_translation # TODO: Make this polymorphic merged_translation_state_params = translation_state_params.merge(user_type: "User") authorize_admin_proofreader_or_assigned_translator @translation_state if @translation_state.update(merged_translation_state_params) redirect_to translation_state_path(@translation_state.id), notice: 'Translation was successfully updated.' else render :edit end end def approve_translation if @translation_state.approve_translation(vulgata_current_user) if params[:proofreading] == "true" redirect_to proofread_path, notice: 'Translation was successfully approved.' else redirect_to translation_state_path(@translation_state), notice: 'Translation was successfully approved.' end else render :edit end end def reject_translation transaction = ActiveRecord::Base.transaction do translator = @translation_state.latest_translated_version.user @translation_state.update(status: :rejected, user: vulgata_current_user, comment: params[:comment]) @translation_state.update(status: :pending, user: translator, locked: true) end if transaction if params[:proofreading] == "true" redirect_to proofread_path, notice: 'Translation was successfully rejected.' else redirect_to translation_state_path(@translation_state.id), notice: 'Translation was successfully rejected.' end else render :edit end end def next_done_translation_state Vulgata::TranslationState .where(status: [Vulgata::TranslationState.statuses[:done], Vulgata::TranslationState.statuses[:proofreading]], locale: @user_locales) .order(translation_queue_sort) .first end def dequeue_translation_state_for_proofreading Vulgata::TranslationState.transaction do translation_state = next_done_translation_state return nil if translation_state.nil? translation_state.status = Vulgata::TranslationState.statuses[:proofreading] translation_state.user = vulgata_current_user translation_state.save translation_state end end def set_user_locales @user_locales = vulgata_current_user.vulgata_locales end def set_translation_state @translation_state = TranslationState.find(params[:id]) end def set_translators @vulgata_users = vulgata_users end def translation_state_params params.require(:translation_state) .permit(:item_type, :item_id, :status, :user_id, :locale, :priority, :stop_proofreading) end end end