Module: Decidim::Admin::CreateParticipatorySpaceAdminUserActions

Defined in:
app/commands/decidim/admin/create_participatory_space_admin_user_actions.rb

Overview

This is not a Command but an abstration of reusable methods by commands in participatory spaces that create space admins. Expects the command to have a ‘participatory_space` attribute.

Instance Method Summary collapse

Instance Method Details

#add_admin_as_followerObject



80
81
82
83
84
85
86
87
88
89
90
91
# File 'app/commands/decidim/admin/create_participatory_space_admin_user_actions.rb', line 80

def add_admin_as_follower
  return if user.follows?(@participatory_space)

  form = Decidim::FollowForm
         .from_params(followable_gid: @participatory_space.to_signed_global_id.to_s)
         .with_context(
           current_organization: @participatory_space.organization,
           current_user: user
         )

  Decidim::CreateFollow.new(form, user).call
end

#callObject

Executes the command. Broadcasts these events:

  • :ok when everything is valid.

  • :invalid if the form wasn’t valid and we couldn’t proceed.

Returns nothing.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/commands/decidim/admin/create_participatory_space_admin_user_actions.rb', line 14

def call
  return broadcast(:invalid) if form.invalid?

  ActiveRecord::Base.transaction do
    @user ||= existing_user || new_user
    existing_role || create_role
    add_admin_as_follower
  end

  broadcast(:ok)
rescue ActiveRecord::RecordInvalid
  form.errors.add(:email, :taken)
  broadcast(:invalid)
end

#create_roleObject

This is command specific It is expected to

  • create a XxxUserRole using ‘Decidim.traceability`

  • send a notification to the user telling she has been invited to manage the participatory space

Raises:

  • (NotImplementedError)


40
41
42
# File 'app/commands/decidim/admin/create_participatory_space_admin_user_actions.rb', line 40

def create_role
  raise NotImplementedError
end

#existing_roleObject

This is command specific It is expected to find if a UserRole for the same user, role and participatory_process already exist Return a boolean, or some object equally evaluable

Raises:

  • (NotImplementedError)


32
33
34
# File 'app/commands/decidim/admin/create_participatory_space_admin_user_actions.rb', line 32

def existing_role
  raise NotImplementedError
end

#existing_userObject



44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/commands/decidim/admin/create_participatory_space_admin_user_actions.rb', line 44

def existing_user
  return @existing_user if defined?(@existing_user)

  @existing_user = User.find_by(
    email: form.email,
    organization: @participatory_space.organization
  )

  InviteUserAgain.call(@existing_user, invitation_instructions) if @existing_user&.invitation_pending?

  @existing_user
end

#invitation_instructionsObject



74
75
76
77
78
# File 'app/commands/decidim/admin/create_participatory_space_admin_user_actions.rb', line 74

def invitation_instructions
  return "invite_admin" if form.role == "admin"

  "invite_collaborator"
end

#new_userObject



57
58
59
60
61
62
63
# File 'app/commands/decidim/admin/create_participatory_space_admin_user_actions.rb', line 57

def new_user
  @new_user ||= InviteUser.call(user_form) do
    on(:ok) do |user|
      return user
    end
  end
end

#user_formObject



65
66
67
68
69
70
71
72
# File 'app/commands/decidim/admin/create_participatory_space_admin_user_actions.rb', line 65

def user_form
  OpenStruct.new(name: form.name,
                 email: form.email.downcase,
                 organization: @participatory_space.organization,
                 admin: false,
                 invited_by: current_user,
                 invitation_instructions: invitation_instructions)
end