Sha256: 6a48116bfea560c5c195e2680fa168feb091cd2aa0392b7090198833fc34856a

Contents?: true

Size: 1.2 KB

Versions: 13

Compression:

Stored size: 1.2 KB

Contents

# frozen_string_literal: true

module Decidim
  # A command with all the business logic to leave a user group.
  class LeaveUserGroup < Decidim::Command
    # Public: Initializes the command.
    #
    # user - the user that wants to leave the group
    # user_group - The user group to leave
    def initialize(user, user_group)
      @user = user
      @user_group = user_group
    end

    # Executes the command. Broadcasts these events:
    #
    # - :ok when everything is valid.
    # - :invalid if we could not proceed.
    #
    # Returns nothing.
    def call
      return broadcast(:invalid) unless can_leave?
      return broadcast(:last_admin) if last_admin?

      leave_user_group

      broadcast(:ok, @user_group)
    end

    private

    attr_reader :user, :user_group

    def leave_user_group
      Decidim::UserGroupMembership.find_by!(user:, user_group:).destroy!
    end

    def can_leave?
      Decidim::UserGroupMembership.where(user:, user_group:).any?
    end

    def last_admin?
      admin_memberships = Decidim::UserGroupMembership.where(user_group:, role: [:creator, :admin])
      admin_memberships.length == 1 && admin_memberships.pluck(:decidim_user_id).include?(user.id)
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
decidim-core-0.29.1 app/commands/decidim/leave_user_group.rb
decidim-core-0.28.4 app/commands/decidim/leave_user_group.rb
decidim-core-0.29.0 app/commands/decidim/leave_user_group.rb
decidim-core-0.28.3 app/commands/decidim/leave_user_group.rb
decidim-core-0.29.0.rc4 app/commands/decidim/leave_user_group.rb
decidim-core-0.29.0.rc3 app/commands/decidim/leave_user_group.rb
decidim-core-0.29.0.rc2 app/commands/decidim/leave_user_group.rb
decidim-core-0.29.0.rc1 app/commands/decidim/leave_user_group.rb
decidim-core-0.28.2 app/commands/decidim/leave_user_group.rb
decidim-core-0.28.1 app/commands/decidim/leave_user_group.rb
decidim-core-0.28.0 app/commands/decidim/leave_user_group.rb
decidim-core-0.28.0.rc5 app/commands/decidim/leave_user_group.rb
decidim-core-0.28.0.rc4 app/commands/decidim/leave_user_group.rb