Module: Decidim::Admin::CreateParticipatorySpaceAdminUserActions

Included in:
Decidim::Assemblies::Admin::CreateAssemblyAdmin, ParticipatoryProcesses::Admin::CreateParticipatoryProcessAdmin
Defined in:
decidim-admin/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


84
85
86
87
88
89
90
91
92
93
94
95
# File 'decidim-admin/app/commands/decidim/admin/create_participatory_space_admin_user_actions.rb', line 84

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 'decidim-admin/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 'decidim-admin/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 'decidim-admin/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 'decidim-admin/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)

  @existing_user
end

#invitation_instructionsObject


78
79
80
81
82
# File 'decidim-admin/app/commands/decidim/admin/create_participatory_space_admin_user_actions.rb', line 78

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

  "invite_collaborator"
end

#invitation_pending?(user) ⇒ Boolean

Returns:

  • (Boolean)

57
58
59
# File 'decidim-admin/app/commands/decidim/admin/create_participatory_space_admin_user_actions.rb', line 57

def invitation_pending?(user)
  user.invited_to_sign_up? && !user.invitation_accepted?
end

#new_userObject


61
62
63
64
65
66
67
# File 'decidim-admin/app/commands/decidim/admin/create_participatory_space_admin_user_actions.rb', line 61

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

#user_formObject


69
70
71
72
73
74
75
76
# File 'decidim-admin/app/commands/decidim/admin/create_participatory_space_admin_user_actions.rb', line 69

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