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

56 lines
1.6 KiB

  1. class WikiRevision < ApplicationRecord
  2. belongs_to :wiki_page
  3. belongs_to :base_revision, class_name: 'WikiRevision', optional: true
  4. belongs_to :created_user, class_name: 'User'
  5. belongs_to :redirect_page, class_name: 'WikiPage', optional: true
  6. has_many :wiki_revision_lines, dependent: :delete_all
  7. has_many :wiki_lines, through: :wiki_revision_lines
  8. enum :kind, { content: 0, redirect: 1 }
  9. validates :kind, presence: true
  10. validates :lines_count, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
  11. validates :tree_sha256, length: { is: 64 }, allow_nil: true
  12. validate :kind_consistency
  13. def body
  14. return unless content?
  15. wiki_revision_lines
  16. .includes(:wiki_line)
  17. .order(:position)
  18. .map { |rev| rev.wiki_line.body }
  19. .join("\n")
  20. end
  21. private
  22. def kind_consistency
  23. if content?
  24. if tree_sha256.blank?
  25. errors.add(:tree_sha256, '種類がページの場合は必須です.')
  26. end
  27. if redirect_page_id.present?
  28. errors.add(:redirect_page_id, '種類がページの場合は空である必要があります.')
  29. end
  30. end
  31. if redirect?
  32. if redirect_page_id.blank?
  33. errors.add(:redirect_page_id, '種類がリダイレクトの場合は必須です.')
  34. end
  35. if tree_sha256.present?
  36. errors.add(:tree_sha256, '種類がリダイレクトの場合は空である必要があります.')
  37. end
  38. if lines_count.to_i > 0
  39. errors.add(:lines_count, '種類がリダイレクトの場合は 0 である必要があります.')
  40. end
  41. end
  42. end
  43. end