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-10-11 15:37:16 +02:00
|
|
|
has_one :preference_smart_listing_page, dependent: :destroy
|
|
|
|
|
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
|
2016-11-24 19:33:34 +01:00
|
|
|
has_many :dossiers, -> { where.not(state: :draft) }, through: :procedures
|
2016-07-18 18:24:29 +02:00
|
|
|
has_many :follows
|
2016-08-01 18:10:32 +02:00
|
|
|
has_many :preference_list_dossiers
|
2016-03-14 16:49:12 +01:00
|
|
|
|
2016-08-02 14:49:41 +02:00
|
|
|
after_create :build_default_preferences_list_dossier
|
2016-10-11 15:37:16 +02:00
|
|
|
after_create :build_default_preferences_smart_listing_page
|
2016-10-18 15:49:04 +02:00
|
|
|
after_update :sync_credentials, if: -> { Features.unified_login }
|
2016-08-02 14:49:41 +02:00
|
|
|
|
2016-07-18 18:24:29 +02:00
|
|
|
def dossiers_follow
|
2016-11-24 19:33:34 +01:00
|
|
|
@dossiers_follow ||= dossiers.joins(:follows).where("follows.gestionnaire_id = #{id}")
|
2016-07-18 18:24:29 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
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
|
2016-08-02 14:49:41 +02:00
|
|
|
|
2016-10-07 12:10:58 +02:00
|
|
|
def build_default_preferences_list_dossier procedure_id=nil
|
2016-08-02 14:49:41 +02:00
|
|
|
|
2016-10-07 12:10:58 +02:00
|
|
|
PreferenceListDossier.available_columns_for(procedure_id).each do |table|
|
2016-08-02 14:49:41 +02:00
|
|
|
table.second.each do |column|
|
|
|
|
|
|
|
|
if valid_couple_table_attr? table.first, column.first
|
|
|
|
PreferenceListDossier.create(
|
|
|
|
libelle: column.second[:libelle],
|
|
|
|
table: column.second[:table],
|
|
|
|
attr: column.second[:attr],
|
|
|
|
attr_decorate: column.second[:attr_decorate],
|
|
|
|
bootstrap_lg: column.second[:bootstrap_lg],
|
|
|
|
order: nil,
|
|
|
|
filter: nil,
|
2016-10-07 12:10:58 +02:00
|
|
|
procedure_id: procedure_id,
|
2016-08-02 14:49:41 +02:00
|
|
|
gestionnaire: self
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-10-11 15:37:16 +02:00
|
|
|
def build_default_preferences_smart_listing_page
|
|
|
|
PreferenceSmartListingPage.create(page: 1, procedure: nil, gestionnaire: self, liste: 'a_traiter')
|
|
|
|
end
|
|
|
|
|
2016-08-02 14:49:41 +02:00
|
|
|
private
|
|
|
|
|
|
|
|
def valid_couple_table_attr? table, column
|
|
|
|
couples = [{
|
|
|
|
table: :dossier,
|
|
|
|
column: :dossier_id
|
|
|
|
}, {
|
|
|
|
table: :procedure,
|
|
|
|
column: :libelle
|
|
|
|
}, {
|
|
|
|
table: :etablissement,
|
|
|
|
column: :siret
|
|
|
|
}, {
|
|
|
|
table: :entreprise,
|
|
|
|
column: :raison_sociale
|
|
|
|
}, {
|
|
|
|
table: :dossier,
|
|
|
|
column: :state
|
|
|
|
}]
|
|
|
|
|
|
|
|
couples.include?({table: table, column: column})
|
|
|
|
end
|
2016-10-11 10:31:32 +02:00
|
|
|
|
|
|
|
def sync_credentials
|
|
|
|
if email_changed? || encrypted_password_changed?
|
|
|
|
user = User.find_by(email: email_was)
|
|
|
|
if user
|
|
|
|
return user.update_columns(
|
|
|
|
email: email,
|
|
|
|
encrypted_password: encrypted_password)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
true
|
|
|
|
end
|
2015-09-22 10:15:12 +02:00
|
|
|
end
|