Module: Plutonium::Invites::Controller

Extended by:
ActiveSupport::Concern
Defined in:
lib/plutonium/invites/controller.rb

Overview

Controller provides the invitation acceptance flow for controllers.

This concern handles:

  • Showing the invitation landing page

  • Accepting invitations for logged-in users

  • Signup flow for new users

  • Cookie management for pending invitations

Examples:

Basic usage

class UserInvitationsController < ApplicationController
  include Plutonium::Invites::Controller

  layout "invitation"

  private

  def invite_class
    UserInvite
  end

  def after_accept_path
    root_path
  end

  def 
    rodauth.
  end
end

Instance Method Summary collapse

Instance Method Details

#acceptObject

POST /invitations/:token/accept

Accepts the invitation for the currently logged-in user.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/plutonium/invites/controller.rb', line 71

def accept
  return unless (@invite = load_and_validate_invite(params[:token]))

  unless current_user
    redirect_to invitation_path(token: params[:token]),
      alert: "Please sign in to accept this invitation"
    return
  end

  @invite.accept_for_user!(current_user)
  cookies.delete(:pending_invitation)

  redirect_to after_accept_path,
    notice: "Invitation accepted! Welcome to #{@invite.entity.to_label}!"
rescue ActiveRecord::RecordInvalid => e
  @error_title = "Acceptance Error"
  @error_message = e.record.errors.full_messages.join(", ")
  render :error, status: :forbidden
end

#showObject

GET /invitations/:token

Shows the invitation landing page. If the user is logged in, shows the acceptance form. If not, shows signup/login options.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/plutonium/invites/controller.rb', line 45

def show
  return unless (@invite = load_and_validate_invite(params[:token]))

  # Store invitation token in cookie for later use
  cookies.encrypted[:pending_invitation] = {
    value: params[:token],
    expires: 1.hour.from_now
  }

  if current_user
    begin
      @invite.validate_email_constraints!(current_user.email)
      render :show
    rescue ActiveRecord::RecordInvalid => e
      @error_title = "Email Validation Error"
      @error_message = e.record.errors.full_messages.join(", ")
      render :error, status: :forbidden
    end
  else
    render :landing
  end
end

#signupObject

GET/POST /invitations/:token/signup

Handles new user signup directly from the invitation.



94
95
96
97
98
99
100
101
102
# File 'lib/plutonium/invites/controller.rb', line 94

def 
  return unless (@invite = load_and_validate_invite(params[:token]))

  if request.post?
    
  else
    render :signup
  end
end