file attachments in db
This commit is contained in:
parent
dc8d274487
commit
3dc1db8836
5 changed files with 52 additions and 27 deletions
21
app/models/attachment.rb
Normal file
21
app/models/attachment.rb
Normal file
|
@ -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
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
12
db/migrate/20170122201451_create_attachments.rb
Normal file
12
db/migrate/20170122201451_create_attachments.rb
Normal file
|
@ -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
|
26
db/schema.rb
26
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
|
||||
|
|
Loading…
Add table
Reference in a new issue