Sha256: 4377f8814c371773d309b8ae41c0138782b7b6328013e319e17bbc23ebd59c94

Contents?: true

Size: 1.92 KB

Versions: 1

Compression:

Stored size: 1.92 KB

Contents

# frozen_string_literal: true

module Decidim
  module ActionDelegator
    module Admin
      class CreateDelegation < Decidim::Command
        # Public: Initializes the command.
        #
        # form         - A form object with the params.
        # delegated_by - The user performing the operation
        def initialize(form, performed_by, current_setting)
          @form = form
          @performed_by = performed_by
          @current_setting = current_setting
        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(:error, generic_error_message) if form.invalid? || current_setting.nil?
          return broadcast(:error, above_max_grants_error_message) if above_max_grants?

          create_delegation

          return broadcast(:ok) if delegation.persisted?

          broadcast(:error, delegation.errors.full_messages.first)
        end

        private

        attr_reader :form, :performed_by, :current_setting, :delegation

        def above_max_grants?
          grants_count >= current_setting.max_grants
        end

        def above_max_grants_error_message
          I18n.t("delegations.create.error_max_grants", scope: "decidim.action_delegator.admin")
        end

        def generic_error_message
          I18n.t("delegations.create.error", scope: "decidim.action_delegator.admin")
        end

        def grants_count
          SettingDelegations.new(current_setting).query
                            .where(grantee_id: form.grantee_id)
                            .count
        end

        def create_delegation
          @delegation = Delegation.create(
            granter: form.granter,
            grantee: form.grantee,
            setting: current_setting
          )
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
decidim-action_delegator-0.8.1 app/commands/decidim/action_delegator/admin/create_delegation.rb