Sha256: c649fcdb89d618d549aaa801fc996814ebdada9098aab2819d53b40a5abe73cd

Contents?: true

Size: 1.97 KB

Versions: 8

Compression:

Stored size: 1.97 KB

Contents

# frozen_string_literal: true

module Decidim
  module TermCustomizer
    module Admin
      class AddTranslationsController < TermCustomizer::Admin::ApplicationController
        helper_method :set

        def index
          enforce_permission_to :read, :translation

          @form = form(TranslationKeyImportForm).from_model(Translation.new)
        end

        def create
          enforce_permission_to :create, :translation
          @form = form(TranslationKeyImportForm).from_params(
            params,
            translation_set: set
          )

          ImportTranslationKeys.call(@form) do
            on(:ok) do
              flash[:notice] = I18n.t("translations.create.success", scope: "decidim.term_customizer.admin")
              redirect_to translation_set_translations_path(set)
            end

            on(:invalid) do
              flash.now[:alert] = I18n.t("translations.create.error", scope: "decidim.term_customizer.admin")
              render action: "index"
            end
          end
        end

        def search
          enforce_permission_to :read, :translation
          return render json: [] unless params.has_key?(:term)

          directory = TranslationDirectory.new(current_locale)
          translations = directory.translations_search(params[:term])
          translations.reject! { |k| reject_keys.include?(k) }

          render json: translations.map { |k, v| [k, v] }
        end

        private

        def translation_set
          @translation_set ||= OrganizationTranslationSets.new(
            current_organization
          ).query.find(params[:translation_set_id])
        end

        def reject_keys
          @reject_keys ||= (request_keys + existing_keys).uniq
        end

        def request_keys
          return params[:keys] if params.has_key?(:keys)

          []
        end

        def existing_keys
          SetTranslations.new(set).pluck(:key).uniq
        end

        alias set translation_set
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
decidim-term_customizer-0.17.1 app/controllers/decidim/term_customizer/admin/add_translations_controller.rb
decidim-term_customizer-0.17.0 app/controllers/decidim/term_customizer/admin/add_translations_controller.rb
decidim-term_customizer-0.16.6 app/controllers/decidim/term_customizer/admin/add_translations_controller.rb
decidim-term_customizer-0.16.5 app/controllers/decidim/term_customizer/admin/add_translations_controller.rb
decidim-term_customizer-0.16.3 app/controllers/decidim/term_customizer/admin/add_translations_controller.rb
decidim-term_customizer-0.16.2 app/controllers/decidim/term_customizer/admin/add_translations_controller.rb
decidim-term_customizer-0.16.1 app/controllers/decidim/term_customizer/admin/add_translations_controller.rb
decidim-term_customizer-0.16.0 app/controllers/decidim/term_customizer/admin/add_translations_controller.rb