Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Customize Invite Forms #770

Open
pranavbhalla opened this issue Jul 31, 2018 · 8 comments
Open

Customize Invite Forms #770

pranavbhalla opened this issue Jul 31, 2018 · 8 comments

Comments

@pranavbhalla
Copy link

Hi, I have two different types of user groups for my app. 'Customer' and 'Staff'. When the admin is sending an invite, I am trying to create different forms for both groups to ensure appropriate inputs for each are filled up.

Please let me know how to go about doing this?

@scambra
Copy link
Owner

scambra commented Jul 31, 2018

You can generate scoped views for each model:
https://github.com/scambra/devise_invitable#configuring-views

@pranavbhalla
Copy link
Author

Thanks. So I have one single Devise user model but there is a type attribute that categorizes each user. Depending on this type, I want to create different invite forms.

@pranavbhalla
Copy link
Author

So I created another Devise model and now I have the following structure - - Staff Model (Devise) - Users model (Devise)

However, my admin is a part of the Users model and is only able to invite Users and not able to invite Staff. How to let the admin invite both ?

@scambra
Copy link
Owner

scambra commented Aug 5, 2018

With one model you should use one view, and you can render a different partial depending on type in that view.

You don't need to create a different devise model if you don't want to, I thought you had 2 models. If you want to use different models, but invite them from one model, you have to change authenticate_inviter!, explained in README

@pranavbhalla
Copy link
Author

pranavbhalla commented Aug 8, 2018

Thanks, that really helps. Also, if my devise model has_many roles and if the admin wants to assign the roles while creating the invitation, I am guessing the def create method in invitations controller would need to be tweaked along with wihitelisting the roles attributes? I tried this but not able to succeed -

def create
 params[:staff][:approle_ids].each do |role|
   if !role.empty?
     resource.approles.build(:usertype_id => role)
   end
 end
 super
end`

@scambra
Copy link
Owner

scambra commented Aug 8, 2018

invite_params will be assigned to resource, so you shouldn't need to tweak create method, as you can see in lines:
https://github.com/scambra/devise_invitable/blob/master/app/controllers/devise/invitations_controller.rb#L19
https://github.com/scambra/devise_invitable/blob/master/app/controllers/devise/invitations_controller.rb#L79

resource is assigned with result of calling invite! with invite_params

However your approle_ids looks like role_ids, so you can't assign them directly to approle_ids. Perhaps, you may use different name, like role_ids, and add role_ids= method to your model.

I think you are trying to build approles in resource before calling super, so self.resource is not assigned yet, not sure what default value is returning devise for resource, but calling super will override it, so your approles are lost. You may try overriding invite_resource with block, so block is passed to invite!:

def invite_resource(&block)
  super do |resource|
    params[:staff][:approle_ids].each do |role|
      resource.approles.build(:usertype_id => role) unless role.empty?
    end
  end
end

@pranavbhalla
Copy link
Author

Ok great thanks, this works. I have another question -

Admin (User model with Admin role assigned) is responsible for sending the invites to new users but the invite must be approved by the Director (User model with Director role assigned) first and only then the invitation should be sent via email. Also, there are additional fields such as Name, address etc. when the admin sends the invite.

Is there an inbuilt devise-invitable feature? If not, is the following the correct way to tackle this ?

Admin fills a simple form with all staff details and sends to Director for approval.
When the Director approves, we trigger the invite mail.
Should we create another model to store the details in 1st step?

@scambra
Copy link
Owner

scambra commented Aug 20, 2018

You can create another model, it would work but probably not needed.

Also, you can send skip_invitation in admin form (add it to invite_params), or override some controller method to set skip_invitation to true as explained in README (set in invite_resource for example). Then director may review invitations which are not sent and approve them which would call user.deliver_invitation to send email.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants