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

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