1

I am building a rails app which has projects which users (handles by Devise) can be invited to as collaborators (much like GitHub repositories and collaborators). I am struggling getting the invitations running (removing collaborators specifically). I have been following this tutorial to get the creation of invitations running but the tutorial does not cover revoking the invitations (removing collaborators).

I have a projects_controller.rb file and an invites_controller.rb file. The invites controller handles the creation of new invitations, which is working fine (i.e: if a user already exists, they are immediately added to a project, if a user doesn't exist, an invitation is sent to the entered email address).

How should I go about adding functionality for removing collaborators? To me, it would seam logical to use an invites#destroy (being that invitations are created in that controller) but then, simply deleting an invite will not revoke the user's permission to a project. And what about the users who create a project initially, they will not have an invitation at all..

Does anyone know which path I should take here? Let me know if any more information would be helpful.

Thanks

simonlehmann
  • 852
  • 1
  • 10
  • 27

2 Answers2

2

Assuming that you handle collaborators to a project with a has_many association:

class Project < ApplicationRecord
  has_many :users
end

then you can implement invites#destroy to remove a user from the list of collaborators:

class InvitesController < ApplicationController
  def destroy
    @project = Project.find params[:project_id]
    @user = User.find params[:user_to_remove]
    @project.users.delete(@user)
    # Add whatever renders or redirects you need to here
  end
end

Your view can use this button to remove a collaborator:

# Make sure @project (the project to remove from) and @user (the user to remove) are defined and non-nil
<%= link_to "Remove Collaborator", url_for(:controller => :invites, :action => :destroy, :project_id => @project.id, :user_to_remove => @user.id), :method => :delete %>
ArtOfCode
  • 5,702
  • 5
  • 37
  • 56
0

Use the gem 'devise_invitable' for handling invitations. https://github.com/scambra/devise_invitable

Chakreshwar Sharma
  • 2,571
  • 1
  • 11
  • 35
  • Hi, Thanks for your response. Correct me if i'm wrong, but as I understand it, devise_invitable is great for application-wide invitations (e.g: signing up for the app via referral), but doesn't give you much for scoped invitations.. – simonlehmann Sep 15 '16 at 09:29
  • It provides several scopes , you can override it as per your business logic – Chakreshwar Sharma Sep 15 '16 at 09:30