Sha256: 93fc1e2c3e7129dccbd73578426f5ec179dedcd478e3bfe9773e5546a0c589c9

Contents?: true

Size: 1.29 KB

Versions: 8

Compression:

Stored size: 1.29 KB

Contents

# frozen_string_literal: true

module Decidim
  module TermCustomizer
    module Admin
      # A command with all the business logic when creating a new translation
      # set in the system.
      class CreateTranslation < 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
          form.value.map do |locale, value|
            TermCustomizer::Translation.create!(
              translation_set: form.translation_set,
              key: form.key,
              value: value,
              locale: locale
            )
          end
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

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