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

128 lines
3.5 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 child_posts = children
  30. def sibling_posts
  31. parent_post_ids = parent_posts.order(:id).pluck(:id)
  32. parent_post_ids.to_h { [_1, PostImplication.where(parent_post: _1).map(&:post)] }
  33. end
  34. def as_json options = { }
  35. super(options).merge(thumbnail: thumbnail.attached? ?
  36. Rails.application.routes.url_helpers.rails_blob_url(
  37. thumbnail, only_path: false) :
  38. nil)
  39. rescue
  40. super(options).merge(thumbnail: nil)
  41. end
  42. def snapshot_tag_names = tags.joins(:tag_name).order('tag_names.name').pluck('tag_names.name')
  43. def snapshot_parent_post_ids = parents.order(:parent_post_id).pluck(:parent_post_id)
  44. def related limit: nil
  45. ids = post_similarities.order(cos: :desc)
  46. ids = ids.limit(limit) if limit
  47. ids = ids.pluck(:target_post_id)
  48. return Post.none if ids.empty?
  49. Post.where(id: ids)
  50. .with_attached_thumbnail
  51. .order(Arel.sql("FIELD(posts.id, #{ ids.join(',') })"))
  52. end
  53. def resized_thumbnail!
  54. return unless thumbnail.attached?
  55. image = MiniMagick::Image.read(thumbnail.download)
  56. image.resize '180x180'
  57. thumbnail.purge
  58. thumbnail.attach(io: File.open(image.path),
  59. filename: 'resized_thumbnail.jpg',
  60. content_type: 'image/jpeg')
  61. end
  62. private
  63. def validate_original_created_range
  64. f = original_created_from
  65. b = original_created_before
  66. return if f.blank? || b.blank?
  67. f = Time.zone.parse(f) if String === f
  68. b = Time.zone.parse(b) if String === b
  69. return if !(f) || !(b)
  70. if f >= b
  71. errors.add :original_created_before, 'オリジナルの作成日時の順番がをかしぃです.'
  72. end
  73. end
  74. def url_must_be_http_url
  75. begin
  76. u = URI.parse(url)
  77. rescue URI::InvalidURIError
  78. errors.add(:url, 'URL が不正です.')
  79. return
  80. end
  81. if !(u in URI::HTTP) || u.host.blank?
  82. errors.add(:url, 'URL が不正です.')
  83. return
  84. end
  85. end
  86. def normalise_url
  87. return if url.blank?
  88. self.url = url.strip
  89. u = URI.parse(url)
  90. return unless u in URI::HTTP
  91. u.host = u.host.downcase if u.host
  92. u.path = u.path.sub(/\/\Z/, '') if u.path.present?
  93. self.url = u.to_s
  94. rescue URI::InvalidURIError
  95. ;
  96. end
  97. end