demarches-normaliennes/app/models/user.rb

49 lines
1.3 KiB
Ruby
Raw Normal View History

2018-03-06 13:44:29 +01:00
class User < ApplicationRecord
include CredentialsSyncableConcern
include EmailSanitizableConcern
enum loged_in_with_france_connect: {
particulier: 'particulier',
entreprise: 'entreprise'
}
2015-09-23 10:02:01 +02:00
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
2018-05-26 00:06:40 +02:00
devise :database_authenticatable, :registerable, :async,
:recoverable, :rememberable, :trackable, :validatable, :confirmable, :lockable
2015-09-23 12:16:21 +02:00
has_many :dossiers, dependent: :destroy
has_many :invites, dependent: :destroy
has_many :dossiers_invites, through: :invites, source: :dossier
has_many :piece_justificative, dependent: :destroy
2018-08-08 17:37:41 +02:00
has_many :feedbacks, dependent: :destroy
has_one :france_connect_information, dependent: :destroy
accepts_nested_attributes_for :france_connect_information
2017-02-07 16:56:21 +01:00
before_validation -> { sanitize_email(:email) }
# Callback provided by Devise
def after_confirmation
link_invites!
end
2018-05-30 18:26:23 +02:00
def owns?(dossier)
dossier.user_id == id
end
def invite?(dossier_id)
invites.pluck(:dossier_id).include?(dossier_id.to_i)
end
def owns_or_invite?(dossier)
owns?(dossier) || invite?(dossier.id)
end
private
def link_invites!
Invite.where(email: email).update_all(user_id: id)
end
2015-09-23 10:02:01 +02:00
end