2015-09-21 17:59:03 +02:00
|
|
|
class Procedure < ActiveRecord::Base
|
2016-01-18 16:20:51 +01:00
|
|
|
has_many :types_de_piece_justificative, dependent: :destroy
|
|
|
|
has_many :types_de_champ, dependent: :destroy
|
|
|
|
has_many :dossiers, dependent: :destroy
|
|
|
|
|
|
|
|
has_one :module_api_carto, dependent: :destroy
|
|
|
|
|
|
|
|
belongs_to :administrateur
|
|
|
|
|
2016-05-20 15:39:17 +02:00
|
|
|
has_many :assign_to
|
|
|
|
has_many :gestionnaires, through: :assign_to
|
|
|
|
|
2016-01-25 15:54:21 +01:00
|
|
|
delegate :use_api_carto, to: :module_api_carto
|
|
|
|
|
2015-11-16 16:16:08 +01:00
|
|
|
accepts_nested_attributes_for :types_de_champ,:reject_if => proc { |attributes| attributes['libelle'].blank? }, :allow_destroy => true
|
2015-11-20 13:54:08 +01:00
|
|
|
accepts_nested_attributes_for :types_de_piece_justificative, :reject_if => proc { |attributes| attributes['libelle'].blank? }, :allow_destroy => true
|
2015-12-08 10:11:58 +01:00
|
|
|
accepts_nested_attributes_for :module_api_carto
|
2015-11-10 10:23:15 +01:00
|
|
|
|
2015-12-10 17:13:39 +01:00
|
|
|
mount_uploader :logo, ProcedureLogoUploader
|
|
|
|
|
2015-09-22 11:21:52 +02:00
|
|
|
validates :libelle, presence: true, allow_blank: false, allow_nil: false
|
|
|
|
validates :description, presence: true, allow_blank: false, allow_nil: false
|
2015-11-17 15:30:03 +01:00
|
|
|
|
|
|
|
def types_de_champ_ordered
|
|
|
|
types_de_champ.order(:order_place)
|
|
|
|
end
|
2015-11-19 11:37:01 +01:00
|
|
|
|
2016-06-08 16:45:18 +02:00
|
|
|
def types_de_piece_justificative_ordered
|
|
|
|
types_de_piece_justificative.order(:order_place)
|
|
|
|
end
|
|
|
|
|
2016-05-26 15:59:50 +02:00
|
|
|
def self.not_archived id
|
|
|
|
Procedure.where(archived: false).find(id)
|
|
|
|
end
|
|
|
|
|
2015-11-19 11:37:01 +01:00
|
|
|
def switch_types_de_champ index_of_first_element
|
2016-06-08 16:45:18 +02:00
|
|
|
switch_list_order(types_de_champ_ordered, index_of_first_element)
|
|
|
|
end
|
|
|
|
|
|
|
|
def switch_types_de_piece_justificative index_of_first_element
|
|
|
|
switch_list_order(types_de_piece_justificative_ordered, index_of_first_element)
|
|
|
|
end
|
|
|
|
|
|
|
|
def switch_list_order(list, index_of_first_element)
|
2015-11-19 11:37:01 +01:00
|
|
|
return false if index_of_first_element < 0
|
2016-06-08 16:45:18 +02:00
|
|
|
return false if index_of_first_element == list.count - 1
|
|
|
|
return false if list.count < 1
|
|
|
|
list[index_of_first_element].update_attributes(order_place: index_of_first_element + 1)
|
|
|
|
list[index_of_first_element + 1].update_attributes(order_place: index_of_first_element)
|
2015-11-19 11:37:01 +01:00
|
|
|
true
|
|
|
|
end
|
2015-12-21 14:40:28 +01:00
|
|
|
|
|
|
|
def locked?
|
|
|
|
dossiers.where.not(state: :draft).count > 0
|
|
|
|
end
|
2015-09-21 17:59:03 +02:00
|
|
|
end
|