コミットを比較
4 コミット
| 作成者 | SHA1 | 日付 | |
|---|---|---|---|
| 43c4b52280 | |||
| cf9e6b0cf9 | |||
| acd062f289 | |||
| 80659a2b81 |
@@ -6,6 +6,7 @@ class TagImplication < ApplicationRecord
|
||||
validates :parent_tag_id, presence: true
|
||||
|
||||
validate :parent_tag_mustnt_be_itself
|
||||
validate :parent_tag_mustnt_create_cycle
|
||||
|
||||
private
|
||||
|
||||
@@ -14,4 +15,27 @@ class TagImplication < ApplicationRecord
|
||||
errors.add :parent_tag_id, '親タグは子タグと同一であってはなりません.'
|
||||
end
|
||||
end
|
||||
|
||||
def parent_tag_mustnt_create_cycle
|
||||
return if tag_id.blank? || parent_tag_id.blank?
|
||||
return if errors[:parent_tag_id].present?
|
||||
|
||||
seen = { }
|
||||
stack = [parent_tag_id]
|
||||
|
||||
until stack.empty?
|
||||
current_id = stack.pop
|
||||
next if seen[current_id]
|
||||
|
||||
seen[current_id] = true
|
||||
|
||||
if current_id == tag_id
|
||||
errors.add :parent_tag_id, '親タグに子孫タグを指定すると循環します.'
|
||||
errors.add :base, 'タグの親子関係が循環します.'
|
||||
return
|
||||
end
|
||||
|
||||
stack.concat(TagImplication.where(tag_id: current_id).pluck(:parent_tag_id))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe TagImplication, type: :model do
|
||||
it 'rejects a parent tag that would create a cycle' do
|
||||
child = create(:tag, name: 'tag_implication_cycle_child')
|
||||
parent = create(:tag, name: 'tag_implication_cycle_parent')
|
||||
|
||||
described_class.create!(tag: child, parent_tag: parent)
|
||||
|
||||
implication = described_class.new(tag: parent, parent_tag: child)
|
||||
|
||||
expect(implication).not_to be_valid
|
||||
expect(implication.errors[:parent_tag_id]).to include(
|
||||
'親タグに子孫タグを指定すると循環します.'
|
||||
)
|
||||
expect(implication.errors[:base]).to be_present
|
||||
end
|
||||
|
||||
it 'terminates even when existing data already contains a cycle' do
|
||||
child = create(:tag, name: 'tag_implication_existing_cycle_child')
|
||||
parent = create(:tag, name: 'tag_implication_existing_cycle_parent')
|
||||
ancestor = create(:tag, name: 'tag_implication_existing_cycle_ancestor')
|
||||
|
||||
described_class.create!(tag: parent, parent_tag: ancestor)
|
||||
described_class.insert_all!(
|
||||
[
|
||||
{ tag_id: ancestor.id, parent_tag_id: parent.id,
|
||||
created_at: Time.current, updated_at: Time.current }
|
||||
]
|
||||
)
|
||||
|
||||
implication = described_class.new(tag: child, parent_tag: parent)
|
||||
|
||||
expect(implication).to be_valid
|
||||
end
|
||||
end
|
||||
@@ -54,7 +54,17 @@ RSpec.describe Tag, type: :model do
|
||||
first = create(:tag, name: 'expand_cycle_first')
|
||||
second = create(:tag, name: 'expand_cycle_second')
|
||||
TagImplication.create!(tag: first, parent_tag: second)
|
||||
TagImplication.create!(tag: second, parent_tag: first)
|
||||
now = Time.current
|
||||
TagImplication.insert_all!(
|
||||
[
|
||||
{
|
||||
tag_id: second.id,
|
||||
parent_tag_id: first.id,
|
||||
created_at: now,
|
||||
updated_at: now
|
||||
}
|
||||
]
|
||||
)
|
||||
|
||||
expect(described_class.expand_parent_tags([first])).to contain_exactly(first, second)
|
||||
end
|
||||
|
||||
@@ -56,6 +56,17 @@ RSpec.describe "TagChildren", type: :request do
|
||||
|
||||
expect(response).to have_http_status(:no_content)
|
||||
end
|
||||
|
||||
it 'returns 422 and does not create relation when the new link makes a cycle' do
|
||||
TagImplication.create!(tag: parent, parent_tag: child)
|
||||
|
||||
expect {
|
||||
do_request
|
||||
}.not_to change(TagImplication, :count)
|
||||
|
||||
expect(response).to have_http_status(:unprocessable_entity)
|
||||
expect(TagImplication.where(tag: child, parent_tag: parent)).not_to exist
|
||||
end
|
||||
end
|
||||
|
||||
context "when Tag.find raises (invalid ids)" do
|
||||
|
||||
@@ -797,7 +797,17 @@ RSpec.describe 'Tags API', type: :request do
|
||||
)
|
||||
TagImplication.create!(tag: first, parent_tag: root_material)
|
||||
TagImplication.create!(tag: second, parent_tag: first)
|
||||
TagImplication.create!(tag: first, parent_tag: second)
|
||||
now = Time.current
|
||||
TagImplication.insert_all!(
|
||||
[
|
||||
{
|
||||
tag_id: first.id,
|
||||
parent_tag_id: second.id,
|
||||
created_at: now,
|
||||
updated_at: now
|
||||
}
|
||||
]
|
||||
)
|
||||
|
||||
get '/tags/with-depth', params: { parent: root_material.id }
|
||||
|
||||
@@ -1364,8 +1374,6 @@ RSpec.describe 'Tags API', type: :request do
|
||||
end
|
||||
|
||||
it 'parent_tags に指定すると循環する tag は 422 にする' do
|
||||
pending '#332 で対応予定'
|
||||
|
||||
child = Tag.create!(
|
||||
tag_name: TagName.create!(name: 'put_cycle_child'),
|
||||
category: :general
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
import { act, fireEvent, screen, waitFor } from '@testing-library/react'
|
||||
import { act, fireEvent, screen, waitFor, within } from '@testing-library/react'
|
||||
import { Route, Routes } from 'react-router-dom'
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
|
||||
import TheatreDetailPage from '@/pages/theatres/TheatreDetailPage'
|
||||
import { buildPost,
|
||||
buildTag,
|
||||
buildTheatre,
|
||||
buildTheatreComment,
|
||||
buildTheatreInfo,
|
||||
@@ -96,6 +97,9 @@ const renderPage = (user = buildUser ({ id: 1, role: 'member' })) =>
|
||||
{ route: '/theatres/7' },
|
||||
)
|
||||
|
||||
const tagSection = (): HTMLElement =>
|
||||
screen.getAllByRole ('heading', { name: 'タグ' })[0].closest ('section')!
|
||||
|
||||
const mockDefaultApi = () => {
|
||||
api.apiGet.mockImplementation ((path: string) => {
|
||||
switch (path)
|
||||
@@ -245,6 +249,53 @@ describe ('TheatreDetailPage', () => {
|
||||
expect (postEmbed.seek).not.toHaveBeenCalledWith (0)
|
||||
})
|
||||
|
||||
it ('shows child tags from the post tag tree in both vertical and horizontal layouts', async () => {
|
||||
const childTag = buildTag ({ id: 12, name: '子タグ', category: 'general' })
|
||||
const parentTag = buildTag ({
|
||||
id: 11,
|
||||
name: '親タグ',
|
||||
category: 'general',
|
||||
children: [childTag],
|
||||
})
|
||||
const duplicateParentTag = buildTag ({
|
||||
id: 13,
|
||||
name: '別親タグ',
|
||||
category: 'general',
|
||||
children: [childTag],
|
||||
})
|
||||
|
||||
postsApi.fetchPost.mockResolvedValueOnce (buildPost ({
|
||||
...currentPost,
|
||||
tags: [parentTag, duplicateParentTag],
|
||||
}))
|
||||
|
||||
renderPage ()
|
||||
|
||||
await screen.findByText ('Embed:上映中の投稿')
|
||||
|
||||
expect (within (tagSection ()).getByRole ('link', { name: '親タグ' }))
|
||||
.toBeInTheDocument ()
|
||||
expect (within (tagSection ()).getByRole ('link', { name: '別親タグ' }))
|
||||
.toBeInTheDocument ()
|
||||
expect (within (tagSection ()).getAllByRole ('link', { name: '子タグ' })[0])
|
||||
.toBeInTheDocument ()
|
||||
expect (within (tagSection ()).getAllByText ('↳').length).toBeGreaterThan (0)
|
||||
|
||||
fireEvent.click (screen.getByRole ('button', { name: '2 列 A 型' }))
|
||||
fireEvent.click (screen.getAllByRole ('button', { name: '横並び' })[0])
|
||||
|
||||
await waitFor (() => {
|
||||
expect (within (tagSection ()).getByRole ('link', { name: '親タグ' }))
|
||||
.toBeInTheDocument ()
|
||||
expect (within (tagSection ()).getByRole ('link', { name: '別親タグ' }))
|
||||
.toBeInTheDocument ()
|
||||
expect (within (tagSection ()).getByRole ('link', { name: '子タグ' }))
|
||||
.toBeInTheDocument ()
|
||||
expect (within (tagSection ()).getAllByRole ('link', { name: '子タグ' }))
|
||||
.toHaveLength (1)
|
||||
})
|
||||
})
|
||||
|
||||
it ('does not advance host post while video length is unknown', async () => {
|
||||
api.apiPut.mockImplementation ((path: string) => {
|
||||
switch (path)
|
||||
|
||||
@@ -102,6 +102,28 @@ const compareTagName = (a: Tag, b: Tag): number =>
|
||||
a.name === b.name ? 0 : (a.name < b.name ? -1 : 1)
|
||||
|
||||
|
||||
const flattenTags = (tags: Tag[]): Tag[] => {
|
||||
const flattened: Tag[] = []
|
||||
const seen = new Set<number> ()
|
||||
|
||||
const visit = (tag: Tag) => {
|
||||
if (seen.has (tag.id))
|
||||
return
|
||||
|
||||
seen.add (tag.id)
|
||||
flattened.push (tag)
|
||||
|
||||
for (const child of tag.children ?? [])
|
||||
visit (child)
|
||||
}
|
||||
|
||||
for (const tag of tags)
|
||||
visit (tag)
|
||||
|
||||
return flattened
|
||||
}
|
||||
|
||||
|
||||
const tagsByCategory = (tags: Tag[]): Partial<Record<Category, Tag[]>> => {
|
||||
const grouped: Partial<Record<Category, Tag[]>> = { }
|
||||
|
||||
@@ -118,15 +140,39 @@ const tagsByCategory = (tags: Tag[]): Partial<Record<Category, Tag[]>> => {
|
||||
}
|
||||
|
||||
|
||||
const renderReadonlyTagTree = (
|
||||
tag: Tag,
|
||||
nestLevel: number,
|
||||
path: string,
|
||||
ancestors: Set<number> = new Set<number> (),
|
||||
): ReactNode[] => {
|
||||
const key = `${ path }-${ tag.id }`
|
||||
const nextAncestors = new Set (ancestors)
|
||||
nextAncestors.add (tag.id)
|
||||
|
||||
return [
|
||||
(
|
||||
<li key={key} className="text-left leading-tight">
|
||||
<TagLink tag={tag} nestLevel={nestLevel} withCount={false}/>
|
||||
</li>),
|
||||
...((tag.children ?? [])
|
||||
.filter (child => !(nextAncestors.has (child.id)))
|
||||
.sort (compareTagName)
|
||||
.flatMap (child =>
|
||||
renderReadonlyTagTree (child, nestLevel + 1, key, nextAncestors)))]
|
||||
}
|
||||
|
||||
|
||||
const TagList: FC<{ tags: Tag[]; compact?: boolean; flow?: TagFlow }> = (
|
||||
{ tags, compact, flow = 'vertical' }) => {
|
||||
const grouped = tagsByCategory (tags)
|
||||
const horizontalGrouped = tagsByCategory (flattenTags (tags))
|
||||
const verticalGrouped = tagsByCategory (tags)
|
||||
|
||||
if (flow === 'horizontal')
|
||||
{
|
||||
return (
|
||||
<ul className={cn ('flex flex-wrap gap-x-3 gap-y-1', compact && 'text-sm')}>
|
||||
{CATEGORIES.flatMap (cat => grouped[cat] ?? []).map (tag => (
|
||||
{CATEGORIES.flatMap (cat => horizontalGrouped[cat] ?? []).map (tag => (
|
||||
<li key={tag.id} className="text-left leading-tight">
|
||||
<TagLink tag={tag} withCount={false}/>
|
||||
</li>))}
|
||||
@@ -136,7 +182,7 @@ const TagList: FC<{ tags: Tag[]; compact?: boolean; flow?: TagFlow }> = (
|
||||
return (
|
||||
<div className="space-y-3">
|
||||
{CATEGORIES.map (cat => {
|
||||
const rows = grouped[cat] ?? []
|
||||
const rows = verticalGrouped[cat] ?? []
|
||||
if (rows.length === 0)
|
||||
return null
|
||||
|
||||
@@ -146,10 +192,8 @@ const TagList: FC<{ tags: Tag[]; compact?: boolean; flow?: TagFlow }> = (
|
||||
{CATEGORY_NAMES[cat]}
|
||||
</div>
|
||||
<ul className={cn ('space-y-1', compact && 'text-sm')}>
|
||||
{rows.map (tag => (
|
||||
<li key={tag.id} className="text-left leading-tight">
|
||||
<TagLink tag={tag} withCount={false}/>
|
||||
</li>))}
|
||||
{rows.flatMap (tag =>
|
||||
renderReadonlyTagTree (tag, 0, `cat-${ cat }`))}
|
||||
</ul>
|
||||
</div>)
|
||||
})}
|
||||
|
||||
新しい課題から参照
ユーザをブロックする