ぼざクリ タグ広場 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.
 
 
 
 
 
 

81 lines
1.6 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 as_json options = { }
  8. self.sha = nil
  9. super options
  10. end
  11. def sha= val
  12. if val.present?
  13. @sha = val
  14. @page = wiki.page("#{ id }.md", @sha)
  15. else
  16. @page = wiki.page("#{ id }.md")
  17. @sha = @page.versions.first.id
  18. end
  19. vers = @page.versions
  20. idx = vers.find_index { |ver| ver.id == @sha }
  21. if idx
  22. @pred = vers[idx + 1]&.id
  23. @succ = idx.positive? ? vers[idx - 1].id : nil
  24. @updated_at = vers[idx].authored_date
  25. else
  26. @sha = nil
  27. @pred = nil
  28. @succ = nil
  29. @updated_at = nil
  30. end
  31. @sha
  32. end
  33. def sha
  34. @sha
  35. end
  36. def pred
  37. @pred
  38. end
  39. def succ
  40. @succ
  41. end
  42. def updated_at
  43. @updated_at
  44. end
  45. def body
  46. sha = nil unless @page
  47. @page&.raw_data&.force_encoding('UTF-8')
  48. end
  49. def set_body content, user:
  50. commit_info = { name: user.id.to_s,
  51. email: 'dummy@example.com' }
  52. page = wiki.page("#{ id }.md")
  53. if page
  54. commit_info[:message] = "Updated #{ id }"
  55. wiki.update_page(page, id.to_s, :markdown, content, commit_info)
  56. else
  57. commit_info[:message] = "Created #{ id }"
  58. wiki.write_page(id.to_s, :markdown, content, commit_info)
  59. end
  60. end
  61. private
  62. WIKI_PATH = Rails.root.join('wiki').to_s
  63. def wiki
  64. @wiki ||= Gollum::Wiki.new(WIKI_PATH)
  65. end
  66. end