From 3dc1db883677c8c72cfc090979d08ac9a54e3c8d Mon Sep 17 00:00:00 2001 From: Devin Howard Date: Sun, 22 Jan 2017 15:23:24 -0500 Subject: [PATCH] file attachments in db --- app/models/attachment.rb | 21 +++++++++++++++ app/models/topic.rb | 17 +----------- config/environments/development.rb | 3 +++ .../20170122201451_create_attachments.rb | 12 +++++++++ db/schema.rb | 26 +++++++++++-------- 5 files changed, 52 insertions(+), 27 deletions(-) create mode 100644 app/models/attachment.rb create mode 100644 db/migrate/20170122201451_create_attachments.rb diff --git a/app/models/attachment.rb b/app/models/attachment.rb new file mode 100644 index 00000000..9a1f93df --- /dev/null +++ b/app/models/attachment.rb @@ -0,0 +1,21 @@ +class Attachment < ApplicationRecord + belongs_to :attachable, polymorphic: true + + has_attached_file :file, + styles: lambda { |a| + a.instance.is_image? ? { + small: 'x200>', + medium: 'x300>', + large: 'x400>' + } : {} + } + + validates_attachment_content_type :file, :content_type => [ + /\Aimage\/.*\Z/, + /\Avideo\/.*\Z/ + ] + + def is_image? + file.instance.file_content_type =~ %r(image) + end +end diff --git a/app/models/topic.rb b/app/models/topic.rb index 90443862..56cc0acb 100644 --- a/app/models/topic.rb +++ b/app/models/topic.rb @@ -21,22 +21,7 @@ class Topic < ApplicationRecord validates :permission, presence: true validates :permission, inclusion: { in: Perm::ISSIONS.map(&:to_s) } - # This method associates the attribute ":image" with a file attachment - has_attached_file :image - - # , styles: { - # thumb: '100x100>', - # square: '200x200#', - # medium: '300x300>' - # } - - # Validate the attached image is image/jpg, image/png, etc - validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/ - - # This method associates the attribute ":image" with a file attachment - has_attached_file :audio - # Validate the attached audio is audio/wav, audio/mp3, etc - validates_attachment_content_type :audio, content_type: /\Aaudio\/.*\Z/ + has_many :attachments, as: :attachable, dependent: :destroy def synapses synapses1.or(synapses2) diff --git a/config/environments/development.rb b/config/environments/development.rb index 8fef2145..30160444 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -29,4 +29,7 @@ Rails.application.configure do # Expands the lines which load the assets config.assets.debug = false config.assets.quiet = true + + # S3 file storage + config.paperclip_defaults = {} # store on local machine for dev end diff --git a/db/migrate/20170122201451_create_attachments.rb b/db/migrate/20170122201451_create_attachments.rb new file mode 100644 index 00000000..a798fcfd --- /dev/null +++ b/db/migrate/20170122201451_create_attachments.rb @@ -0,0 +1,12 @@ +class CreateAttachments < ActiveRecord::Migration[5.0] + def change + create_table :attachments do |t| + t.references :attachable, polymorphic: true + t.attachment :file + t.timestamps + end + + remove_attachment :topics, :image + remove_attachment :topics, :audio + end +end diff --git a/db/schema.rb b/db/schema.rb index 7d146be5..a14b6715 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: 20161218183817) do +ActiveRecord::Schema.define(version: 20170122201451) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -26,6 +26,18 @@ ActiveRecord::Schema.define(version: 20161218183817) do t.index ["user_id"], name: "index_access_requests_on_user_id", using: :btree end + create_table "attachments", force: :cascade do |t| + t.string "attachable_type" + t.integer "attachable_id" + t.string "file_file_name" + t.string "file_content_type" + t.integer "file_file_size" + t.datetime "file_updated_at" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["attachable_type", "attachable_id"], name: "index_attachments_on_attachable_type_and_attachable_id", using: :btree + end + create_table "delayed_jobs", force: :cascade do |t| t.integer "priority", default: 0, null: false t.integer "attempts", default: 0, null: false @@ -269,17 +281,9 @@ ActiveRecord::Schema.define(version: 20161218183817) do t.text "link" t.integer "user_id" t.integer "metacode_id" - 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.text "permission" - t.string "image_file_name", limit: 255 - t.string "image_content_type", limit: 255 - t.integer "image_file_size" - t.datetime "image_updated_at" - t.string "audio_file_name", limit: 255 - t.string "audio_content_type", limit: 255 - t.integer "audio_file_size" - t.datetime "audio_updated_at" t.integer "defer_to_map_id" t.index ["metacode_id"], name: "index_topics_on_metacode_id", using: :btree t.index ["user_id"], name: "index_topics_on_user_id", using: :btree