Commit 3de5f562 by ivan Lan

Add Media::Obj

parent defb46ec
# == Schema Information
#
# Table name: media_objs
#
# id :integer not null, primary key
# file_file_name :string(255)
# file_content_type :string(255)
# file_file_size :integer
# file_updated_at :datetime
# type :string(255)
# creator_type :string(255)
# creator_id :integer
# created_at :datetime not null
# updated_at :datetime not null
#
# Indexes
#
# index_media_objs_on_creator_type_and_creator_id (creator_type,creator_id)
#
module Whaleback
module Media
class Obj < ApplicationRecord
belongs_to :creator, polymorphic: true, optional: true
has_many :attachments
has_attached_file :file
validates_attachment_presence :file
before_destroy :stop_destroying, unless: :useless
TypeTrans = {
'' => 'Media::Obj',
'image' => 'Media::Image',
'document' => 'Media::Document',
'video' => 'Media::Video',
'voice' => 'Media::Voice',
}
attr_accessor :default_mode
def url mode=nil
ActionController::Base.helpers.asset_url(
mode || default_mode ? file.url(mode) : file
)
end
def name
file_file_name
end
def size
file_file_size
end
def media_type
TypeTrans.invert[type]
end
def media_type= val
self.type = TypeTrans[val]
end
def useless
attachments.empty?
end
def stop_destroying
raise Whaleback::Errors::BaseError.new('This media obj being used.', -1, 400)
end
require_relative 'obj/document'
require_relative 'obj/image'
require_relative 'obj/video'
require_relative 'obj/voice'
end
end
end
# == Schema Information
#
# Table name: media_objs
#
# id :integer not null, primary key
# file_file_name :string(255)
# file_content_type :string(255)
# file_file_size :integer
# file_updated_at :datetime
# type :string(255)
# creator_type :string(255)
# creator_id :integer
# created_at :datetime not null
# updated_at :datetime not null
#
# Indexes
#
# index_media_objs_on_creator_type_and_creator_id (creator_type,creator_id)
#
module Whaleback
module Media
class Obj
class Document < Obj
validates_attachment_size :file, less_than: 500.megabytes
validates_attachment_content_type :file,
content_type: [
"application/pdf",
"application/vnd.ms-excel",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"application/msword",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"application/vnd.ms-powerpoint",
"application/vnd.openxmlformats-officedocument.presentationml.presentation",
"text/plain",
"application/zip",
"application/json"
]
end
end
end
end
# == Schema Information
#
# Table name: media_objs
#
# id :integer not null, primary key
# file_file_name :string(255)
# file_content_type :string(255)
# file_file_size :integer
# file_updated_at :datetime
# type :string(255)
# creator_type :string(255)
# creator_id :integer
# created_at :datetime not null
# updated_at :datetime not null
#
# Indexes
#
# index_media_objs_on_creator_type_and_creator_id (creator_type,creator_id)
#
module Whaleback
module Media
class Obj
class Image < Obj
has_attached_file :file, styles: {
thumb: ["32x32#", :png],
medium: '300x300>',
product: '600x600>'
}
validates_attachment_size :file, less_than: 5.megabytes
validates_attachment_content_type :file, content_type: /image\/.*\Z/
def default_mode
:original
end
end
end
end
end
# == Schema Information
#
# Table name: media_objs
#
# id :integer not null, primary key
# file_file_name :string(255)
# file_content_type :string(255)
# file_file_size :integer
# file_updated_at :datetime
# type :string(255)
# creator_type :string(255)
# creator_id :integer
# created_at :datetime not null
# updated_at :datetime not null
#
# Indexes
#
# index_media_objs_on_creator_type_and_creator_id (creator_type,creator_id)
#
module Whaleback
module Media
class Obj
class Video < Obj
has_attached_file :file,
styles: { thumb: ["400x400#", :png] },
processors: [:transcoder]
validates_attachment_size :file, less_than: 1024.megabytes
validates_attachment_content_type :file, content_type: /^video\/.*$/
end
end
end
end
# == Schema Information
#
# Table name: media_objs
#
# id :integer not null, primary key
# file_file_name :string(255)
# file_content_type :string(255)
# file_file_size :integer
# file_updated_at :datetime
# type :string(255)
# creator_type :string(255)
# creator_id :integer
# created_at :datetime not null
# updated_at :datetime not null
#
# Indexes
#
# index_media_objs_on_creator_type_and_creator_id (creator_type,creator_id)
#
module Whaleback
module Media
class Obj
class Voice < Obj
# has_attached_file :file
validates_attachment_size :file, less_than: 1024.megabytes
validates_attachment_content_type :file, content_type: /^audio\/.*$/
end
end
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment