Sha256: 33d4d6571ba211f0c103f5bcc0e2223e77218b061c033603d957ed46e58e5bec

Contents?: true

Size: 1.53 KB

Versions: 7

Compression:

Stored size: 1.53 KB

Contents

# frozen_string_literal: true

module Decidim
  module TermCustomizer
    module Admin
      # This command is executed when the user duplicates a translation set from
      # the admin panel.
      class DuplicateTranslationSet < Rectify::Command
        # Initializes a DuplicateTranslationSet Command.
        #
        # form - A form object with the params.
        # set  - The instance of the translation set to be duplicated.
        def initialize(form, set)
          @form = form
          @set = set
        end

        # Updates the blog if valid.
        #
        # Broadcasts :ok if successful, :invalid otherwise.
        def call
          return broadcast(:invalid) if form.invalid?

          transaction do
            duplicate_translation_set!
          end

          broadcast(:ok, set)
        end

        private

        attr_reader :form, :set

        def duplicate_translation_set!
          duplicated = TermCustomizer::TranslationSet.create!(name: form.name)

          # Add the constraints
          set.constraints.each do |c|
            duplicated.constraints.create!(
              organization: form.current_organization,
              subject: c.subject,
              subject_type: c.subject_type
            )
          end

          # Add the translations
          set.translations.each do |t|
            duplicated.translations.create!(
              locale: t.locale,
              key: t.key,
              value: t.value
            )
          end

          duplicated
        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/duplicate_translation_set.rb
decidim-term_customizer-0.22.0 app/commands/decidim/term_customizer/admin/duplicate_translation_set.rb
decidim-term_customizer-0.21.0 app/commands/decidim/term_customizer/admin/duplicate_translation_set.rb
decidim-term_customizer-0.20.0 app/commands/decidim/term_customizer/admin/duplicate_translation_set.rb
decidim-term_customizer-0.19.1 app/commands/decidim/term_customizer/admin/duplicate_translation_set.rb
decidim-term_customizer-0.19.0 app/commands/decidim/term_customizer/admin/duplicate_translation_set.rb
decidim-term_customizer-0.18.0 app/commands/decidim/term_customizer/admin/duplicate_translation_set.rb