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

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