7b15cb2c5a
#99 #99 #99 #99 #99 #99 #99 #99 #99 #99 Co-authored-by: miteruzo <miteruzo@naver.com> Reviewed-on: #303
40 lines
1.0 KiB
Ruby
40 lines
1.0 KiB
Ruby
class Material < ApplicationRecord
|
|
include MyDiscard
|
|
|
|
default_scope -> { kept }
|
|
|
|
belongs_to :parent, class_name: 'Material', optional: true
|
|
has_many :children, class_name: 'Material', foreign_key: :parent_id, dependent: :nullify
|
|
|
|
belongs_to :tag, optional: true
|
|
belongs_to :created_by_user, class_name: 'User', optional: true
|
|
belongs_to :updated_by_user, class_name: 'User', optional: true
|
|
|
|
has_one_attached :file, dependent: :purge
|
|
|
|
validates :tag_id, presence: true, uniqueness: true
|
|
|
|
validate :file_must_be_attached
|
|
validate :tag_must_be_material_category
|
|
|
|
def content_type
|
|
return nil unless file&.attached?
|
|
|
|
file.blob.content_type
|
|
end
|
|
|
|
private
|
|
|
|
def file_must_be_attached
|
|
return if url.present? || file.attached?
|
|
|
|
errors.add(:url, 'URL かファイルのどちらかは必須です.')
|
|
end
|
|
|
|
def tag_must_be_material_category
|
|
return if tag.blank? || tag.character? || tag.material?
|
|
|
|
errors.add(:tag, '素材カテゴリのタグを指定してください.')
|
|
end
|
|
end
|