2022-05-23 15:09:22 +02:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: targeted_user_links
|
|
|
|
#
|
|
|
|
# id :uuid not null, primary key
|
|
|
|
# target_context :string not null
|
|
|
|
# target_model_type :string not null
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
|
|
|
# target_model_id :bigint not null
|
2022-06-13 16:00:41 +02:00
|
|
|
# user_id :bigint
|
2022-05-23 15:09:22 +02:00
|
|
|
#
|
|
|
|
class TargetedUserLink < ApplicationRecord
|
2022-06-13 16:00:41 +02:00
|
|
|
belongs_to :user, optional: true
|
2022-05-23 15:09:22 +02:00
|
|
|
belongs_to :target_model, polymorphic: true, optional: false
|
|
|
|
|
2022-06-13 16:00:41 +02:00
|
|
|
enum target_context: { avis: 'avis', invite: 'invite' }
|
2022-05-23 15:09:22 +02:00
|
|
|
|
|
|
|
def invalid_signed_in_user?(signed_in_user)
|
2022-07-11 13:57:28 +02:00
|
|
|
signed_in_user && signed_in_user.email != target_email
|
2022-05-23 15:09:22 +02:00
|
|
|
end
|
|
|
|
|
2022-06-13 16:00:41 +02:00
|
|
|
def target_email
|
|
|
|
case target_context
|
|
|
|
when 'avis'
|
|
|
|
user.email
|
|
|
|
when 'invite'
|
|
|
|
target_model.user&.email || target_model.email
|
|
|
|
else
|
|
|
|
raise 'invalid target_context'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-23 15:09:22 +02:00
|
|
|
def redirect_url(url_helper)
|
|
|
|
case target_context
|
2022-06-13 16:00:41 +02:00
|
|
|
when "invite"
|
|
|
|
invite = target_model
|
2022-10-17 19:09:51 +02:00
|
|
|
|
|
|
|
fail ActiveRecord::RecordNotFound if invite.nil?
|
|
|
|
|
2022-07-11 13:57:28 +02:00
|
|
|
user = User.find_by(email: target_email)
|
|
|
|
user&.active? ?
|
2022-06-13 16:00:41 +02:00
|
|
|
url_helper.invite_path(invite) :
|
|
|
|
url_helper.invite_path(invite, params: { email: invite.email })
|
2022-05-23 15:09:22 +02:00
|
|
|
when "avis"
|
|
|
|
avis = target_model
|
|
|
|
avis.expert.user.active? ?
|
|
|
|
url_helper.expert_avis_path(avis.procedure, avis) :
|
|
|
|
url_helper.sign_up_expert_avis_path(avis.procedure, avis, email: avis.expert.email)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|