Merge remote-tracking branch 'origin/main' into feature/047
This commit is contained in:
@@ -12,6 +12,20 @@ class Post < ApplicationRecord
|
||||
has_many :post_similarities, dependent: :delete_all
|
||||
has_many :post_versions
|
||||
|
||||
has_many :parent_post_implications,
|
||||
class_name: 'PostImplication',
|
||||
foreign_key: :post_id,
|
||||
dependent: :destroy,
|
||||
inverse_of: :post
|
||||
has_many :parents, through: :parent_post_implications, source: :parent_post
|
||||
|
||||
has_many :child_post_implications,
|
||||
class_name: 'PostImplication',
|
||||
foreign_key: :parent_post_id,
|
||||
dependent: :destroy,
|
||||
inverse_of: :parent_post
|
||||
has_many :children, through: :child_post_implications, source: :post
|
||||
|
||||
has_one_attached :thumbnail
|
||||
|
||||
before_validation :normalise_url
|
||||
@@ -21,17 +35,29 @@ class Post < ApplicationRecord
|
||||
validate :validate_original_created_range
|
||||
validate :url_must_be_http_url
|
||||
|
||||
def parent_posts = parents
|
||||
|
||||
def child_posts = children
|
||||
|
||||
def sibling_posts
|
||||
parent_post_ids = parent_posts.order(:id).pluck(:id)
|
||||
|
||||
parent_post_ids.to_h { [_1, PostImplication.where(parent_post: _1).map(&:post)] }
|
||||
end
|
||||
|
||||
def as_json options = { }
|
||||
super(options).merge({ thumbnail: thumbnail.attached? ?
|
||||
Rails.application.routes.url_helpers.rails_blob_url(
|
||||
thumbnail, only_path: false) :
|
||||
nil })
|
||||
super(options).merge(thumbnail: thumbnail.attached? ?
|
||||
Rails.application.routes.url_helpers.rails_blob_url(
|
||||
thumbnail, only_path: false) :
|
||||
nil)
|
||||
rescue
|
||||
super(options).merge(thumbnail: nil)
|
||||
end
|
||||
|
||||
def snapshot_tag_names = tags.joins(:tag_name).order('tag_names.name').pluck('tag_names.name')
|
||||
|
||||
def snapshot_parent_post_ids = parents.order(:id).pluck(:id)
|
||||
|
||||
def related limit: nil
|
||||
ids = post_similarities.order(cos: :desc)
|
||||
ids = ids.limit(limit) if limit
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
class PostImplication < ApplicationRecord
|
||||
self.primary_key = :post_id, :parent_post_id
|
||||
|
||||
belongs_to :post, inverse_of: :parent_post_implications
|
||||
belongs_to :parent_post, class_name: 'Post', inverse_of: :child_post_implications
|
||||
|
||||
validates :post_id, presence: true, uniqueness: { scope: :parent_post_id }
|
||||
validates :parent_post_id, presence: true
|
||||
|
||||
validate :parent_post_mustnt_be_itself
|
||||
|
||||
private
|
||||
|
||||
def parent_post_mustnt_be_itself
|
||||
if parent_post_id == post_id
|
||||
errors.add :parent_post_id, '親投稿に同じ投稿を設定することはできません.'
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -86,9 +86,9 @@ class Tag < ApplicationRecord
|
||||
def self.niconico = find_or_create_by_tag_name!('ニコニコ', category: :meta)
|
||||
def self.youtube = find_or_create_by_tag_name!('YouTube', category: :meta)
|
||||
|
||||
def self.normalise_tags tag_names, with_tagme: true,
|
||||
with_no_deerjikist: true,
|
||||
deny_nico: true
|
||||
def self.normalise_tags! tag_names, with_tagme: true,
|
||||
with_no_deerjikist: true,
|
||||
deny_nico: true
|
||||
if deny_nico && tag_names.any? { |n| n.downcase.start_with?('nico:') }
|
||||
raise NicoTagNormalisationError
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user