ぼざクリタグ広場 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.
 
 
 
 
 
 

103 lines
2.8 KiB

  1. class Post < ApplicationRecord
  2. require 'mini_magick'
  3. belongs_to :parent, class_name: 'Post', optional: true, foreign_key: 'parent_id'
  4. belongs_to :uploaded_user, class_name: 'User', optional: true
  5. has_many :post_tags, dependent: :destroy, inverse_of: :post
  6. has_many :active_post_tags, -> { kept }, class_name: 'PostTag', inverse_of: :post
  7. has_many :post_tags_with_discarded, -> { with_discarded }, class_name: 'PostTag'
  8. has_many :tags, through: :active_post_tags
  9. has_many :user_post_views, dependent: :delete_all
  10. has_many :post_similarities, dependent: :delete_all
  11. has_many :post_versions
  12. has_one_attached :thumbnail
  13. before_validation :normalise_url
  14. validates :url, presence: true, uniqueness: true
  15. validate :validate_original_created_range
  16. validate :url_must_be_http_url
  17. def as_json options = { }
  18. super(options).merge({ thumbnail: thumbnail.attached? ?
  19. Rails.application.routes.url_helpers.rails_blob_url(
  20. thumbnail, only_path: false) :
  21. nil })
  22. rescue
  23. super(options).merge(thumbnail: nil)
  24. end
  25. def snapshot_tag_names = tags.joins(:tag_name).order('tag_names.name').pluck('tag_names.name')
  26. def related limit: nil
  27. ids = post_similarities.order(cos: :desc)
  28. ids = ids.limit(limit) if limit
  29. ids = ids.pluck(:target_post_id)
  30. return Post.none if ids.empty?
  31. Post.where(id: ids)
  32. .with_attached_thumbnail
  33. .order(Arel.sql("FIELD(posts.id, #{ ids.join(',') })"))
  34. end
  35. def resized_thumbnail!
  36. return unless thumbnail.attached?
  37. image = MiniMagick::Image.read(thumbnail.download)
  38. image.resize '180x180'
  39. thumbnail.purge
  40. thumbnail.attach(io: File.open(image.path),
  41. filename: 'resized_thumbnail.jpg',
  42. content_type: 'image/jpeg')
  43. end
  44. private
  45. def validate_original_created_range
  46. f = original_created_from
  47. b = original_created_before
  48. return if f.blank? || b.blank?
  49. f = Time.zone.parse(f) if String === f
  50. b = Time.zone.parse(b) if String === b
  51. return if !(f) || !(b)
  52. if f >= b
  53. errors.add :original_created_before, 'オリジナルの作成日時の順番がをかしぃです.'
  54. end
  55. end
  56. def url_must_be_http_url
  57. begin
  58. u = URI.parse(url)
  59. rescue URI::InvalidURIError
  60. errors.add(:url, 'URL が不正です.')
  61. return
  62. end
  63. if !(u in URI::HTTP) || u.host.blank?
  64. errors.add(:url, 'URL が不正です.')
  65. return
  66. end
  67. end
  68. def normalise_url
  69. return if url.blank?
  70. self.url = url.strip
  71. u = URI.parse(url)
  72. return unless u in URI::HTTP
  73. u.host = u.host.downcase if u.host
  74. u.path = u.path.sub(/\/\Z/, '') if u.path.present?
  75. self.url = u.to_s
  76. rescue URI::InvalidURIError
  77. ;
  78. end
  79. end