ぼざクリタグ広場 https://hub.nizika.monster
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

40 lines
1.0 KiB

  1. class Material < ApplicationRecord
  2. include MyDiscard
  3. default_scope -> { kept }
  4. belongs_to :parent, class_name: 'Material', optional: true
  5. has_many :children, class_name: 'Material', foreign_key: :parent_id, dependent: :nullify
  6. belongs_to :tag, optional: true
  7. belongs_to :created_by_user, class_name: 'User', optional: true
  8. belongs_to :updated_by_user, class_name: 'User', optional: true
  9. has_one_attached :file, dependent: :purge
  10. validates :tag_id, presence: true, uniqueness: true
  11. validate :file_must_be_attached
  12. validate :tag_must_be_material_category
  13. def content_type
  14. return nil unless file&.attached?
  15. file.blob.content_type
  16. end
  17. private
  18. def file_must_be_attached
  19. return if url.present? || file.attached?
  20. errors.add(:url, 'URL かファイルのどちらかは必須です.')
  21. end
  22. def tag_must_be_material_category
  23. return if tag.blank? || tag.character? || tag.material?
  24. errors.add(:tag, '素材カテゴリのタグを指定してください.')
  25. end
  26. end