module Contractable
  extend ActiveSupport::Concern
  
  included do
    has_one_attached :contract

    validates :contract, content_type: ["application/pdf"]

    scope :having_contract_attached,  -> (release_ids) { left_joins(:contract_attachment).where(active_storage_attachments: { record_id: release_ids }).group(:id).having("COUNT(active_storage_attachments) > 0") }

    def contract_file_name
      "#{project.name.parameterize}_#{contract_template.release_type}_#{(signed_at || created_at).strftime("%Y.%m.%d")}_#{release_number}_#{name.parameterize}"
    end
  end
end
