Sha256: 9412a6f01fc405f93140be320f9b45d17109900645a0a2d9eb5bff94ae30075a

Contents?: true

Size: 1.2 KB

Versions: 2

Compression:

Stored size: 1.2 KB

Contents

# frozen_string_literal: true

module Decidim
  module DecidimAwesome
    module Admin
      class UpdateConstraint < Command
        # Public: Initializes the command.
        #
        # form - A constraint form
        def initialize(form)
          @form = form
        end

        # Executes the command. Broadcasts these events:
        #
        # - :ok when everything is valid.
        # - :invalid if we couldn't proceed.
        #
        # Returns nothing.
        def call
          return broadcast(:invalid, form.errors.full_messages.join(". ")) if form.invalid?
          return broadcast(:invalid) if attributes.blank?

          begin
            constraint = ConfigConstraint.find(form.id)
            constraint.settings = attributes
            constraint.save!
            broadcast(:ok)
          rescue ActiveRecord::RecordNotUnique
            broadcast(:invalid, I18n.t("decidim.decidim_awesome.admin.constraints.errors.not_unique"))
          rescue StandardError => e
            broadcast(:invalid, e.message)
          end
        end

        attr_reader :form

        def attributes
          form.attributes.filter { |key, val| key != "id" && val.present? }
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
decidim-decidim_awesome-0.11.2 app/commands/decidim/decidim_awesome/admin/update_constraint.rb
decidim-decidim_awesome-0.11.1 app/commands/decidim/decidim_awesome/admin/update_constraint.rb