feat: Wiki の管理方法変更(#188) (#195)
Merge branch 'feature/188' of https://git.miteruzo.com/miteruzo/btrc-hub into feature/188 #188 Merge branch 'main' into feature/188 #188 #188 #188 #188 Co-authored-by: miteruzo <miteruzo@naver.com> Reviewed-on: #195
This commit was merged in pull request #195.
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
class WikiLine < ApplicationRecord
|
||||
has_many :wiki_revision_lines, dependent: :restrict_with_exception
|
||||
|
||||
validates :sha256, presence: true, uniqueness: true, length: { is: 64 }
|
||||
validates :body, presence: true
|
||||
|
||||
def self.upsert_by_body! body
|
||||
sha = Digest::SHA256.hexdigest(body)
|
||||
now = Time.current
|
||||
|
||||
upsert({ sha256: sha, body:, created_at: now, updated_at: now })
|
||||
|
||||
find_by!(sha256: sha)
|
||||
end
|
||||
end
|
||||
@@ -1,80 +1,50 @@
|
||||
require 'gollum-lib'
|
||||
require 'set'
|
||||
|
||||
|
||||
class WikiPage < ApplicationRecord
|
||||
belongs_to :tag, optional: true
|
||||
belongs_to :created_user, class_name: 'User', foreign_key: 'created_user_id'
|
||||
belongs_to :updated_user, class_name: 'User', foreign_key: 'updated_user_id'
|
||||
has_many :wiki_revisions, dependent: :destroy
|
||||
belongs_to :created_user, class_name: 'User'
|
||||
belongs_to :updated_user, class_name: 'User'
|
||||
|
||||
has_many :redirected_from_revisions,
|
||||
class_name: 'WikiRevision',
|
||||
foreign_key: :redirect_page_id,
|
||||
dependent: :nullify
|
||||
|
||||
validates :title, presence: true, length: { maximum: 255 }, uniqueness: true
|
||||
|
||||
def as_json options = { }
|
||||
self.sha = nil
|
||||
super options
|
||||
end
|
||||
|
||||
def sha= val
|
||||
if val.present?
|
||||
@sha = val
|
||||
@page = wiki.page("#{ id }.md", @sha)
|
||||
else
|
||||
@page = wiki.page("#{ id }.md")
|
||||
@sha = @page.versions.first.id
|
||||
end
|
||||
vers = @page.versions
|
||||
idx = vers.find_index { |ver| ver.id == @sha }
|
||||
if idx
|
||||
@pred = vers[idx + 1]&.id
|
||||
@succ = idx.positive? ? vers[idx - 1].id : nil
|
||||
@updated_at = vers[idx].authored_date
|
||||
else
|
||||
@sha = nil
|
||||
@pred = nil
|
||||
@succ = nil
|
||||
@updated_at = nil
|
||||
end
|
||||
@sha
|
||||
end
|
||||
|
||||
def sha
|
||||
@sha
|
||||
end
|
||||
|
||||
def pred
|
||||
@pred
|
||||
end
|
||||
|
||||
def succ
|
||||
@succ
|
||||
end
|
||||
|
||||
def updated_at
|
||||
@updated_at
|
||||
def current_revision
|
||||
wiki_revisions.order(id: :desc).first
|
||||
end
|
||||
|
||||
def body
|
||||
sha = nil unless @page
|
||||
@page&.raw_data&.force_encoding('UTF-8')
|
||||
rev = current_revision
|
||||
rev.body if rev&.content?
|
||||
end
|
||||
|
||||
def set_body content, user:
|
||||
commit_info = { name: user.id.to_s,
|
||||
email: 'dummy@example.com' }
|
||||
page = wiki.page("#{ id }.md")
|
||||
if page
|
||||
commit_info[:message] = "Updated #{ id }"
|
||||
wiki.update_page(page, id.to_s, :markdown, content, commit_info)
|
||||
else
|
||||
commit_info[:message] = "Created #{ id }"
|
||||
wiki.write_page(id.to_s, :markdown, content, commit_info)
|
||||
def resolve_redirect limit: 10
|
||||
page = self
|
||||
visited = Set.new
|
||||
|
||||
limit.times do
|
||||
return page if visited.include?(page.id)
|
||||
|
||||
visited.add(page.id)
|
||||
|
||||
rev = page.current_revision
|
||||
return page if !(rev&.redirect?) || !(rev.redirect_page)
|
||||
|
||||
page = rev.redirect_page
|
||||
end
|
||||
|
||||
page
|
||||
end
|
||||
|
||||
private
|
||||
def pred_revision_id revision_id
|
||||
wiki_revisions.where('id < ?', revision_id).order(id: :desc).limit(1).pick(:id)
|
||||
end
|
||||
|
||||
WIKI_PATH = Rails.root.join('wiki').to_s
|
||||
|
||||
def wiki
|
||||
@wiki ||= Gollum::Wiki.new(WIKI_PATH)
|
||||
def succ_revision_id revision_id
|
||||
wiki_revisions.where('id > ?', revision_id).order(id: :asc).limit(1).pick(:id)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
class WikiRevision < ApplicationRecord
|
||||
belongs_to :wiki_page
|
||||
belongs_to :base_revision, class_name: 'WikiRevision', optional: true
|
||||
belongs_to :created_user, class_name: 'User'
|
||||
belongs_to :redirect_page, class_name: 'WikiPage', optional: true
|
||||
|
||||
has_many :wiki_revision_lines, dependent: :delete_all
|
||||
has_many :wiki_lines, through: :wiki_revision_lines
|
||||
|
||||
enum :kind, { content: 0, redirect: 1 }
|
||||
|
||||
validates :kind, presence: true
|
||||
validates :lines_count, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
|
||||
validates :tree_sha256, length: { is: 64 }, allow_nil: true
|
||||
|
||||
validate :kind_consistency
|
||||
|
||||
def body
|
||||
return unless content?
|
||||
|
||||
wiki_revision_lines
|
||||
.includes(:wiki_line)
|
||||
.order(:position)
|
||||
.map { |rev| rev.wiki_line.body }
|
||||
.join("\n")
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def kind_consistency
|
||||
if content?
|
||||
if tree_sha256.blank?
|
||||
errors.add(:tree_sha256, '種類がページの場合は必須です.')
|
||||
end
|
||||
|
||||
if redirect_page_id.present?
|
||||
errors.add(:redirect_page_id, '種類がページの場合は空である必要があります.')
|
||||
end
|
||||
end
|
||||
|
||||
if redirect?
|
||||
if redirect_page_id.blank?
|
||||
errors.add(:redirect_page_id, '種類がリダイレクトの場合は必須です.')
|
||||
end
|
||||
|
||||
if tree_sha256.present?
|
||||
errors.add(:tree_sha256, '種類がリダイレクトの場合は空である必要があります.')
|
||||
end
|
||||
|
||||
if lines_count.to_i > 0
|
||||
errors.add(:lines_count, '種類がリダイレクトの場合は 0 である必要があります.')
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,8 @@
|
||||
class WikiRevisionLine < ApplicationRecord
|
||||
belongs_to :wiki_revision
|
||||
belongs_to :wiki_line
|
||||
|
||||
validates :position, presence: true,
|
||||
numericality: { only_integer: true, greater_than_or_equal_to: 0 }
|
||||
validates :position, uniqueness: { scope: :wiki_revision_id }
|
||||
end
|
||||
Reference in New Issue
Block a user