Sha256: 4313de22de19144d40650479f3eb5c55db5fd71b6b592a7cba2bcf6823003ceb

Contents?: true

Size: 1.58 KB

Versions: 15

Compression:

Stored size: 1.58 KB

Contents

# frozen_string_literal: true

module Decidim
  module TermCustomizer
    module Admin
      # This command is executed when the user changes a translation set from
      # the admin panel.
      class UpdateTranslationSet < Rectify::Command
        # Initializes a UpdateTranslationSet Command.
        #
        # form - The form from which to get the data.
        # set  - The current instance of the translation set to be updated.
        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
            update_translation_set!
          end

          broadcast(:ok, set)
        end

        private

        attr_reader :form, :set

        def update_translation_set!
          set.update!(name: form.name)

          # Update the constraints
          set.constraints.destroy_all
          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

            set.constraints.create!(attrs)
          end

          if set.constraints.count < 1
            # Make sure that the organization constraint at least exists always
            set.constraints.create!(organization: form.current_organization)
          end
        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/update_translation_set.rb
decidim-term_customizer-0.22.0 app/commands/decidim/term_customizer/admin/update_translation_set.rb
decidim-term_customizer-0.21.0 app/commands/decidim/term_customizer/admin/update_translation_set.rb
decidim-term_customizer-0.20.0 app/commands/decidim/term_customizer/admin/update_translation_set.rb
decidim-term_customizer-0.19.1 app/commands/decidim/term_customizer/admin/update_translation_set.rb
decidim-term_customizer-0.19.0 app/commands/decidim/term_customizer/admin/update_translation_set.rb
decidim-term_customizer-0.18.0 app/commands/decidim/term_customizer/admin/update_translation_set.rb
decidim-term_customizer-0.17.1 app/commands/decidim/term_customizer/admin/update_translation_set.rb
decidim-term_customizer-0.17.0 app/commands/decidim/term_customizer/admin/update_translation_set.rb
decidim-term_customizer-0.16.6 app/commands/decidim/term_customizer/admin/update_translation_set.rb
decidim-term_customizer-0.16.5 app/commands/decidim/term_customizer/admin/update_translation_set.rb
decidim-term_customizer-0.16.3 app/commands/decidim/term_customizer/admin/update_translation_set.rb
decidim-term_customizer-0.16.2 app/commands/decidim/term_customizer/admin/update_translation_set.rb
decidim-term_customizer-0.16.1 app/commands/decidim/term_customizer/admin/update_translation_set.rb
decidim-term_customizer-0.16.0 app/commands/decidim/term_customizer/admin/update_translation_set.rb