Sha256: d4f035baf5f34b384658eb9a7ac6d4f83283c8f80f3300247bbd33e79fba5451

Contents?: true

Size: 1.22 KB

Versions: 15

Compression:

Stored size: 1.22 KB

Contents

# frozen_string_literal: true

module Decidim
  module DecidimAwesome
    module Admin
      class CreateConstraint < Rectify::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) if form.invalid?
          return broadcast(:invalid) if attributes.blank?

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

        private

        attr_reader :form, :constraint

        def attributes
          form.attributes.filter { |_i, v| v.present? }
        end
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
decidim-decidim_awesome-0.8.3 app/commands/decidim/decidim_awesome/admin/create_constraint.rb
decidim-decidim_awesome-0.8.2 app/commands/decidim/decidim_awesome/admin/create_constraint.rb
decidim-decidim_awesome-0.8.1 app/commands/decidim/decidim_awesome/admin/create_constraint.rb
decidim-decidim_awesome-0.8.0 app/commands/decidim/decidim_awesome/admin/create_constraint.rb
decidim-decidim_awesome-0.7.2 app/commands/decidim/decidim_awesome/admin/create_constraint.rb
decidim-decidim_awesome-0.7.0 app/commands/decidim/decidim_awesome/admin/create_constraint.rb
decidim-decidim_awesome-0.6.7 app/commands/decidim/decidim_awesome/admin/create_constraint.rb
decidim-decidim_awesome-0.6.6 app/commands/decidim/decidim_awesome/admin/create_constraint.rb
decidim-decidim_awesome-0.6.5 app/commands/decidim/decidim_awesome/admin/create_constraint.rb
decidim-decidim_awesome-0.6.4 app/commands/decidim/decidim_awesome/admin/create_constraint.rb
decidim-decidim_awesome-0.6.3 app/commands/decidim/decidim_awesome/admin/create_constraint.rb
decidim-decidim_awesome-0.6.2 app/commands/decidim/decidim_awesome/admin/create_constraint.rb
decidim-decidim_awesome-0.6.1 app/commands/decidim/decidim_awesome/admin/create_constraint.rb
decidim-decidim_awesome-0.6.0 app/commands/decidim/decidim_awesome/admin/create_constraint.rb
decidim-decidim_awesome-0.5.1 app/commands/decidim/decidim_awesome/admin/create_constraint.rb