demarches-normaliennes/app/models/piece_justificative.rb

57 lines
1.7 KiB
Ruby
Raw Normal View History

2018-03-06 13:44:29 +01:00
class PieceJustificative < ApplicationRecord
belongs_to :dossier, touch: true
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
validates :content, :file_size => { :maximum => 20.megabytes }
validates :content, presence: true, allow_blank: false, allow_nil: false
scope :updated_since?, -> (date) { where('pieces_justificatives.updated_at > ?', date) }
def empty?
content.blank?
end
2017-01-03 15:12:49 +01:00
def libelle
if type_de_piece_justificative.nil?
return content.to_s
else
type_de_piece_justificative.libelle
end
end
def content_url
2018-01-11 19:04:39 +01:00
if content.url.present?
2018-04-18 12:24:37 +02:00
if Flipflop.remote_storage?
2016-05-18 11:43:32 +02:00
(RemoteDownloader.new content.filename).url
else
2016-05-24 18:35:25 +02:00
(LocalDownloader.new content.path,
2017-06-12 13:49:51 +02:00
(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
end