demarches-normaliennes/app/models/piece_justificative.rb

57 lines
1.7 KiB
Ruby
Raw Normal View History

class PieceJustificative < ActiveRecord::Base
belongs_to :dossier
belongs_to :type_de_piece_justificative
has_one :commentaire
belongs_to :user
delegate :api_entreprise, :libelle, :order_place, to: :type_de_piece_justificative
alias_attribute :type, :type_de_piece_justificative_id
mount_uploader :content, PieceJustificativeUploader
2016-11-25 12:07:01 +01:00
validates :content, :file_size => {:maximum => 20.megabytes}
validates :content, presence: true, allow_blank: false, allow_nil: false
2016-12-26 18:37:27 +01:00
after_save :internal_notification, if: Proc.new { !dossier.nil? }
def empty?
content.blank?
end
def content_url
2016-05-18 11:43:32 +02:00
unless content.url.nil?
if Features.remote_storage
(RemoteDownloader.new content.filename).url
else
2016-05-24 18:35:25 +02:00
(LocalDownloader.new content.path,
(type_de_piece_justificative.nil? ? content.original_filename : type_de_piece_justificative.libelle)).url
end
end
end
def self.accept_format
" application/pdf,
application/msword,
application/vnd.openxmlformats-officedocument.wordprocessingml.document,
application/vnd.ms-excel,
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,
application/vnd.ms-powerpoint,
application/vnd.openxmlformats-officedocument.presentationml.presentation,
application/vnd.oasis.opendocument.text,
application/vnd.oasis.opendocument.presentation,
application/vnd.oasis.opendocument.spreadsheet,
image/png,
image/jpeg
"
end
private
def internal_notification
unless self.type_de_piece_justificative.nil? && dossier.state == 'draft'
NotificationService.new('piece_justificative', self.dossier.id, self.libelle).notify
end
end
end