feat: 投稿とタグのリレーション・テーブルについて論理削除と履歴を追加(#84) (#148)
#84 マイグレ修正 Merge remote-tracking branch 'origin/main' into feature/084 #84 構文エラー修正 #84 #84 Co-authored-by: miteruzo <miteruzo@naver.com> Reviewed-on: #148
This commit was merged in pull request #148.
This commit is contained in:
@@ -1,7 +1,3 @@
|
||||
require 'open-uri'
|
||||
require 'nokogiri'
|
||||
|
||||
|
||||
class PostsController < ApplicationController
|
||||
# GET /posts
|
||||
def index
|
||||
@@ -80,8 +76,9 @@ class PostsController < ApplicationController
|
||||
post.thumbnail.attach(thumbnail)
|
||||
if post.save
|
||||
post.resized_thumbnail!
|
||||
post.tags = Tag.normalise_tags(tag_names)
|
||||
post.tags = Tag.expand_parent_tags(post.tags)
|
||||
tags = Tag.normalise_tags(tag_names)
|
||||
tags = Tag.expand_parent_tags(tags)
|
||||
sync_post_tags!(post, tags)
|
||||
render json: post.as_json(include: { tags: { only: [:id, :name, :category, :post_count] } }),
|
||||
status: :created
|
||||
else
|
||||
@@ -114,10 +111,11 @@ class PostsController < ApplicationController
|
||||
original_created_before = params[:original_created_before]
|
||||
|
||||
post = Post.find(params[:id].to_i)
|
||||
tags = post.tags.where(category: 'nico').to_a +
|
||||
Tag.normalise_tags(tag_names, with_tagme: false)
|
||||
tags = Tag.expand_parent_tags(tags)
|
||||
if post.update(title:, tags:, original_created_from:, original_created_before:)
|
||||
if post.update(title:, original_created_from:, original_created_before:)
|
||||
tags = post.tags.where(category: 'nico').to_a +
|
||||
Tag.normalise_tags(tag_names, with_tagme: false)
|
||||
tags = Tag.expand_parent_tags(tags)
|
||||
sync_post_tags!(post, tags)
|
||||
json = post.as_json
|
||||
json['tags'] = build_tag_tree_for(post.tags)
|
||||
render json:, status: :ok
|
||||
@@ -135,7 +133,11 @@ class PostsController < ApplicationController
|
||||
def filtered_posts
|
||||
tag_names = params[:tags]&.split(' ')
|
||||
match_type = params[:match]
|
||||
tag_names.present? ? filter_posts_by_tags(tag_names, match_type) : Post.all
|
||||
if tag_names.present?
|
||||
filter_posts_by_tags(tag_names, match_type)
|
||||
else
|
||||
Post.all
|
||||
end
|
||||
end
|
||||
|
||||
def filter_posts_by_tags tag_names, match_type
|
||||
@@ -150,6 +152,30 @@ class PostsController < ApplicationController
|
||||
posts.distinct
|
||||
end
|
||||
|
||||
def sync_post_tags! post, desired_tags
|
||||
desired_tags.each do |t|
|
||||
t.save! if t.new_record?
|
||||
end
|
||||
|
||||
desired_ids = desired_tags.map(&:id).to_set
|
||||
current_ids = post.tags.pluck(:id).to_set
|
||||
|
||||
to_add = desired_ids - current_ids
|
||||
to_remove = current_ids - desired_ids
|
||||
|
||||
Tag.where(id: to_add).find_each do |tag|
|
||||
begin
|
||||
PostTag.create!(post:, tag:, created_user: current_user)
|
||||
rescue ActiveRecord::RecordNotUnique
|
||||
;
|
||||
end
|
||||
end
|
||||
|
||||
PostTag.where(post_id: post.id, tag_id: to_remove.to_a).kept.find_each do |pt|
|
||||
pt.discard_by!(current_user)
|
||||
end
|
||||
end
|
||||
|
||||
def build_tag_tree_for tags
|
||||
tags = tags.to_a
|
||||
tag_ids = tags.map(&:id)
|
||||
|
||||
Reference in New Issue
Block a user