Commit 4ae96373 authored by Alex Kalderimis's avatar Alex Kalderimis

Clearer separation of concerns between page and meta

This makes the assumption that a page is valid an error condition, and
moves a long chain of accessors to the page model.
parent 01b0e4cd
...@@ -295,6 +295,10 @@ class WikiPage ...@@ -295,6 +295,10 @@ class WikiPage
'wiki_page' 'wiki_page'
end end
def version_commit_timestamp
version&.commit&.committed_date
end
private private
def serialize_front_matter(hash) def serialize_front_matter(hash)
......
...@@ -5,6 +5,7 @@ class WikiPage ...@@ -5,6 +5,7 @@ class WikiPage
include Gitlab::Utils::StrongMemoize include Gitlab::Utils::StrongMemoize
CanonicalSlugConflictError = Class.new(ActiveRecord::RecordInvalid) CanonicalSlugConflictError = Class.new(ActiveRecord::RecordInvalid)
WikiPageInvalid = Class.new(ArgumentError)
self.table_name = 'wiki_page_meta' self.table_name = 'wiki_page_meta'
...@@ -32,12 +33,13 @@ class WikiPage ...@@ -32,12 +33,13 @@ class WikiPage
# @param [String] last_known_slug # @param [String] last_known_slug
# @param [WikiPage] wiki_page # @param [WikiPage] wiki_page
# #
# As with all `find_or_create` methods, this one raises errors on # This method raises errors on validation issues.
# validation issues.
def find_or_create(last_known_slug, wiki_page) def find_or_create(last_known_slug, wiki_page)
raise WikiPageInvalid unless wiki_page.valid?
project = wiki_page.wiki.project project = wiki_page.wiki.project
known_slugs = [last_known_slug, wiki_page.slug].compact.uniq known_slugs = [last_known_slug, wiki_page.slug].compact.uniq
raise 'no slugs!' if known_slugs.empty? raise 'No slugs found! This should not be possible.' if known_slugs.empty?
transaction do transaction do
updates = wiki_page_updates(wiki_page) updates = wiki_page_updates(wiki_page)
...@@ -70,7 +72,8 @@ class WikiPage ...@@ -70,7 +72,8 @@ class WikiPage
private private
def wiki_page_updates(wiki_page) def wiki_page_updates(wiki_page)
last_commit_date = wiki_page.version.commit.committed_date last_commit_date = wiki_page.version_commit_timestamp || Time.now.utc
{ {
title: wiki_page.title, title: wiki_page.title,
created_at: last_commit_date, created_at: last_commit_date,
......
...@@ -233,6 +233,14 @@ describe WikiPage::Meta do ...@@ -233,6 +233,14 @@ describe WikiPage::Meta do
expect { find_record }.to raise_error(ActiveRecord::RecordInvalid) expect { find_record }.to raise_error(ActiveRecord::RecordInvalid)
end end
end end
context 'the wiki page is not valid' do
let(:wiki_page) { build(:wiki_page, project: project, title: nil) }
it 'raises an error' do
expect { find_record }.to raise_error(described_class::WikiPageInvalid)
end
end
end end
context 'no existing record exists' do context 'no existing record exists' do
......
...@@ -844,6 +844,20 @@ describe WikiPage do ...@@ -844,6 +844,20 @@ describe WikiPage do
end end
end end
describe '#version_commit_timestamp' do
context 'for a new page' do
it 'returns nil' do
expect(new_page.version_commit_timestamp).to be_nil
end
end
context 'for page that exists' do
it 'returns the timestamp of the commit' do
expect(existing_page.version_commit_timestamp).to eq(existing_page.version.commit.committed_date)
end
end
end
private private
def get_slugs(page_or_dir) def get_slugs(page_or_dir)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment