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

69 lines
1.5 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. @updated_at = vers[idx].authored_date
  20. @sha
  21. end
  22. def sha
  23. @sha
  24. end
  25. def pred
  26. @pred
  27. end
  28. def succ
  29. @succ
  30. end
  31. def updated_at
  32. @updated_at
  33. end
  34. def body
  35. sha = nil unless @page
  36. @page&.raw_data&.force_encoding('UTF-8')
  37. end
  38. def set_body content, user:
  39. commit_info = { name: user.id.to_s,
  40. email: 'dummy@example.com' }
  41. page = wiki.page("#{ id }.md")
  42. if page
  43. commit_info[:message] = "Updated #{ id }"
  44. wiki.update_page(page, id.to_s, :markdown, content, commit_info)
  45. else
  46. commit_info[:message] = "Created #{ id }"
  47. wiki.write_page(id.to_s, :markdown, content, commit_info)
  48. end
  49. end
  50. private
  51. WIKI_PATH = Rails.root.join('wiki').to_s
  52. def wiki
  53. @wiki ||= Gollum::Wiki.new(WIKI_PATH)
  54. end
  55. end