Sha256: 6424a2fdd60e0441e0195cfbce31eeb06298b55baec3e594714e0a01b72ce87a

Contents?: true

Size: 1.24 KB

Versions: 12

Compression:

Stored size: 1.24 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 couldn't 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, user_group: user_group).destroy!
    end

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

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

Version data entries

12 entries across 12 versions & 1 rubygems

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