|
- 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
|