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