Sha256: ae3dc359b9e02d3fbb7abd6616fec69f0e8bbb281bbc79519d875eccb7444290

Contents?: true

Size: 1.61 KB

Versions: 7

Compression:

Stored size: 1.61 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
        include TermCustomizer::PluralFormsForm

        # 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
            create_plural_forms(@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 form.translation_set.translations.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

7 entries across 7 versions & 1 rubygems

Version Path
decidim-term_customizer-0.23.0 app/commands/decidim/term_customizer/admin/import_translation_keys.rb
decidim-term_customizer-0.22.0 app/commands/decidim/term_customizer/admin/import_translation_keys.rb
decidim-term_customizer-0.21.0 app/commands/decidim/term_customizer/admin/import_translation_keys.rb
decidim-term_customizer-0.20.0 app/commands/decidim/term_customizer/admin/import_translation_keys.rb
decidim-term_customizer-0.19.1 app/commands/decidim/term_customizer/admin/import_translation_keys.rb
decidim-term_customizer-0.19.0 app/commands/decidim/term_customizer/admin/import_translation_keys.rb
decidim-term_customizer-0.18.0 app/commands/decidim/term_customizer/admin/import_translation_keys.rb