demarches-normaliennes/app/models/champ.rb

64 lines
1.6 KiB
Ruby
Raw Normal View History

class Champ < ActiveRecord::Base
2015-11-03 10:48:40 +01:00
belongs_to :dossier
2015-11-05 11:21:44 +01:00
belongs_to :type_de_champ
2016-11-14 18:00:26 +01:00
has_many :commentaires
2016-08-09 16:21:39 +02:00
delegate :libelle, :type_champ, :order_place, :mandatory, :description, :drop_down_list, to: :type_de_champ
2016-12-26 18:37:27 +01:00
after_save :internal_notification, if: Proc.new { !dossier.nil? }
2016-12-26 11:47:51 +01:00
def mandatory?
mandatory
end
def data_provide
2016-10-10 16:17:17 +02:00
return 'datepicker' if (type_champ == 'datetime' || type_champ == 'date') && !(BROWSER.value.chrome? || BROWSER.value.edge?)
return 'typeahead' if type_champ == 'address'
end
def data_date_format
('dd/mm/yyyy' if type_champ == 'datetime' || type_champ == 'date')
end
def same_hour? num
same_date? num, '%H'
end
def same_minute? num
same_date? num, '%M'
end
2017-03-29 13:37:07 +02:00
def mandatory_and_blank?
mandatory? && value.blank?
end
def same_date? num, compare
if type_champ == 'datetime' && !value.nil?
if value.to_datetime.strftime(compare) == num
return true
end
end
false
end
def self.regions
2016-12-26 11:47:51 +01:00
JSON.parse(Carto::GeoAPI::Driver.regions).sort_by { |e| e['nom'] }.inject([]) { |acc, liste| acc.push(liste['nom']) }
end
def self.departements
2016-12-26 11:47:51 +01:00
JSON.parse(Carto::GeoAPI::Driver.departements).inject([]) { |acc, liste| acc.push(liste['code'] + ' - ' + liste['nom']) }.push('99 - Étranger')
end
def self.pays
2016-12-26 11:47:51 +01:00
JSON.parse(Carto::GeoAPI::Driver.pays).inject([]) { |acc, liste| acc.push(liste['nom']) }
end
private
def internal_notification
unless dossier.state == 'draft'
NotificationService.new('champs', self.dossier.id, self.libelle).notify
end
end
2015-11-03 10:48:40 +01:00
end