Sha256: 80ce497860cfc4bf397bb625399144698f891cf6a31dfec5f2ca1c4500981e3f

Contents?: true

Size: 1.85 KB

Versions: 4

Compression:

Stored size: 1.85 KB

Contents

# frozen_string_literal: true

module Decidim
  module ActionDelegator
    module Admin
      class CreateDelegation < Rectify::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(form.attributes.merge(setting: current_setting))
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
decidim-action_delegator-0.6.0 app/commands/decidim/action_delegator/admin/create_delegation.rb
decidim-action_delegator-0.5.0 app/commands/decidim/action_delegator/admin/create_delegation.rb
decidim-action_delegator-0.4.1 app/commands/decidim/action_delegator/admin/create_delegation.rb
decidim-action_delegator-0.4 app/commands/decidim/action_delegator/admin/create_delegation.rb