コミットを比較
6 コミット
| 作成者 | SHA1 | 日付 | |
|---|---|---|---|
| 2e2b99f74b | |||
| 3665cd8948 | |||
| 176519b929 | |||
| 474e0695ba | |||
| 1e788de9a0 | |||
| 238d236b2b |
@@ -245,19 +245,41 @@ class PostsController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def filter_posts_by_tags tag_names, match_type
|
def filter_posts_by_tags tag_names, match_type
|
||||||
tag_names = TagName.canonicalise(tag_names)
|
literals = tag_names.map do |raw_name|
|
||||||
|
{ name: TagName.canonicalise(raw_name.sub(/\Anot:/i, '')).first,
|
||||||
|
negative: raw_name.downcase.start_with?('not:') }
|
||||||
|
end
|
||||||
|
|
||||||
posts = Post.joins(tags: :tag_name)
|
return Post.all if literals.empty?
|
||||||
|
|
||||||
if match_type == 'any'
|
if match_type == 'any'
|
||||||
posts.where(tag_names: { name: tag_names }).distinct
|
literals.reduce(Post.none) do |posts, literal|
|
||||||
|
posts.or(tag_literal_relation(literal[:name], negative: literal[:negative]))
|
||||||
|
end
|
||||||
else
|
else
|
||||||
posts.where(tag_names: { name: tag_names })
|
literals.reduce(Post.all) do |posts, literal|
|
||||||
.group('posts.id')
|
ids = tagged_post_ids_for(literal[:name])
|
||||||
.having('COUNT(DISTINCT tag_names.id) = ?', tag_names.uniq.size)
|
if literal[:negative]
|
||||||
|
posts.where.not(id: ids)
|
||||||
|
else
|
||||||
|
posts.where(id: ids)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def tag_literal_relation name, negative:
|
||||||
|
ids = tagged_post_ids_for(name)
|
||||||
|
if negative
|
||||||
|
Post.where.not(id: ids)
|
||||||
|
else
|
||||||
|
Post.where(id: ids)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def tagged_post_ids_for(name) =
|
||||||
|
Post.joins(tags: :tag_name).where(tag_names: { name: }).select(:id)
|
||||||
|
|
||||||
def sync_post_tags! post, desired_tags
|
def sync_post_tags! post, desired_tags
|
||||||
desired_tags.each do |t|
|
desired_tags.each do |t|
|
||||||
t.save! if t.new_record?
|
t.save! if t.new_record?
|
||||||
|
|||||||
@@ -17,8 +17,7 @@ class TagsController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def autocomplete
|
def autocomplete
|
||||||
q = params[:q].to_s.strip
|
q = params[:q].to_s.strip.sub(/\Anot:/i, '')
|
||||||
return render json: [] if q.blank?
|
|
||||||
|
|
||||||
with_nico = bool?(:nico, default: true)
|
with_nico = bool?(:nico, default: true)
|
||||||
present_only = bool?(:present, default: true)
|
present_only = bool?(:present, default: true)
|
||||||
|
|||||||
+26
-14
@@ -1,8 +1,12 @@
|
|||||||
class Tag < ApplicationRecord
|
class Tag < ApplicationRecord
|
||||||
|
include Discard::Model
|
||||||
|
|
||||||
class NicoTagNormalisationError < ArgumentError
|
class NicoTagNormalisationError < ArgumentError
|
||||||
;
|
;
|
||||||
end
|
end
|
||||||
|
|
||||||
|
default_scope -> { kept }
|
||||||
|
|
||||||
has_many :post_tags, inverse_of: :tag
|
has_many :post_tags, inverse_of: :tag
|
||||||
has_many :active_post_tags, -> { kept }, class_name: 'PostTag', inverse_of: :tag
|
has_many :active_post_tags, -> { kept }, class_name: 'PostTag', inverse_of: :tag
|
||||||
has_many :post_tags_with_discarded, -> { with_discarded }, class_name: 'PostTag'
|
has_many :post_tags_with_discarded, -> { with_discarded }, class_name: 'PostTag'
|
||||||
@@ -144,25 +148,33 @@ class Tag < ApplicationRecord
|
|||||||
target_tag => Tag
|
target_tag => Tag
|
||||||
|
|
||||||
Tag.transaction do
|
Tag.transaction do
|
||||||
Array(source_tags).compact.uniq.each do |st|
|
Array(source_tags).compact.uniq.each do |source_tag|
|
||||||
st => Tag
|
source_tag => Tag
|
||||||
|
|
||||||
next if st == target_tag
|
next if source_tag == target_tag
|
||||||
|
|
||||||
st.post_tags.find_each do |pt|
|
source_tag.post_tags.kept.find_each do |source_pt|
|
||||||
if PostTag.kept.exists?(post_id: pt.post_id, tag_id: target_tag.id)
|
post_id = source_pt.post_id
|
||||||
pt.discard_by!(nil)
|
source_pt.discard_by!(nil)
|
||||||
# discard 後の update! は禁止なので DB を直に更新
|
unless PostTag.kept.exists?(post_id:, tag: target_tag)
|
||||||
pt.update_columns(tag_id: target_tag.id, updated_at: Time.current)
|
PostTag.create!(post_id:, tag: target_tag)
|
||||||
else
|
|
||||||
pt.update!(tag: target_tag)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
tag_name = st.tag_name
|
source_tag_name = source_tag.tag_name
|
||||||
st.destroy!
|
|
||||||
tag_name.reload
|
if source_tag_name.wiki_page.present?
|
||||||
tag_name.update!(canonical: target_tag.tag_name)
|
raise ActiveRecord::RecordInvalid.new(source_tag_name)
|
||||||
|
end
|
||||||
|
|
||||||
|
source_tag.discard!
|
||||||
|
|
||||||
|
if source_tag.nico?
|
||||||
|
source_tag_name.discard!
|
||||||
|
else
|
||||||
|
source_tag_name.update_columns(canonical_id: target_tag.tag_name_id,
|
||||||
|
updated_at: Time.current)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# 投稿件数を再集計
|
# 投稿件数を再集計
|
||||||
|
|||||||
@@ -1,4 +1,8 @@
|
|||||||
class TagName < ApplicationRecord
|
class TagName < ApplicationRecord
|
||||||
|
include Discard::Model
|
||||||
|
|
||||||
|
default_scope -> { kept }
|
||||||
|
|
||||||
has_one :tag
|
has_one :tag
|
||||||
has_one :wiki_page
|
has_one :wiki_page
|
||||||
|
|
||||||
@@ -10,6 +14,7 @@ class TagName < ApplicationRecord
|
|||||||
validate :canonical_must_be_canonical
|
validate :canonical_must_be_canonical
|
||||||
validate :alias_name_must_not_have_prefix
|
validate :alias_name_must_not_have_prefix
|
||||||
validate :canonical_must_not_be_present_with_tag_or_wiki_page
|
validate :canonical_must_not_be_present_with_tag_or_wiki_page
|
||||||
|
validate :name_must_be_sanitised
|
||||||
|
|
||||||
def self.canonicalise names
|
def self.canonicalise names
|
||||||
names = Array(names).map { |n| n.to_s.strip }.reject(&:blank?)
|
names = Array(names).map { |n| n.to_s.strip }.reject(&:blank?)
|
||||||
@@ -22,6 +27,10 @@ class TagName < ApplicationRecord
|
|||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
def sanitise_name
|
||||||
|
self.name = TagNameSanitisationRule.sanitise(name)
|
||||||
|
end
|
||||||
|
|
||||||
def canonical_must_be_canonical
|
def canonical_must_be_canonical
|
||||||
if canonical&.canonical_id?
|
if canonical&.canonical_id?
|
||||||
errors.add :canonical, 'canonical は実体を示す必要があります.'
|
errors.add :canonical, 'canonical は実体を示す必要があります.'
|
||||||
@@ -39,4 +48,10 @@ class TagName < ApplicationRecord
|
|||||||
errors.add :canonical, 'タグもしくは Wiki の参照がある名前はエーリアスになれません.'
|
errors.add :canonical, 'タグもしくは Wiki の参照がある名前はエーリアスになれません.'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def name_must_be_sanitised
|
||||||
|
if name != TagNameSanitisationRule.sanitise(name)
|
||||||
|
errors.add :name, '名前に使用できない文字が含まれてゐます.'
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -0,0 +1,58 @@
|
|||||||
|
class TagNameSanitisationRule < ApplicationRecord
|
||||||
|
include Discard::Model
|
||||||
|
|
||||||
|
self.primary_key = :priority
|
||||||
|
|
||||||
|
default_scope -> { kept }
|
||||||
|
|
||||||
|
validates :source_pattern, presence: true, uniqueness: true
|
||||||
|
|
||||||
|
validate :source_pattern_must_be_regexp
|
||||||
|
|
||||||
|
class << self
|
||||||
|
def sanitise(name) =
|
||||||
|
rules.reduce(name.dup) { |name, (pattern, replacement)| name.gsub(pattern, replacement) }
|
||||||
|
|
||||||
|
def apply!
|
||||||
|
TagName.find_each do |tn|
|
||||||
|
name = sanitise(tn.name)
|
||||||
|
next if name == tn.name
|
||||||
|
|
||||||
|
TagName.transaction do
|
||||||
|
existing_tn = TagName.find_by(name:)
|
||||||
|
if existing_tn
|
||||||
|
existing_tn = existing_tn.canonical || existing_tn
|
||||||
|
next if existing_tn.id == tn.id
|
||||||
|
|
||||||
|
existing_tag = Tag.find_by(tag_name_id: existing_tn.id)
|
||||||
|
source_tag = Tag.find_by(tag_name_id: tn.id)
|
||||||
|
|
||||||
|
if existing_tag
|
||||||
|
Tag.merge_tags!(existing_tag, source_tag) if tn.tag
|
||||||
|
elsif source_tag
|
||||||
|
source_tag.update_columns(tag_name_id: existing_tn.id, updated_at: Time.current)
|
||||||
|
end
|
||||||
|
tn.discard!
|
||||||
|
|
||||||
|
next
|
||||||
|
end
|
||||||
|
|
||||||
|
# TagName 側の自動サニタイズを回避
|
||||||
|
tn.update_columns(name:, updated_at: Time.current)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def rules = kept.order(:priority).map { |r| [Regexp.new(r.source_pattern), r.replacement] }
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def source_pattern_must_be_regexp
|
||||||
|
Regexp.new(source_pattern)
|
||||||
|
rescue RegexpError
|
||||||
|
errors.add :source_pattern, '変な正規表現だね〜(笑)'
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -2,6 +2,10 @@ require 'set'
|
|||||||
|
|
||||||
|
|
||||||
class WikiPage < ApplicationRecord
|
class WikiPage < ApplicationRecord
|
||||||
|
include Discard::Model
|
||||||
|
|
||||||
|
default_scope -> { kept }
|
||||||
|
|
||||||
has_many :wiki_revisions, dependent: :destroy
|
has_many :wiki_revisions, dependent: :destroy
|
||||||
belongs_to :created_user, class_name: 'User'
|
belongs_to :created_user, class_name: 'User'
|
||||||
belongs_to :updated_user, class_name: 'User'
|
belongs_to :updated_user, class_name: 'User'
|
||||||
|
|||||||
@@ -0,0 +1,31 @@
|
|||||||
|
class CreateTagNameSanitisationRules < ActiveRecord::Migration[8.0]
|
||||||
|
def up
|
||||||
|
create_table :tag_name_sanitisation_rules, id: :integer, primary_key: :priority do |t|
|
||||||
|
t.string :source_pattern, null: false
|
||||||
|
t.string :replacement, null: false
|
||||||
|
t.timestamps
|
||||||
|
t.datetime :discarded_at
|
||||||
|
t.index :source_pattern, unique: true
|
||||||
|
t.index :discarded_at
|
||||||
|
end
|
||||||
|
|
||||||
|
now = ActiveRecord::Base.connection.quote(Time.current)
|
||||||
|
execute <<~SQL
|
||||||
|
INSERT INTO
|
||||||
|
tag_name_sanitisation_rules(priority, source_pattern, replacement, created_at, updated_at)
|
||||||
|
VALUES
|
||||||
|
(10, '\\\\*', '_', #{ now }, #{ now })
|
||||||
|
, (20, '\\\\?', '_', #{ now }, #{ now })
|
||||||
|
, (25, '\\\\/', '_', #{ now }, #{ now })
|
||||||
|
, (30, '_+', '_', #{ now }, #{ now })
|
||||||
|
, (40, '_$', '', #{ now }, #{ now })
|
||||||
|
, (45, '^([^:]+\\\\:)?_', '\\\\1', #{ now }, #{ now })
|
||||||
|
, (50, '^([^:]+\\\\:)?$', '\\\\1null', #{ now }, #{ now })
|
||||||
|
;
|
||||||
|
SQL
|
||||||
|
end
|
||||||
|
|
||||||
|
def down
|
||||||
|
drop_table :tag_name_sanitisation_rules
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
class AddDiscardedAtToTags < ActiveRecord::Migration[8.0]
|
||||||
|
def change
|
||||||
|
add_column :tags, :discarded_at, :datetime
|
||||||
|
add_index :tags, :discarded_at
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
class AddDiscardedAtToTagNames < ActiveRecord::Migration[8.0]
|
||||||
|
def change
|
||||||
|
add_column :tag_names, :discarded_at, :datetime
|
||||||
|
add_index :tag_names, :discarded_at
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
class AddDiscardedAtToWikiPages < ActiveRecord::Migration[8.0]
|
||||||
|
def change
|
||||||
|
add_column :wiki_pages, :discarded_at, :datetime
|
||||||
|
add_index :wiki_pages, :discarded_at
|
||||||
|
end
|
||||||
|
end
|
||||||
生成ファイル
+17
-1
@@ -10,7 +10,7 @@
|
|||||||
#
|
#
|
||||||
# It's strongly recommended that you check this file into your version control system.
|
# It's strongly recommended that you check this file into your version control system.
|
||||||
|
|
||||||
ActiveRecord::Schema[8.0].define(version: 2026_03_03_122700) do
|
ActiveRecord::Schema[8.0].define(version: 2026_03_11_232300) do
|
||||||
create_table "active_storage_attachments", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
|
create_table "active_storage_attachments", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
|
||||||
t.string "name", null: false
|
t.string "name", null: false
|
||||||
t.string "record_type", null: false
|
t.string "record_type", null: false
|
||||||
@@ -127,12 +127,24 @@ ActiveRecord::Schema[8.0].define(version: 2026_03_03_122700) do
|
|||||||
t.index ["tag_id"], name: "index_tag_implications_on_tag_id"
|
t.index ["tag_id"], name: "index_tag_implications_on_tag_id"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
create_table "tag_name_sanitisation_rules", primary_key: "priority", id: :integer, charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
|
||||||
|
t.string "source_pattern", null: false
|
||||||
|
t.string "replacement", null: false
|
||||||
|
t.datetime "created_at", null: false
|
||||||
|
t.datetime "updated_at", null: false
|
||||||
|
t.datetime "discarded_at"
|
||||||
|
t.index ["discarded_at"], name: "index_tag_name_sanitisation_rules_on_discarded_at"
|
||||||
|
t.index ["source_pattern"], name: "index_tag_name_sanitisation_rules_on_source_pattern", unique: true
|
||||||
|
end
|
||||||
|
|
||||||
create_table "tag_names", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
|
create_table "tag_names", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
|
||||||
t.string "name", null: false
|
t.string "name", null: false
|
||||||
t.bigint "canonical_id"
|
t.bigint "canonical_id"
|
||||||
t.datetime "created_at", null: false
|
t.datetime "created_at", null: false
|
||||||
t.datetime "updated_at", null: false
|
t.datetime "updated_at", null: false
|
||||||
|
t.datetime "discarded_at"
|
||||||
t.index ["canonical_id"], name: "index_tag_names_on_canonical_id"
|
t.index ["canonical_id"], name: "index_tag_names_on_canonical_id"
|
||||||
|
t.index ["discarded_at"], name: "index_tag_names_on_discarded_at"
|
||||||
t.index ["name"], name: "index_tag_names_on_name", unique: true
|
t.index ["name"], name: "index_tag_names_on_name", unique: true
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -150,6 +162,8 @@ ActiveRecord::Schema[8.0].define(version: 2026_03_03_122700) do
|
|||||||
t.datetime "created_at", null: false
|
t.datetime "created_at", null: false
|
||||||
t.datetime "updated_at", null: false
|
t.datetime "updated_at", null: false
|
||||||
t.integer "post_count", default: 0, null: false
|
t.integer "post_count", default: 0, null: false
|
||||||
|
t.datetime "discarded_at"
|
||||||
|
t.index ["discarded_at"], name: "index_tags_on_discarded_at"
|
||||||
t.index ["tag_name_id"], name: "index_tags_on_tag_name_id", unique: true
|
t.index ["tag_name_id"], name: "index_tags_on_tag_name_id", unique: true
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -192,7 +206,9 @@ ActiveRecord::Schema[8.0].define(version: 2026_03_03_122700) do
|
|||||||
t.bigint "updated_user_id", null: false
|
t.bigint "updated_user_id", null: false
|
||||||
t.datetime "created_at", null: false
|
t.datetime "created_at", null: false
|
||||||
t.datetime "updated_at", null: false
|
t.datetime "updated_at", null: false
|
||||||
|
t.datetime "discarded_at"
|
||||||
t.index ["created_user_id"], name: "index_wiki_pages_on_created_user_id"
|
t.index ["created_user_id"], name: "index_wiki_pages_on_created_user_id"
|
||||||
|
t.index ["discarded_at"], name: "index_wiki_pages_on_discarded_at"
|
||||||
t.index ["tag_name_id"], name: "index_wiki_pages_on_tag_name_id", unique: true
|
t.index ["tag_name_id"], name: "index_wiki_pages_on_tag_name_id", unique: true
|
||||||
t.index ["updated_user_id"], name: "index_wiki_pages_on_updated_user_id"
|
t.index ["updated_user_id"], name: "index_wiki_pages_on_updated_user_id"
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -108,7 +108,7 @@ namespace :nico do
|
|||||||
desired_non_nico_tag_ids = []
|
desired_non_nico_tag_ids = []
|
||||||
|
|
||||||
datum['tags'].each do |raw|
|
datum['tags'].each do |raw|
|
||||||
name = "nico:#{ raw }"
|
name = TagNameSanitisationRule.sanitise("nico:#{ raw }")
|
||||||
tag = Tag.find_or_create_by_tag_name!(name, category: :nico)
|
tag = Tag.find_or_create_by_tag_name!(name, category: :nico)
|
||||||
desired_nico_tag_based_ids << tag.id
|
desired_nico_tag_based_ids << tag.id
|
||||||
|
|
||||||
|
|||||||
@@ -1,12 +1,22 @@
|
|||||||
FactoryBot.define do
|
FactoryBot.define do
|
||||||
factory :tag do
|
factory :tag do
|
||||||
|
transient do
|
||||||
|
name { nil }
|
||||||
|
end
|
||||||
|
|
||||||
category { :general }
|
category { :general }
|
||||||
post_count { 0 }
|
post_count { 0 }
|
||||||
association :tag_name
|
association :tag_name
|
||||||
|
|
||||||
|
after(:build) do |tag, evaluator|
|
||||||
|
tag.name = evaluator.name if evaluator.name.present?
|
||||||
|
end
|
||||||
|
|
||||||
trait :nico do
|
trait :nico do
|
||||||
category { :nico }
|
category { :nico }
|
||||||
tag_name { association(:tag_name, name: "nico:#{ SecureRandom.hex(4) }") }
|
transient do
|
||||||
|
name { "nico:#{ SecureRandom.hex(4) }" }
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -0,0 +1,101 @@
|
|||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
RSpec.describe TagNameSanitisationRule, type: :model do
|
||||||
|
describe '.sanitise' do
|
||||||
|
before do
|
||||||
|
described_class.create!(priority: 10, source_pattern: '_', replacement: '')
|
||||||
|
described_class.create!(priority: 20, source_pattern: 'ABC', replacement: 'abc')
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'applies sanitisation rules sequentially in priority order' do
|
||||||
|
expect(described_class.sanitise('A_B_C')).to eq('abc')
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'does not fail when a rule does not match' do
|
||||||
|
expect { described_class.sanitise('xyz') }.not_to raise_error
|
||||||
|
expect(described_class.sanitise('xyz')).to eq('xyz')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'validations' do
|
||||||
|
it 'is invalid when source_pattern is not a valid regexp' do
|
||||||
|
rule = described_class.new(priority: 10, source_pattern: '[', replacement: '')
|
||||||
|
expect(rule).to be_invalid
|
||||||
|
expect(rule.errors[:source_pattern]).to be_present
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '.apply!' do
|
||||||
|
before do
|
||||||
|
described_class.create!(priority: 10, source_pattern: '_', replacement: '')
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when no conflicting tag_name exists' do
|
||||||
|
let!(:tag_name) do
|
||||||
|
TagName.create!(name: 'tmp').tap do |tn|
|
||||||
|
tn.update_columns(name: 'foo_bar', updated_at: Time.current)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'renames the tag_name' do
|
||||||
|
described_class.apply!
|
||||||
|
expect(tag_name.reload.name).to eq('foobar')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when a conflicting canonical tag_name exists' do
|
||||||
|
let!(:existing) { TagName.create!(name: 'foobar') }
|
||||||
|
let!(:source) do
|
||||||
|
TagName.create!(name: 'tmp').tap do |tn|
|
||||||
|
tn.update_columns(name: 'foo_bar', updated_at: Time.current)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'deletes the source tag_name' do
|
||||||
|
described_class.apply!
|
||||||
|
expect(TagName.exists?(source.id)).to be(false)
|
||||||
|
expect(existing.reload.name).to eq('foobar')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when the source tag_name has a tag and the existing one has no tag' do
|
||||||
|
let!(:existing) { TagName.create!(name: 'foobar') }
|
||||||
|
let!(:source_tag) { create(:tag, name: 'tmp', category: :general) }
|
||||||
|
let!(:source_tag_name_id) { source_tag.tag_name_id }
|
||||||
|
|
||||||
|
before do
|
||||||
|
source_tag.tag_name.update_columns(name: 'foo_bar', updated_at: Time.current)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'moves the tag to the existing tag_name' do
|
||||||
|
described_class.apply!
|
||||||
|
expected_tag_name_id = existing.canonical_id || existing.id
|
||||||
|
expect(source_tag.reload.tag_name_id).to eq(expected_tag_name_id)
|
||||||
|
expect(TagName.exists?(source_tag_name_id)).to be(false)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when both source and existing tag_names have tags' do
|
||||||
|
let!(:existing_tn) { TagName.create!(name: 'foobar') }
|
||||||
|
let!(:existing_tag) { Tag.create!(tag_name: existing_tn, category: :general) }
|
||||||
|
|
||||||
|
let!(:source_tn) { TagName.create!(name: 'tmp') }
|
||||||
|
let!(:source_tag) { Tag.create!(tag_name: source_tn, category: :general) }
|
||||||
|
let!(:source_tag_name_id) { source_tn.id }
|
||||||
|
|
||||||
|
before do
|
||||||
|
source_tn.update_columns(name: 'foo_bar', updated_at: Time.current)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'merges the source tag into the existing tag and deletes the source tag_name' do
|
||||||
|
expect(TagName.find_by(name: 'foobar')&.tag&.id).to eq(existing_tag.id)
|
||||||
|
expect(TagName.find_by(name: 'foo_bar')&.tag&.id).to eq(source_tag.id)
|
||||||
|
|
||||||
|
described_class.apply!
|
||||||
|
|
||||||
|
expect(Tag.exists?(source_tag.id)).to be(false)
|
||||||
|
expect(TagName.exists?(source_tag.tag_name_id)).to be(false)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
+101
-23
@@ -2,20 +2,31 @@ require 'rails_helper'
|
|||||||
|
|
||||||
RSpec.describe Tag, type: :model do
|
RSpec.describe Tag, type: :model do
|
||||||
describe '.merge_tags!' do
|
describe '.merge_tags!' do
|
||||||
let!(:target_tag) { create(:tag) }
|
let!(:target_tag) { create(:tag, category: :general) }
|
||||||
let!(:source_tag) { create(:tag) }
|
let!(:source_tag) { create(:tag, category: :general) }
|
||||||
|
let!(:source_tag_name) { source_tag.tag_name }
|
||||||
|
|
||||||
let!(:post_record) { Post.create!(url: 'https://example.com/posts/1', title: 'test post') }
|
let!(:post_record) do
|
||||||
|
Post.create!(url: 'https://example.com/posts/1', title: 'test post')
|
||||||
|
end
|
||||||
|
|
||||||
context 'when merging a simple source tag' do
|
context 'when merging a simple source tag' do
|
||||||
let!(:source_post_tag) { PostTag.create!(post: post_record, tag: source_tag) }
|
let!(:source_post_tag) { PostTag.create!(post: post_record, tag: source_tag) }
|
||||||
|
|
||||||
it 'moves the post_tag, deletes the source tag, and aliases the source tag_name' do
|
it 'discards the source post_tag, creates an active target post_tag, discards the source tag, and aliases the source tag_name' do
|
||||||
described_class.merge_tags!(target_tag, [source_tag])
|
described_class.merge_tags!(target_tag, [source_tag])
|
||||||
|
|
||||||
expect(source_post_tag.reload.tag_id).to eq(target_tag.id)
|
source_pt = PostTag.with_discarded.find(source_post_tag.id)
|
||||||
expect(Tag.exists?(source_tag.id)).to be(false)
|
active_target = PostTag.kept.find_by(post_id: post_record.id, tag_id: target_tag.id)
|
||||||
expect(source_tag.tag_name.reload.canonical_id).to eq(target_tag.tag_name_id)
|
|
||||||
|
expect(source_pt.discarded_at).to be_present
|
||||||
|
expect(source_pt.tag_id).to eq(source_tag.id)
|
||||||
|
expect(active_target).to be_present
|
||||||
|
|
||||||
|
expect(Tag.with_discarded.find(source_tag.id)).to be_discarded
|
||||||
|
expect(TagName.with_discarded.find(source_tag_name.id)).not_to be_discarded
|
||||||
|
expect(source_tag_name.reload.canonical_id).to eq(target_tag.tag_name_id)
|
||||||
|
expect(target_tag.reload.post_count).to eq(1)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -23,38 +34,84 @@ RSpec.describe Tag, type: :model do
|
|||||||
let!(:target_post_tag) { PostTag.create!(post: post_record, tag: target_tag) }
|
let!(:target_post_tag) { PostTag.create!(post: post_record, tag: target_tag) }
|
||||||
let!(:source_post_tag) { PostTag.create!(post: post_record, tag: source_tag) }
|
let!(:source_post_tag) { PostTag.create!(post: post_record, tag: source_tag) }
|
||||||
|
|
||||||
it 'discards the duplicate source post_tag and keeps one active target post_tag' do
|
it 'discards the source post_tag, keeps one active target post_tag, discards the source tag, and aliases the source tag_name' do
|
||||||
described_class.merge_tags!(target_tag, [source_tag])
|
described_class.merge_tags!(target_tag, [source_tag])
|
||||||
|
|
||||||
|
source_pt = PostTag.with_discarded.find(source_post_tag.id)
|
||||||
active = PostTag.kept.where(post_id: post_record.id, tag_id: target_tag.id)
|
active = PostTag.kept.where(post_id: post_record.id, tag_id: target_tag.id)
|
||||||
discarded_source = PostTag.with_discarded.find(source_post_tag.id)
|
|
||||||
|
|
||||||
|
expect(source_pt.discarded_at).to be_present
|
||||||
|
expect(source_pt.tag_id).to eq(source_tag.id)
|
||||||
expect(active.count).to eq(1)
|
expect(active.count).to eq(1)
|
||||||
expect(discarded_source.discarded_at).to be_present
|
expect(active.first.id).to eq(target_post_tag.id)
|
||||||
expect(Tag.exists?(source_tag.id)).to be(false)
|
|
||||||
expect(source_tag.tag_name.reload.canonical_id).to eq(target_tag.tag_name_id)
|
expect(Tag.with_discarded.find(source_tag.id)).to be_discarded
|
||||||
|
expect(TagName.with_discarded.find(source_tag_name.id)).not_to be_discarded
|
||||||
|
expect(source_tag_name.reload.canonical_id).to eq(target_tag.tag_name_id)
|
||||||
|
expect(target_tag.reload.post_count).to eq(1)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when source_tags includes the target itself' do
|
context 'when source_tags includes the target itself' do
|
||||||
let!(:source_post_tag) { PostTag.create!(post: post_record, tag: source_tag) }
|
let!(:source_post_tag) { PostTag.create!(post: post_record, tag: source_tag) }
|
||||||
|
|
||||||
it 'ignores the target tag in source_tags' do
|
it 'ignores the target in source_tags while still merging the source tag' do
|
||||||
described_class.merge_tags!(target_tag, [source_tag, target_tag])
|
described_class.merge_tags!(target_tag, [source_tag, target_tag])
|
||||||
|
|
||||||
expect(Tag.exists?(target_tag.id)).to be(true)
|
source_pt = PostTag.with_discarded.find(source_post_tag.id)
|
||||||
expect(Tag.exists?(source_tag.id)).to be(false)
|
active_target = PostTag.kept.find_by(post_id: post_record.id, tag_id: target_tag.id)
|
||||||
expect(source_post_tag.reload.tag_id).to eq(target_tag.id)
|
|
||||||
|
expect(Tag.find(target_tag.id)).to be_present
|
||||||
|
expect(Tag.with_discarded.find(source_tag.id)).to be_discarded
|
||||||
|
expect(source_pt.discarded_at).to be_present
|
||||||
|
expect(source_pt.tag_id).to eq(source_tag.id)
|
||||||
|
expect(active_target).to be_present
|
||||||
|
expect(source_tag_name.reload.canonical_id).to eq(target_tag.tag_name_id)
|
||||||
|
expect(target_tag.reload.post_count).to eq(1)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when aliasing the source tag_name is invalid' do
|
context 'when the source tag_name is invalid under sanitisation rules' do
|
||||||
|
let!(:source_post_tag) { PostTag.create!(post: post_record, tag: source_tag) }
|
||||||
|
let!(:sanitisation_rule) do
|
||||||
|
TagNameSanitisationRule.create!(
|
||||||
|
priority: 99_999,
|
||||||
|
source_pattern: 'INVALIDTOKEN',
|
||||||
|
replacement: ''
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
before do
|
||||||
|
source_tag_name.update_columns(
|
||||||
|
name: "#{ source_tag_name.name }INVALIDTOKEN",
|
||||||
|
updated_at: Time.current
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'still merges, but discards the source tag_name instead of aliasing it' do
|
||||||
|
described_class.merge_tags!(target_tag, [source_tag])
|
||||||
|
|
||||||
|
source_pt = PostTag.with_discarded.find(source_post_tag.id)
|
||||||
|
active_target = PostTag.kept.find_by(post_id: post_record.id, tag_id: target_tag.id)
|
||||||
|
discarded_source_tag_name = TagName.with_discarded.find(source_tag_name.id)
|
||||||
|
|
||||||
|
expect(source_pt.discarded_at).to be_present
|
||||||
|
expect(source_pt.tag_id).to eq(source_tag.id)
|
||||||
|
expect(active_target).to be_present
|
||||||
|
|
||||||
|
expect(Tag.with_discarded.find(source_tag.id)).to be_discarded
|
||||||
|
expect(target_tag.reload.post_count).to eq(1)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when the source tag_name has a wiki_page' do
|
||||||
let!(:source_post_tag) { PostTag.create!(post: post_record, tag: source_tag) }
|
let!(:source_post_tag) { PostTag.create!(post: post_record, tag: source_tag) }
|
||||||
let!(:wiki_page) do
|
let!(:wiki_page) do
|
||||||
WikiPage.create!(
|
WikiPage.create!(
|
||||||
tag_name: source_tag.tag_name,
|
tag_name: source_tag_name,
|
||||||
created_user: create_admin_user!,
|
created_user: create_admin_user!,
|
||||||
updated_user: create_admin_user!)
|
updated_user: create_admin_user!
|
||||||
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'rolls back the transaction' do
|
it 'rolls back the transaction' do
|
||||||
@@ -62,9 +119,30 @@ RSpec.describe Tag, type: :model do
|
|||||||
described_class.merge_tags!(target_tag, [source_tag])
|
described_class.merge_tags!(target_tag, [source_tag])
|
||||||
}.to raise_error(ActiveRecord::RecordInvalid)
|
}.to raise_error(ActiveRecord::RecordInvalid)
|
||||||
|
|
||||||
expect(Tag.exists?(source_tag.id)).to be(true)
|
expect(Tag.with_discarded.find(source_tag.id)).not_to be_discarded
|
||||||
expect(source_post_tag.reload.tag_id).to eq(source_tag.id)
|
expect(TagName.with_discarded.find(source_tag_name.id)).not_to be_discarded
|
||||||
expect(source_tag.tag_name.reload.canonical_id).to be_nil
|
expect(PostTag.kept.find(source_post_tag.id).tag_id).to eq(source_tag.id)
|
||||||
|
expect(PostTag.kept.find_by(post_id: post_record.id, tag_id: target_tag.id)).to be_nil
|
||||||
|
expect(source_tag_name.reload.canonical_id).to be_nil
|
||||||
|
expect(target_tag.reload.post_count).to eq(0)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when merging a nico source tag' do
|
||||||
|
let!(:target_tag) { create(:tag, category: :nico, name: 'nico:foo') }
|
||||||
|
let!(:source_tag) { create(:tag, category: :nico, name: 'nico:bar') }
|
||||||
|
let!(:source_tag_name_id) { source_tag.tag_name_id }
|
||||||
|
|
||||||
|
it 'discards the source tag_name instead of aliasing it' do
|
||||||
|
described_class.merge_tags!(target_tag, [source_tag])
|
||||||
|
|
||||||
|
discarded_source_tag = Tag.with_discarded.find(source_tag.id)
|
||||||
|
discarded_source_tag_name = TagName.with_discarded.find(source_tag_name_id)
|
||||||
|
|
||||||
|
expect(discarded_source_tag).to be_discarded
|
||||||
|
expect(discarded_source_tag_name).to be_discarded
|
||||||
|
expect(discarded_source_tag_name.canonical_id).to be_nil
|
||||||
|
expect(target_tag.reload.post_count).to eq(0)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ RSpec.describe 'Posts API', type: :request do
|
|||||||
end
|
end
|
||||||
|
|
||||||
let!(:tag_name) { TagName.create!(name: 'spec_tag') }
|
let!(:tag_name) { TagName.create!(name: 'spec_tag') }
|
||||||
let!(:tag) { Tag.create!(tag_name: tag_name, category: 'general') }
|
let!(:tag) { Tag.create!(tag_name: tag_name, category: :general) }
|
||||||
|
|
||||||
let!(:post_record) do
|
let!(:post_record) do
|
||||||
Post.create!(title: 'spec post', url: 'https://example.com/spec').tap do |p|
|
Post.create!(title: 'spec post', url: 'https://example.com/spec').tap do |p|
|
||||||
@@ -28,9 +28,9 @@ RSpec.describe 'Posts API', type: :request do
|
|||||||
let!(:user) { create_member_user! }
|
let!(:user) { create_member_user! }
|
||||||
|
|
||||||
let!(:tag_name) { TagName.create!(name: "spec_tag") }
|
let!(:tag_name) { TagName.create!(name: "spec_tag") }
|
||||||
let!(:tag) { Tag.create!(tag_name:, category: "general") }
|
let!(:tag) { Tag.create!(tag_name:, category: :general) }
|
||||||
let!(:tag_name2) { TagName.create!(name: 'unko') }
|
let!(:tag_name2) { TagName.create!(name: 'unko') }
|
||||||
let!(:tag2) { Tag.create!(tag_name: tag_name2, category: 'deerjikist') }
|
let!(:tag2) { Tag.create!(tag_name: tag_name2, category: :deerjikist) }
|
||||||
let!(:alias_tag_name) { TagName.create!(name: 'manko', canonical: tag_name) }
|
let!(:alias_tag_name) { TagName.create!(name: 'manko', canonical: tag_name) }
|
||||||
|
|
||||||
let!(:hit_post) do
|
let!(:hit_post) do
|
||||||
@@ -116,6 +116,123 @@ RSpec.describe 'Posts API', type: :request do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context 'when tags contain not:' do
|
||||||
|
let!(:foo_tag_name) { TagName.create!(name: 'not_spec_foo') }
|
||||||
|
let!(:foo_tag) { Tag.create!(tag_name: foo_tag_name, category: :general) }
|
||||||
|
|
||||||
|
let!(:bar_tag_name) { TagName.create!(name: 'not_spec_bar') }
|
||||||
|
let!(:bar_tag) { Tag.create!(tag_name: bar_tag_name, category: :general) }
|
||||||
|
|
||||||
|
let!(:baz_tag_name) { TagName.create!(name: 'not_spec_baz') }
|
||||||
|
let!(:baz_tag) { Tag.create!(tag_name: baz_tag_name, category: :general) }
|
||||||
|
|
||||||
|
let!(:foo_alias_tag_name) do
|
||||||
|
TagName.create!(name: 'not_spec_foo_alias', canonical: foo_tag_name)
|
||||||
|
end
|
||||||
|
|
||||||
|
let!(:foo_only_post) do
|
||||||
|
Post.create!(uploaded_user: user, title: 'foo only',
|
||||||
|
url: 'https://example.com/not-spec-foo').tap do |p|
|
||||||
|
PostTag.create!(post: p, tag: foo_tag)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
let!(:bar_only_post) do
|
||||||
|
Post.create!(uploaded_user: user, title: 'bar only',
|
||||||
|
url: 'https://example.com/not-spec-bar').tap do |p|
|
||||||
|
PostTag.create!(post: p, tag: bar_tag)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
let!(:baz_only_post) do
|
||||||
|
Post.create!(uploaded_user: user, title: 'baz only',
|
||||||
|
url: 'https://example.com/not-spec-baz').tap do |p|
|
||||||
|
PostTag.create!(post: p, tag: baz_tag)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
let!(:foo_bar_post) do
|
||||||
|
Post.create!(uploaded_user: user, title: 'foo bar',
|
||||||
|
url: 'https://example.com/not-spec-foo-bar').tap do |p|
|
||||||
|
PostTag.create!(post: p, tag: foo_tag)
|
||||||
|
PostTag.create!(post: p, tag: bar_tag)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
let!(:foo_baz_post) do
|
||||||
|
Post.create!(uploaded_user: user, title: 'foo baz',
|
||||||
|
url: 'https://example.com/not-spec-foo-baz').tap do |p|
|
||||||
|
PostTag.create!(post: p, tag: foo_tag)
|
||||||
|
PostTag.create!(post: p, tag: baz_tag)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
let(:controlled_ids) do
|
||||||
|
[foo_only_post.id, bar_only_post.id, baz_only_post.id,
|
||||||
|
foo_bar_post.id, foo_baz_post.id]
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'supports not search' do
|
||||||
|
get '/posts', params: { tags: 'not:not_spec_foo' }
|
||||||
|
|
||||||
|
expect(response).to have_http_status(:ok)
|
||||||
|
|
||||||
|
expect(json.fetch('posts').map { |p| p['id'] } & controlled_ids).to match_array(
|
||||||
|
[bar_only_post.id, baz_only_post.id]
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'supports alias in not search' do
|
||||||
|
get '/posts', params: { tags: 'not:not_spec_foo_alias' }
|
||||||
|
|
||||||
|
expect(response).to have_http_status(:ok)
|
||||||
|
|
||||||
|
expect(json.fetch('posts').map { |p| p['id'] } & controlled_ids).to match_array(
|
||||||
|
[bar_only_post.id, baz_only_post.id]
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'treats multiple not terms as AND when match is omitted' do
|
||||||
|
get '/posts', params: { tags: 'not:not_spec_foo not:not_spec_bar' }
|
||||||
|
|
||||||
|
expect(response).to have_http_status(:ok)
|
||||||
|
|
||||||
|
expect(json.fetch('posts').map { |p| p['id'] } & controlled_ids).to match_array(
|
||||||
|
[baz_only_post.id]
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'treats multiple not terms as OR when match=any' do
|
||||||
|
get '/posts', params: { tags: 'not:not_spec_foo not:not_spec_bar', match: 'any' }
|
||||||
|
|
||||||
|
expect(response).to have_http_status(:ok)
|
||||||
|
|
||||||
|
expect(json.fetch('posts').map { |p| p['id'] } & controlled_ids).to match_array(
|
||||||
|
[foo_only_post.id, bar_only_post.id, baz_only_post.id, foo_baz_post.id]
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'supports mixed positive and negative search with AND' do
|
||||||
|
get '/posts', params: { tags: 'not_spec_foo not:not_spec_bar' }
|
||||||
|
|
||||||
|
expect(response).to have_http_status(:ok)
|
||||||
|
|
||||||
|
expect(json.fetch('posts').map { |p| p['id'] } & controlled_ids).to match_array(
|
||||||
|
[foo_only_post.id, foo_baz_post.id]
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'supports mixed positive and negative search with OR when match=any' do
|
||||||
|
get '/posts', params: { tags: 'not_spec_foo not:not_spec_bar', match: 'any' }
|
||||||
|
|
||||||
|
expect(response).to have_http_status(:ok)
|
||||||
|
|
||||||
|
expect(json.fetch('posts').map { |p| p['id'] } & controlled_ids).to match_array(
|
||||||
|
[foo_only_post.id, baz_only_post.id, foo_bar_post.id, foo_baz_post.id]
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context 'when url is provided' do
|
context 'when url is provided' do
|
||||||
let!(:url_hit_post) do
|
let!(:url_hit_post) do
|
||||||
Post.create!(uploaded_user: user, title: 'url hit',
|
Post.create!(uploaded_user: user, title: 'url hit',
|
||||||
@@ -409,7 +526,7 @@ RSpec.describe 'Posts API', type: :request do
|
|||||||
context "when nico tag already exists in tags" do
|
context "when nico tag already exists in tags" do
|
||||||
before do
|
before do
|
||||||
Tag.find_or_create_by!(tag_name: TagName.find_or_create_by!(name: 'nico:nico_tag'),
|
Tag.find_or_create_by!(tag_name: TagName.find_or_create_by!(name: 'nico:nico_tag'),
|
||||||
category: 'nico')
|
category: :nico)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'return 400' do
|
it 'return 400' do
|
||||||
@@ -475,7 +592,7 @@ RSpec.describe 'Posts API', type: :request do
|
|||||||
|
|
||||||
# 追加で別タグも作って、更新時に入れ替わることを見る
|
# 追加で別タグも作って、更新時に入れ替わることを見る
|
||||||
tn2 = TagName.create!(name: 'spec_tag_2')
|
tn2 = TagName.create!(name: 'spec_tag_2')
|
||||||
Tag.create!(tag_name: tn2, category: 'general')
|
Tag.create!(tag_name: tn2, category: :general)
|
||||||
|
|
||||||
put "/posts/#{post_record.id}", params: {
|
put "/posts/#{post_record.id}", params: {
|
||||||
title: 'updated title',
|
title: 'updated title',
|
||||||
@@ -494,7 +611,7 @@ RSpec.describe 'Posts API', type: :request do
|
|||||||
context "when nico tag already exists in tags" do
|
context "when nico tag already exists in tags" do
|
||||||
before do
|
before do
|
||||||
Tag.find_or_create_by!(tag_name: TagName.find_or_create_by!(name: 'nico:nico_tag'),
|
Tag.find_or_create_by!(tag_name: TagName.find_or_create_by!(name: 'nico:nico_tag'),
|
||||||
category: 'nico')
|
category: :nico)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'return 400' do
|
it 'return 400' do
|
||||||
@@ -531,7 +648,7 @@ RSpec.describe 'Posts API', type: :request do
|
|||||||
it 'returns add/remove events (history) for a post' do
|
it 'returns add/remove events (history) for a post' do
|
||||||
# add
|
# add
|
||||||
tn2 = TagName.create!(name: 'spec_tag2')
|
tn2 = TagName.create!(name: 'spec_tag2')
|
||||||
tag2 = Tag.create!(tag_name: tn2, category: 'general')
|
tag2 = Tag.create!(tag_name: tn2, category: :general)
|
||||||
pt = PostTag.create!(post: post_record, tag: tag2, created_user: member)
|
pt = PostTag.create!(post: post_record, tag: tag2, created_user: member)
|
||||||
|
|
||||||
# remove (discard)
|
# remove (discard)
|
||||||
|
|||||||
@@ -56,8 +56,11 @@ export default (() => {
|
|||||||
if (activeIndex < 0)
|
if (activeIndex < 0)
|
||||||
break
|
break
|
||||||
ev.preventDefault ()
|
ev.preventDefault ()
|
||||||
const selected = suggestions[activeIndex]
|
{
|
||||||
selected && handleTagSelect (selected)
|
const selected = suggestions[activeIndex]
|
||||||
|
if (selected)
|
||||||
|
handleTagSelect (selected)
|
||||||
|
}
|
||||||
break
|
break
|
||||||
|
|
||||||
case 'Escape':
|
case 'Escape':
|
||||||
@@ -67,14 +70,25 @@ export default (() => {
|
|||||||
}
|
}
|
||||||
if (ev.key === 'Enter' && (!(suggestionsVsbl) || activeIndex < 0))
|
if (ev.key === 'Enter' && (!(suggestionsVsbl) || activeIndex < 0))
|
||||||
{
|
{
|
||||||
navigate (`/posts?${ (new URLSearchParams ({ tags: search })).toString () }`)
|
const parts = search.split (' ')
|
||||||
|
const match = parts.map (t => t.toLowerCase ()).includes ('type:or') ? 'any' : 'all'
|
||||||
|
navigate (`/posts?${
|
||||||
|
(new URLSearchParams ({
|
||||||
|
tags: parts.map (t => t.toLowerCase ())
|
||||||
|
.filter (t => t !== 'type:or')
|
||||||
|
.join (' '),
|
||||||
|
match }))
|
||||||
|
.toString () }`)
|
||||||
setSuggestionsVsbl (false)
|
setSuggestionsVsbl (false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const handleTagSelect = (tag: Tag) => {
|
const handleTagSelect = (tag: Tag) => {
|
||||||
const parts = search.split (' ')
|
const parts = search.split (' ')
|
||||||
parts[parts.length - 1] = tag.name
|
parts[parts.length - 1] = (
|
||||||
|
(parts[parts.length - 1].slice(0, 4).toLowerCase () === 'not:')
|
||||||
|
? ('not:' + tag.name)
|
||||||
|
: tag.name)
|
||||||
setSearch (parts.join (' ') + ' ')
|
setSearch (parts.join (' ') + ' ')
|
||||||
setSuggestions ([])
|
setSuggestions ([])
|
||||||
setActiveIndex (-1)
|
setActiveIndex (-1)
|
||||||
@@ -82,7 +96,8 @@ export default (() => {
|
|||||||
|
|
||||||
useEffect (() => {
|
useEffect (() => {
|
||||||
const query = new URLSearchParams (location.search)
|
const query = new URLSearchParams (location.search)
|
||||||
const tagsQuery = query.get ('tags') ?? ''
|
const anyFlg = query.get ('match') === 'any'
|
||||||
|
const tagsQuery = `${ query.get ('tags') ?? '' }${ anyFlg ? ' type:or' : '' }`
|
||||||
setSearch (tagsQuery)
|
setSearch (tagsQuery)
|
||||||
}, [location.search])
|
}, [location.search])
|
||||||
|
|
||||||
|
|||||||
新しい課題から参照
ユーザをブロックする