Sha256: 5a2cb95aa1c72bcd669a2c72f3f8f032cb4dc7841a050596ed1d3c1e4f6a206a

Contents?: true

Size: 1.72 KB

Versions: 15

Compression:

Stored size: 1.72 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 CreateTranslationSet < 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
            @set = create_translation_set!
          end

          if @set.persisted?
            broadcast(:ok, @set)
          else
            broadcast(:invalid)
          end
        end

        private

        attr_reader :form

        def create_translation_set!
          translation_set = TermCustomizer::TranslationSet.create!(
            name: form.name
          )

          form.constraints.each do |c|
            next if c.deleted

            attrs = { organization: form.current_organization }
            if c.subject
              attrs[:subject] = c.subject
            else
              attrs[:subject_type] = c.subject_type
            end

            translation_set.constraints.create!(attrs)
          end

          if translation_set.constraints.count < 1
            # Make sure that the organization constraint at least exists always
            translation_set.constraints.create!(organization: form.current_organization)
          end

          translation_set
        end
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
decidim-term_customizer-0.23.0 app/commands/decidim/term_customizer/admin/create_translation_set.rb
decidim-term_customizer-0.22.0 app/commands/decidim/term_customizer/admin/create_translation_set.rb
decidim-term_customizer-0.21.0 app/commands/decidim/term_customizer/admin/create_translation_set.rb
decidim-term_customizer-0.20.0 app/commands/decidim/term_customizer/admin/create_translation_set.rb
decidim-term_customizer-0.19.1 app/commands/decidim/term_customizer/admin/create_translation_set.rb
decidim-term_customizer-0.19.0 app/commands/decidim/term_customizer/admin/create_translation_set.rb
decidim-term_customizer-0.18.0 app/commands/decidim/term_customizer/admin/create_translation_set.rb
decidim-term_customizer-0.17.1 app/commands/decidim/term_customizer/admin/create_translation_set.rb
decidim-term_customizer-0.17.0 app/commands/decidim/term_customizer/admin/create_translation_set.rb
decidim-term_customizer-0.16.6 app/commands/decidim/term_customizer/admin/create_translation_set.rb
decidim-term_customizer-0.16.5 app/commands/decidim/term_customizer/admin/create_translation_set.rb
decidim-term_customizer-0.16.3 app/commands/decidim/term_customizer/admin/create_translation_set.rb
decidim-term_customizer-0.16.2 app/commands/decidim/term_customizer/admin/create_translation_set.rb
decidim-term_customizer-0.16.1 app/commands/decidim/term_customizer/admin/create_translation_set.rb
decidim-term_customizer-0.16.0 app/commands/decidim/term_customizer/admin/create_translation_set.rb