Skip to content

Commit

Permalink
Registration + invite cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Shpigford committed Nov 1, 2024
1 parent 9438238 commit ee27e60
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 14 deletions.
4 changes: 0 additions & 4 deletions app/controllers/invitations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ def create
@invitation = Current.family.invitations.build(invitation_params)
@invitation.inviter = Current.user

if @invitation.role == "admin" && !Current.user.admin?
@invitation.role = "member"
end

if @invitation.save
InvitationMailer.invite_email(@invitation).deliver_later
flash[:notice] = t(".success")
Expand Down
17 changes: 7 additions & 10 deletions app/controllers/registrations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class RegistrationsController < ApplicationController
layout "auth"

before_action :set_user, only: :create
before_action :load_invitation, if: :invitation_token?
before_action :set_invitation
before_action :claim_invite_code, only: :create, if: :invite_code_required?

def new
Expand Down Expand Up @@ -34,21 +34,18 @@ def create

private

def load_invitation
token = params[:invitation] || params.dig(:user, :invitation)
@invitation = Invitation.pending.find_by!(token: token)
end

def invitation_token?
params[:invitation].present? || params.dig(:user, :invitation).present?
def set_invitation
token = params[:invitation] || user_params(:invitation)
@invitation = Invitation.pending.find_by(token: token)
end

def set_user
@user = User.new user_params.except(:invite_code, :invitation)
end

def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation, :invite_code, :invitation)
def user_params(specific_param = nil)
params = self.params.require(:user).permit(:name, :email, :password, :password_confirmation, :invite_code, :invitation)
specific_param ? params[specific_param] : params
end

def claim_invite_code
Expand Down
5 changes: 5 additions & 0 deletions app/models/invitation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class Invitation < ApplicationRecord
validates :email, presence: true, format: { with: URI::MailTo::EMAIL_REGEXP }
validates :role, presence: true, inclusion: { in: %w[admin member] }
validates :token, presence: true, uniqueness: true
validate :inviter_is_admin

before_validation :generate_token, on: :create
before_create :set_expiration
Expand All @@ -29,4 +30,8 @@ def generate_token
def set_expiration
self.expires_at = 3.days.from_now
end

def inviter_is_admin
inviter.admin?
end
end

0 comments on commit ee27e60

Please sign in to comment.