This commit is contained in:
@@ -63,7 +63,7 @@ class PostsController < ApplicationController
|
||||
post.thumbnail.attach(thumbnail)
|
||||
if post.save
|
||||
post.resized_thumbnail!
|
||||
post.tags = normalise_tags(tags_names)
|
||||
post.tags = Tag.normalise_tags(tags_names)
|
||||
render json: post.as_json(include: { tags: { only: [:id, :name, :category] } }),
|
||||
status: :created
|
||||
else
|
||||
@@ -94,10 +94,10 @@ class PostsController < ApplicationController
|
||||
tag_names = params[:tags].to_s.split(' ')
|
||||
|
||||
post = Post.find(params[:id].to_i)
|
||||
tags = post.tags.where(category: 'nico').to_a + normalise_tags(tag_names)
|
||||
tags = post.tags.where(category: 'nico').to_a + Tag.normalise_tags(tag_names)
|
||||
if post.update(title:, tags:)
|
||||
render json: post.as_json(include: { tags: { only: [:id, :name, :category] } }),
|
||||
status: :created
|
||||
status: :ok
|
||||
else
|
||||
render json: post.errors, status: :unprocessable_entity
|
||||
end
|
||||
@@ -109,14 +109,6 @@ class PostsController < ApplicationController
|
||||
|
||||
private
|
||||
|
||||
CATEGORY_PREFIXES = {
|
||||
'gen:' => 'general',
|
||||
'djk:' => 'deerjikist',
|
||||
'meme:' => 'meme',
|
||||
'chr:' => 'character',
|
||||
'mtr:' => 'material',
|
||||
'meta:' => 'meta' }.freeze
|
||||
|
||||
def filtered_posts
|
||||
tag_names = params[:tags]&.split(' ')
|
||||
match_type = params[:match]
|
||||
@@ -134,19 +126,4 @@ class PostsController < ApplicationController
|
||||
end
|
||||
posts.distinct
|
||||
end
|
||||
|
||||
def normalise_tags tag_names
|
||||
tags = tag_names.map do |name|
|
||||
pf, cat = CATEGORY_PREFIXES.find { |p, _| name.start_with?(p) } || ['', nil]
|
||||
name.delete_prefix!(pf)
|
||||
Tag.find_or_initialize_by(name:).tap do |tag|
|
||||
if cat && tag.category != cat
|
||||
tag.category = cat
|
||||
tag.save!
|
||||
end
|
||||
end
|
||||
end
|
||||
tags << Tag.tagme if tags.size < 20 && tags.none?(Tag.tagme)
|
||||
tags.uniq
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user