Clean up and simplify magic links
This commit is contained in:
+27
-6
@@ -1,11 +1,32 @@
|
||||
class Identity < UntenantedRecord
|
||||
# This is used to instantiate an Identity-like object from the `identity_token` without hitting
|
||||
# the untenanted database. It is intended to be used with caching/etagging methods.
|
||||
Mock = Struct.new(:id, :updated_at) do
|
||||
def identity
|
||||
Identity.find_signed(id)
|
||||
has_many :memberships, dependent: :destroy
|
||||
has_many :magic_links, dependent: :delete_all
|
||||
|
||||
normalizes :email_address, with: ->(value) { value.strip.downcase }
|
||||
|
||||
class << self
|
||||
def link(email_address:, to:)
|
||||
find_or_create_by!(email_address: email_address).tap { |identity| identity.link_to(to) }
|
||||
end
|
||||
|
||||
def unlink(email_address:, from:)
|
||||
find_by(email_address: email_address)&.unlink_from(from)
|
||||
end
|
||||
end
|
||||
|
||||
has_many :memberships, dependent: :destroy
|
||||
def send_magic_link
|
||||
magic_links.create!.tap do |magic_link|
|
||||
MagicLinkMailer.sign_in_instructions(magic_link).deliver_later
|
||||
end
|
||||
end
|
||||
|
||||
def link_to(tenant)
|
||||
memberships.find_or_create_by!(tenant: tenant) do |membership|
|
||||
membership.account_name = ApplicationRecord.with_tenant(membership.tenant) { Account.sole.name }
|
||||
end
|
||||
end
|
||||
|
||||
def unlink_from(tenant)
|
||||
memberships.find_by(tenant: tenant)&.destroy
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user