Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you invite another user to a "team" or "account" or "project" with Clearance and Pundit?

I have a Rails app with Clearance and Pundit and I'm trying to create "teams" where the "Team Leader" can invite other users to join their team. I would like to do something similar to devise_invitable, but with Clearance.

Here is my plan for how this might work:

  1. Users that sign up to the site through the signup form are automatically assigned a unique Team ID and become the "Team Lead." They don't see this ID on the form. (Another way this could be done is they create a unique team name that is saved on sign up.) Is the best way to do this to create a before_filter for assigning the team ID and Team Leader on sign up?

  2. Team IDs or names would be unique and each User can only belong to one team. Association would look like this:

    Team

    has_many :users
    

    User

    belongs_to :team
    
  3. Once this "Team Leader" account is created, this user can invite other users to join the team by filling out a User#new form similar to the Clearance sign up form (name, email, etc.) This form would create the User and assign them to the Team Leader's team. They could be assigned a random password to give to Clearance so that the new user passes the validation.

  4. Once a user has been created by the Team Leader they are saved in the database with the randomly generated password and a Mailer is sent to the invited user that is similar to the standard Clearance password reset Mailer. It just gives them a notification that they have been invited to the team and provides a link to reset their password and then log in. It's basically the Clearance password reset Mailer with different copy.

Is this a decent strategy or is there a more widely used pattern I'm missing for solving this problem with Clearance and Pundit? I've never done anything like this with Rails, so I have no idea if this is a ducted-tape way to use Clearance.

like image 467
Lee McAlilly Avatar asked May 23 '18 14:05

Lee McAlilly


1 Answers

Your plan is good. There is no Clearance or Pundit specific way to do it. Of course, your strategy seems fine and you will have to implement it using Clearance.

  1. As far as I understand, you first want to create the user and then create the team for the user. So it would be better to user after_* callback from the available callbacks. before_filter hook is executed before the record is stored in the database. So for example, unless you have the user stored in the database you can not assign it them as Team Leader "before" the user is saved.

  2. If you want to use UUID instead of ID you can do something like this or else you can simply use the unique constraint on name column of teams table. The association you have mentioned seem fine.

  3. You can create a custom controller here which creates a User. Something like app/controllers/team_members#create. I don't think it is needed to be ducted-tape to Clearance. The example code can be:

def create
  member = User.new(team_member_params)
  if member.save
    #some code
  else
    #some code
  end
end
  1. I don't think it would be a good idea to use password-reset emails. You can send out new mailer using after_create hook.

after_create :send_invitation_to_team_member, if: :not_team_leader

like image 103
Kartikey Tanna Avatar answered Nov 14 '22 22:11

Kartikey Tanna