ぼざクリ タグ広場 https://hub.nizika.monster
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

64 lines
1.4 KiB

  1. require 'gollum-lib'
  2. class WikiPage < ApplicationRecord
  3. belongs_to :tag, optional: true
  4. belongs_to :created_user, class_name: 'User', foreign_key: 'created_user_id'
  5. belongs_to :updated_user, class_name: 'User', foreign_key: 'updated_user_id'
  6. validates :title, presence: true, length: { maximum: 255 }, uniqueness: true
  7. def sha= val
  8. if val.present?
  9. @sha = val
  10. @page = wiki.page("#{ id }.md", @sha)
  11. else
  12. @page = wiki.page("#{ id }.md")
  13. @sha = @page.versions.first.id
  14. end
  15. vers = @page.versions
  16. idx = vers.find_index { |ver| ver.id == @sha }
  17. @pred = vers[idx + 1]&.id
  18. @succ = idx.positive? ? vers[idx - 1].id : nil
  19. @sha
  20. end
  21. def sha
  22. @sha
  23. end
  24. def pred
  25. @pred
  26. end
  27. def succ
  28. @succ
  29. end
  30. def body
  31. sha = nil unless @page
  32. @page&.raw_data&.force_encoding('UTF-8')
  33. end
  34. def set_body content, user:
  35. commit_info = { name: user.id.to_s,
  36. email: 'dummy@example.com' }
  37. page = wiki.page("#{ id }.md")
  38. if page
  39. commit_info[:message] = "Updated #{ id }"
  40. wiki.update_page(page, id.to_s, :markdown, content, commit_info)
  41. else
  42. commit_info[:message] = "Created #{ id }"
  43. wiki.write_page(id.to_s, :markdown, content, commit_info)
  44. end
  45. end
  46. private
  47. WIKI_PATH = Rails.root.join('wiki').to_s
  48. def wiki
  49. @wiki ||= Gollum::Wiki.new(WIKI_PATH)
  50. end
  51. end