# encoding: utf-8

class DocumentUploader < CarrierWave::Uploader::Base

  include Rails.application.routes.url_helpers


  def filename
    "#{friendly_filename(model.title)}_#{friendly_filename(model.accepted_offer.customer.organisation)}_besoin#{model.accepted_offer.offer.need.id}_offre#{model.accepted_offer.offer.id}_#{mounted_as}.#{file.extension}"  if original_filename.present?
  end

  def store_dir
    "#{Rails.root}/private_medias/documents/#{model.id}/#{mounted_as}"
  end

  def extension_white_list
    %w(pdf)
  end

  def url
    #download_admin_offer_accepted_offer_path(model.offer, model)
  end

  def friendly_filename(filename)
    filename.gsub(/[^\w\s_-]+/, '')
            .gsub(/(^|\b\s)\s+($|\s?\b)/, '\\1\\2')
            .gsub(/\s+/, '_')
  end

end
