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

120 lines
3.4 KiB

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