Sha256: 767d67881aabc63cbd112732b2b806d140795b9f0f638b7bfb589e7041b6c738

Contents?: true

Size: 1.73 KB

Versions: 8

Compression:

Stored size: 1.73 KB

Contents

# frozen_string_literal: true

module Decidim
  module TermCustomizer
    module Admin
      # This command is executed when the user changes a translation from the
      # admin panel.
      class UpdateTranslation < Rectify::Command
        # Public: Initializes the command.
        #
        # form        - A form object with the params.
        # translation - The current instance of the translation to be updated.
        def initialize(form, translation)
          @form = form
          @translation = translation
        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
            update_translations!
          end

          broadcast(:ok, translation)
        end

        private

        attr_reader :form, :translation

        def update_translations!
          form.value.each do |locale, value|
            l_translation = TermCustomizer::Translation.find_by(
              translation_set: translation.translation_set,
              key: translation.key,
              locale: locale
            )

            if l_translation
              l_translation.update!(
                key: form.key,
                value: value,
                locale: locale
              )
            else
              TermCustomizer::Translation.create!(
                translation_set: translation.translation_set,
                key: form.key,
                value: value,
                locale: locale
              )
            end
          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/update_translation.rb
decidim-term_customizer-0.17.0 app/commands/decidim/term_customizer/admin/update_translation.rb
decidim-term_customizer-0.16.6 app/commands/decidim/term_customizer/admin/update_translation.rb
decidim-term_customizer-0.16.5 app/commands/decidim/term_customizer/admin/update_translation.rb
decidim-term_customizer-0.16.3 app/commands/decidim/term_customizer/admin/update_translation.rb
decidim-term_customizer-0.16.2 app/commands/decidim/term_customizer/admin/update_translation.rb
decidim-term_customizer-0.16.1 app/commands/decidim/term_customizer/admin/update_translation.rb
decidim-term_customizer-0.16.0 app/commands/decidim/term_customizer/admin/update_translation.rb