diff --git a/app/models/terms_of_service_file.rb b/app/models/terms_of_service_file.rb index 2474a0bed0..b19b9ea84d 100644 --- a/app/models/terms_of_service_file.rb +++ b/app/models/terms_of_service_file.rb @@ -1,6 +1,10 @@ # frozen_string_literal: true class TermsOfServiceFile < ApplicationRecord + has_attached_file :attachment + + validates :attachment, presence: true + # The most recently uploaded file is the current one. def self.current order(:id).last diff --git a/db/migrate/20210415052410_add_attachment_to_terms_of_service_file.rb b/db/migrate/20210415052410_add_attachment_to_terms_of_service_file.rb new file mode 100644 index 0000000000..3e0feb8c9f --- /dev/null +++ b/db/migrate/20210415052410_add_attachment_to_terms_of_service_file.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +class AddAttachmentToTermsOfServiceFile < ActiveRecord::Migration[5.0] + def up + add_attachment :terms_of_service_files, :attachment + end + + def down + remove_attachment :terms_of_service_files, :attachment + end +end diff --git a/db/schema.rb b/db/schema.rb index 9720236c7f..4f4b5abf88 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2021_04_15_015550) do +ActiveRecord::Schema.define(version: 2021_04_15_052410) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -1146,8 +1146,12 @@ ActiveRecord::Schema.define(version: 2021_04_15_015550) do end create_table "terms_of_service_files", force: :cascade do |t| - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.string "attachment_file_name" + t.string "attachment_content_type" + t.integer "attachment_file_size" + t.datetime "attachment_updated_at" end create_table "variant_overrides", force: :cascade do |t| diff --git a/spec/models/terms_of_service_file_spec.rb b/spec/models/terms_of_service_file_spec.rb index 04434269f7..f66322f107 100644 --- a/spec/models/terms_of_service_file_spec.rb +++ b/spec/models/terms_of_service_file_spec.rb @@ -3,6 +3,8 @@ require 'rails_helper' describe TermsOfServiceFile do + let(:pdf) { File.open(Rails.root.join("public/Terms-of-service.pdf")) } + describe ".current" do it "returns nil" do expect(TermsOfServiceFile.current).to be_nil @@ -10,8 +12,8 @@ describe TermsOfServiceFile do it "returns the last one" do existing = [ - TermsOfServiceFile.create!, - TermsOfServiceFile.create!, + TermsOfServiceFile.create!(attachment: pdf), + TermsOfServiceFile.create!(attachment: pdf), ] expect(TermsOfServiceFile.current).to eq existing.last