This commit is contained in:
@@ -302,6 +302,11 @@ class TagsController < ApplicationController
|
|||||||
NicoTagVersionRecorder.record!(tag:, event_type:, created_by_user:)
|
NicoTagVersionRecorder.record!(tag:, event_type:, created_by_user:)
|
||||||
else
|
else
|
||||||
TagVersionRecorder.record!(tag:, event_type:, created_by_user:)
|
TagVersionRecorder.record!(tag:, event_type:, created_by_user:)
|
||||||
|
if tag.has_wiki
|
||||||
|
WikiVersionRecorder.record!(page: tag.tag_name.wiki_page,
|
||||||
|
event_type: :update,
|
||||||
|
created_by_user:)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -13,6 +13,8 @@ class WikiPage < ApplicationRecord
|
|||||||
foreign_key: :redirect_page_id,
|
foreign_key: :redirect_page_id,
|
||||||
dependent: :nullify
|
dependent: :nullify
|
||||||
|
|
||||||
|
has_many :wiki_versions
|
||||||
|
|
||||||
belongs_to :tag_name
|
belongs_to :tag_name
|
||||||
validates :tag_name, presence: true
|
validates :tag_name, presence: true
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
class WikiVersion < ApplicationRecord
|
||||||
|
include VersionRecord
|
||||||
|
|
||||||
|
belongs_to :wiki_page
|
||||||
|
|
||||||
|
validates :title, presence: true
|
||||||
|
validates :body, presence: true
|
||||||
|
end
|
||||||
@@ -21,7 +21,7 @@ module Wiki
|
|||||||
end
|
end
|
||||||
|
|
||||||
def content! body:, message:, base_revision_id:
|
def content! body:, message:, base_revision_id:
|
||||||
normalised = normalise_body(body)
|
normalised = normalise_body(body).gsub(/\n+$/, '')
|
||||||
lines = split_lines(normalised)
|
lines = split_lines(normalised)
|
||||||
|
|
||||||
line_shas = lines.map { |line| Digest::SHA256.hexdigest(line) }
|
line_shas = lines.map { |line| Digest::SHA256.hexdigest(line) }
|
||||||
@@ -37,10 +37,22 @@ module Wiki
|
|||||||
current_id = @page.wiki_revisions.maximum(:id)
|
current_id = @page.wiki_revisions.maximum(:id)
|
||||||
if current_id && current_id != base_revision_id.to_i
|
if current_id && current_id != base_revision_id.to_i
|
||||||
raise Conflict,
|
raise Conflict,
|
||||||
"競合が発生してゐます(現在の Id.:#{ current_id },ベース Id.:#{ base_revision_id })."
|
"競合が発生してゐます" +
|
||||||
|
"(現在の Id.:#{ current_id },ベース Id.:#{ base_revision_id })."
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@page.update!(body: normalised)
|
||||||
|
|
||||||
|
WikiVersionRecorder.record!(
|
||||||
|
page: { title: @page.title, body: normalised },
|
||||||
|
event_type: @page.wiki_versions.exists? ? :update : :create,
|
||||||
|
created_by_user: @created_user)
|
||||||
|
tag = @page.tag_name.tag
|
||||||
|
if tag
|
||||||
|
TagVersionRecorder.record!(tag:, event_type: :update, created_by_user: @created_user)
|
||||||
|
end
|
||||||
|
|
||||||
rev = WikiRevision.create!(
|
rev = WikiRevision.create!(
|
||||||
wiki_page: @page,
|
wiki_page: @page,
|
||||||
base_revision_id:,
|
base_revision_id:,
|
||||||
@@ -54,47 +66,23 @@ module Wiki
|
|||||||
rows = line_ids.each_with_index.map do |line_id, pos|
|
rows = line_ids.each_with_index.map do |line_id, pos|
|
||||||
{ wiki_revision_id: rev.id, wiki_line_id: line_id, position: pos }
|
{ wiki_revision_id: rev.id, wiki_line_id: line_id, position: pos }
|
||||||
end
|
end
|
||||||
WikiRevisionLine.insert_all!(rows)
|
WikiRevisionLine.insert_all!(rows) if rows.any?
|
||||||
|
|
||||||
rev
|
rev
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def redirect! redirect_page:, message:, base_revision_id:
|
def redirect!(redirect_page:, message:, base_revision_id:) = raise '廃止しました.'
|
||||||
ActiveRecord::Base.transaction do
|
|
||||||
@page.lock!
|
|
||||||
|
|
||||||
if base_revision_id.present?
|
|
||||||
current_id = @page.wiki_revisions.maximum(:id)
|
|
||||||
if current_id && current_id != base_revision_id.to_i
|
|
||||||
raise Conflict,
|
|
||||||
"競合が発生してゐます(現在の Id.:#{ current_id },ベース Id.:#{ base_revision_id })."
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
WikiRevision.create!(
|
|
||||||
wiki_page: @page,
|
|
||||||
base_revision_id:,
|
|
||||||
created_user: @created_user,
|
|
||||||
kind: :redirect,
|
|
||||||
redirect_page:,
|
|
||||||
message:,
|
|
||||||
lines_count: 0,
|
|
||||||
tree_sha256: nil)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def normalise_body body
|
def normalise_body body
|
||||||
s = body.to_s
|
s = body.to_s
|
||||||
s.gsub!("\r\n", "\n")
|
s.gsub!(/\r\n?/, "\n")
|
||||||
s.encode('UTF-8', invalid: :replace, undef: :replace, replace: '🖕')
|
s.encode('UTF-8', invalid: :replace, undef: :replace, replace: '🖕')
|
||||||
end
|
end
|
||||||
|
|
||||||
def split_lines body
|
def split_lines(body) = body.split("\n")
|
||||||
body.split("\n")
|
|
||||||
end
|
|
||||||
|
|
||||||
def upsert_lines! lines, line_shas
|
def upsert_lines! lines, line_shas
|
||||||
now = Time.current
|
now = Time.current
|
||||||
|
|||||||
@@ -0,0 +1,17 @@
|
|||||||
|
class WikiVersionRecorder < VersionRecorder
|
||||||
|
def self.record! page:, event_type:, created_by_user:
|
||||||
|
new(page:, event_type:, created_by_user:).record!
|
||||||
|
end
|
||||||
|
|
||||||
|
def initialize page:, event_type:, created_by_user:
|
||||||
|
super(record: page, event_type:, created_by_user:)
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def version_class = WikiVersion
|
||||||
|
def version_association = :wiki_versions
|
||||||
|
def record_key = :wiki_page
|
||||||
|
|
||||||
|
def snapshot_attributes = { title: @record.title, body: @record.body }
|
||||||
|
end
|
||||||
@@ -27,7 +27,7 @@ class CreateWikiVersions < ActiveRecord::Migration[8.0]
|
|||||||
add_column :wiki_pages, :body, :text, after: :tag_name_id
|
add_column :wiki_pages, :body, :text, after: :tag_name_id
|
||||||
|
|
||||||
create_table :wiki_versions do |t|
|
create_table :wiki_versions do |t|
|
||||||
t.references :wiki_page , null: false, foreign_key: true
|
t.references :wiki_page, null: false, foreign_key: true
|
||||||
t.integer :version_no, null: false
|
t.integer :version_no, null: false
|
||||||
t.string :event_type, null: false
|
t.string :event_type, null: false
|
||||||
t.string :title, null: false
|
t.string :title, null: false
|
||||||
|
|||||||
Reference in New Issue
Block a user