Browse Source

#281

feature/281
みてるぞ 1 week ago
parent
commit
60c8c63353
7 changed files with 35 additions and 17 deletions
  1. +1
    -1
      backend/app/controllers/wiki_pages_controller.rb
  2. +20
    -0
      backend/app/models/my_discard.rb
  3. +3
    -3
      backend/app/models/tag.rb
  4. +2
    -6
      backend/app/models/tag_name.rb
  5. +1
    -1
      backend/app/models/wiki_page.rb
  6. +6
    -4
      backend/spec/requests/posts_spec.rb
  7. +2
    -2
      backend/spec/tasks/nico_sync_spec.rb

+ 1
- 1
backend/app/controllers/wiki_pages_controller.rb View File

@@ -90,7 +90,7 @@ class WikiPagesController < ApplicationController

return head :unprocessable_entity if name.blank? || body.blank?

tag_name = TagName.find_or_create_by!(name:)
tag_name = TagName.find_undiscard_or_create_by!(name:)
page = WikiPage.new(tag_name:, created_user: current_user, updated_user: current_user)
if page.save
message = params[:message].presence


+ 20
- 0
backend/app/models/my_discard.rb View File

@@ -0,0 +1,20 @@
module MyDiscard
extend ActiveSupport::Concern

included { include Discard::Model }

class_methods do
def find_undiscard_or_create_by! attrs, &block
record = with_discarded.find_by(attrs)

if record&.discarded?
record.undiscard!
record.update_columns(created_at: record.reload.updated_at)
end

record or create!(attrs, &block)
rescue ActiveRecord::RecordNotUnique
retry
end
end
end

+ 3
- 3
backend/app/models/tag.rb View File

@@ -1,5 +1,5 @@
class Tag < ApplicationRecord
include Discard::Model
include MyDiscard

class NicoTagNormalisationError < ArgumentError
;
@@ -134,10 +134,10 @@ class Tag < ApplicationRecord
end

def self.find_or_create_by_tag_name! name, category:
tn = TagName.find_or_create_by!(name: name.to_s.strip)
tn = TagName.find_undiscard_or_create_by!(name: name.to_s.strip)
tn = tn.canonical if tn.canonical_id?

Tag.find_or_create_by!(tag_name_id: tn.id) do |t|
Tag.find_undiscard_or_create_by!(tag_name_id: tn.id) do |t|
t.category = category
end
rescue ActiveRecord::RecordNotUnique


+ 2
- 6
backend/app/models/tag_name.rb View File

@@ -1,5 +1,5 @@
class TagName < ApplicationRecord
include Discard::Model
include MyDiscard

default_scope -> { kept }

@@ -27,10 +27,6 @@ class TagName < ApplicationRecord

private

def sanitise_name
self.name = TagNameSanitisationRule.sanitise(name)
end

def canonical_must_be_canonical
if canonical&.canonical_id?
errors.add :canonical, 'canonical は実体を示す必要があります.'
@@ -50,7 +46,7 @@ class TagName < ApplicationRecord
end

def name_must_be_sanitised
if name != TagNameSanitisationRule.sanitise(name)
if name? && name != TagNameSanitisationRule.sanitise(name)
errors.add :name, '名前に使用できない文字が含まれてゐます.'
end
end


+ 1
- 1
backend/app/models/wiki_page.rb View File

@@ -2,7 +2,7 @@ require 'set'


class WikiPage < ApplicationRecord
include Discard::Model
include MyDiscard

default_scope -> { kept }



+ 6
- 4
backend/spec/requests/posts_spec.rb View File

@@ -525,8 +525,9 @@ RSpec.describe 'Posts API', type: :request do

context "when nico tag already exists in tags" do
before do
Tag.find_or_create_by!(tag_name: TagName.find_or_create_by!(name: 'nico:nico_tag'),
category: :nico)
Tag.find_undiscard_or_create_by!(
tag_name: TagName.find_undiscard_or_create_by!(name: 'nico:nico_tag'),
category: :nico)
end

it 'return 400' do
@@ -610,8 +611,9 @@ RSpec.describe 'Posts API', type: :request do

context "when nico tag already exists in tags" do
before do
Tag.find_or_create_by!(tag_name: TagName.find_or_create_by!(name: 'nico:nico_tag'),
category: :nico)
Tag.find_undiscard_or_create_by!(
tag_name: TagName.find_undiscard_or_create_by!(name: 'nico:nico_tag'),
category: :nico)
end

it 'return 400' do


+ 2
- 2
backend/spec/tasks/nico_sync_spec.rb View File

@@ -8,8 +8,8 @@ RSpec.describe "nico:sync" do
end

def create_tag!(name, category:)
tn = TagName.find_or_create_by!(name: name.to_s.strip)
Tag.find_or_create_by!(tag_name_id: tn.id) { |t| t.category = category }
tn = TagName.find_undiscard_or_create_by!(name: name.to_s.strip)
Tag.find_undiscard_or_create_by!(tag_name_id: tn.id) { |t| t.category = category }
end

def link_nico_to_tag!(nico_tag, tag)


Loading…
Cancel
Save