Sha256: 70c15cddb7cfb636e6e6db006ddde8a1fce51fbd0a7419269afcd4d8e4c2c6d5

Contents?: true

Size: 1.31 KB

Versions: 12

Compression:

Stored size: 1.31 KB

Contents

# frozen_string_literal: true

module Decidim
  # A command with all the business logic to remove a user from a group.
  class RemoveUserFromGroup < Decidim::Command
    # Public: Initializes the command.
    #
    # membership - the UserGroupMembership to be accepted.
    def initialize(membership, user_group)
      @membership = membership
      @user_group = user_group
    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 membership.blank?
      return broadcast(:invalid) if membership.user_group != user_group

      transaction do
        remove_membership
        send_notification
      end

      broadcast(:ok)
    end

    private

    attr_reader :membership, :user_group

    def remove_membership
      membership.destroy!
    end

    def send_notification
      Decidim::EventsManager.publish(
        event: "decidim.events.groups.removed_from_group",
        event_class: RemovedFromGroupEvent,
        resource: membership.user_group,
        affected_users: [membership.user],
        extra: {
          user_group_name: membership.user_group.name,
          user_group_nickname: membership.user_group.nickname
        }
      )
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
decidim-core-0.27.9 app/commands/decidim/remove_user_from_group.rb
decidim-core-0.27.8 app/commands/decidim/remove_user_from_group.rb
decidim-core-0.27.7 app/commands/decidim/remove_user_from_group.rb
decidim-core-0.27.6 app/commands/decidim/remove_user_from_group.rb
decidim-core-0.27.5 app/commands/decidim/remove_user_from_group.rb
decidim-core-0.27.4 app/commands/decidim/remove_user_from_group.rb
decidim-core-0.27.3 app/commands/decidim/remove_user_from_group.rb
decidim-core-0.27.2 app/commands/decidim/remove_user_from_group.rb
decidim-core-0.27.1 app/commands/decidim/remove_user_from_group.rb
decidim-core-0.27.0 app/commands/decidim/remove_user_from_group.rb
decidim-core-0.27.0.rc2 app/commands/decidim/remove_user_from_group.rb
decidim-core-0.27.0.rc1 app/commands/decidim/remove_user_from_group.rb