Class: Decidim::DemoteMembership

Inherits:
Command
  • Object
show all
Defined in:
decidim-core/app/commands/decidim/demote_membership.rb

Overview

A command with all the business logic to demote an admin. This means removing their admin righta dn converting them to a basic member. It is the coutnerpart of ‘PromoteMembership`.

Instance Method Summary collapse

Methods inherited from Command

call, #evaluate, #method_missing, #respond_to_missing?, #transaction, #with_events

Constructor Details

#initialize(membership, user_group) ⇒ DemoteMembership

Public: Initializes the command.

membership - the UserGroupMembership to be demoted.



11
12
13
14
# File 'decidim-core/app/commands/decidim/demote_membership.rb', line 11

def initialize(membership, user_group)
  @membership = membership
  @user_group = user_group
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Decidim::Command

Instance Method Details

#callObject

Executes the command. Broadcasts these events:

  • :ok when everything is valid.

  • :invalid if we could not proceed.

Returns nothing.



22
23
24
25
26
27
28
29
30
31
32
33
# File 'decidim-core/app/commands/decidim/demote_membership.rb', line 22

def call
  return broadcast(:invalid) if membership.blank?
  return broadcast(:invalid) if membership.role != "admin"
  return broadcast(:invalid) if membership.user_group != user_group

  transaction do
    demote_membership
    send_notification
  end

  broadcast(:ok)
end