2015-09-22 10:15:12 +02:00
|
|
|
class Gestionnaire < ActiveRecord::Base
|
|
|
|
devise :database_authenticatable, :registerable,
|
|
|
|
:recoverable, :rememberable, :trackable, :validatable
|
2015-11-10 10:23:15 +01:00
|
|
|
|
2016-05-20 15:39:17 +02:00
|
|
|
has_and_belongs_to_many :administrateurs
|
2015-11-16 18:08:19 +01:00
|
|
|
|
2016-06-20 17:37:04 +02:00
|
|
|
has_many :assign_to, dependent: :destroy
|
2016-05-20 15:39:17 +02:00
|
|
|
has_many :procedures, through: :assign_to
|
2015-11-16 18:08:19 +01:00
|
|
|
has_many :dossiers, through: :procedures
|
2016-07-18 18:24:29 +02:00
|
|
|
has_many :follows
|
2016-03-14 16:49:12 +01:00
|
|
|
|
|
|
|
def dossiers_filter
|
|
|
|
dossiers.where(procedure_id: procedure_filter_list)
|
|
|
|
end
|
|
|
|
|
2016-07-18 18:24:29 +02:00
|
|
|
def dossiers_follow
|
|
|
|
dossiers.joins(:follows).where("follows.gestionnaire_id = #{id}")
|
|
|
|
end
|
|
|
|
|
2016-03-14 16:49:12 +01:00
|
|
|
def procedure_filter_list
|
|
|
|
procedure_filter.empty? ? procedures.pluck(:id) : procedure_filter
|
|
|
|
end
|
2016-07-18 18:24:29 +02:00
|
|
|
|
|
|
|
def toggle_follow_dossier dossier_id
|
|
|
|
dossier = dossier_id
|
|
|
|
dossier = Dossier.find(dossier_id) unless dossier_id.class == Dossier
|
|
|
|
|
|
|
|
Follow.create!(dossier: dossier, gestionnaire: self)
|
|
|
|
rescue ActiveRecord::RecordInvalid
|
|
|
|
Follow.where(dossier: dossier, gestionnaire: self).delete_all
|
|
|
|
rescue ActiveRecord::RecordNotFound
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def follow? dossier_id
|
|
|
|
dossier_id = dossier_id.id if dossier_id.class == Dossier
|
|
|
|
|
|
|
|
Follow.where(gestionnaire_id: id, dossier_id: dossier_id).any?
|
|
|
|
end
|
2015-09-22 10:15:12 +02:00
|
|
|
end
|