Sha256: 86657857a50abde6bc70b617c715bbbe9f5a33d1d6e120dcc507778a4225cbf2

Contents?: true

Size: 1.51 KB

Versions: 1

Compression:

Stored size: 1.51 KB

Contents

# frozen_string_literal: true

module Decidim
  module TermCustomizer
    module Admin
      # A command with all the business logic when creating new translations
      # from the keys submitted through the form.
      class ImportTranslationKeys < Rectify::Command
        # Public: Initializes the command.
        #
        # form - A form object with the params.
        def initialize(form)
          @form = form
        end

        # Executes the command. Broadcasts these events:
        #
        # - :ok when everything is valid.
        # - :invalid if the form wasn't valid and we couldn't proceed.
        #
        # Returns nothing.
        def call
          return broadcast(:invalid) if form.invalid?

          transaction do
            @translations = create_translations
          end

          if @translations.length.positive?
            broadcast(:ok, @translations)
          else
            broadcast(:invalid)
          end
        end

        private

        attr_reader :form

        def create_translations
          items = form.keys.map do |key|
            form.current_organization.available_locales.map do |locale|
              attrs = {
                key: key,
                locale: locale
              }
              next unless TermCustomizer::Translation.find_by(attrs).nil?

              attrs.merge(value: I18n.t(key, locale: locale, default: ""))
            end
          end.flatten

          form.translation_set.translations.create!(items)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
decidim-term_customizer-0.16.5 app/commands/decidim/term_customizer/admin/import_translation_keys.rb