Change: #647 NGワードの入力フォーム (#663)

* Change: #647 NGワードの入力フォーム

* Wip: 画面改造

* テストコード、画面

* Fix: 複数の問題
This commit is contained in:
KMY(雪あすか) 2024-03-26 08:44:16 +09:00 committed by GitHub
parent 0d2b415e26
commit 95ab1f729c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
33 changed files with 526 additions and 172 deletions

View file

@ -0,0 +1,63 @@
# frozen_string_literal: true
class CreateNgWords < ActiveRecord::Migration[7.1]
class Setting < ApplicationRecord
def value
YAML.safe_load(self[:value], permitted_classes: [ActiveSupport::HashWithIndifferentAccess, Symbol]) if self[:value].present?
end
def value=(new_value)
self[:value] = new_value.to_yaml
end
end
class NgWord < ApplicationRecord; end
def normalized_keyword(keyword)
if regexp?(keyword)
keyword[1..]
else
keyword
end
end
def regexp?(keyword)
keyword.start_with?('?') && keyword.size >= 2
end
def up
create_table :ng_words do |t|
t.string :keyword, null: false
t.boolean :regexp, null: false, default: false
t.boolean :stranger, null: false, default: true
t.timestamps
end
settings = Setting.where(var: %i(ng_words ng_words_for_stranger_mention))
ng_words = settings.find { |s| s.var == 'ng_words' }&.value&.compact_blank&.uniq || []
ng_words_for_stranger_mention = settings.find { |s| s.var == 'ng_words_for_stranger_mention' }&.value&.compact_blank&.uniq || []
(ng_words + ng_words_for_stranger_mention).compact.uniq.each do |word|
NgWord.create!(
keyword: normalized_keyword(word),
regexp: regexp?(word),
stranger: ng_words_for_stranger_mention.include?(word)
)
end
settings.destroy_all
end
def down
ng_words = NgWord.where(stranger: false).map { |s| s.regexp ? "?#{s.keyword}" : s.keyword }
ng_words_for_stranger_mention = NgWord.where(stranger: true).map { |s| s.regexp ? "?#{s.keyword}" : s.keyword }
Setting.where(var: %i(ng_words ng_words_for_stranger_mention)).destroy_all
Setting.new(var: :ng_words).tap { |s| s.value = ng_words }.save!
Setting.new(var: :ng_words_for_stranger_mention).tap { |s| s.value = ng_words_for_stranger_mention }.save!
drop_table :ng_words
end
end

View file

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.1].define(version: 2024_03_12_230204) do
ActiveRecord::Schema[7.1].define(version: 2024_03_20_231633) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -926,6 +926,14 @@ ActiveRecord::Schema[7.1].define(version: 2024_03_12_230204) do
t.datetime "updated_at", null: false
end
create_table "ng_words", force: :cascade do |t|
t.string "keyword", null: false
t.boolean "regexp", default: false, null: false
t.boolean "stranger", default: true, null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "ngword_histories", force: :cascade do |t|
t.string "uri", null: false
t.integer "target_type", null: false