Commit 29be4e0f authored by wendy0402's avatar wendy0402

Allow wiki pages to be renamed in the UI

parent 43c01547
...@@ -113,10 +113,10 @@ class ProjectWiki ...@@ -113,10 +113,10 @@ class ProjectWiki
return false return false
end end
def update_page(page, content, format = :markdown, message = nil) def update_page(page, content:, title: nil, format: :markdown, message: nil)
commit = commit_details(:updated, message, page.title) commit = commit_details(:updated, message, page.title)
wiki.update_page(page, page.name, format.to_sym, content, commit) wiki.update_page(page, title || page.name, format.to_sym, content, commit)
update_project_activity update_project_activity
end end
......
...@@ -180,31 +180,50 @@ class WikiPage ...@@ -180,31 +180,50 @@ class WikiPage
# #
# Returns the String SHA1 of the newly created page # Returns the String SHA1 of the newly created page
# or False if the save was unsuccessful. # or False if the save was unsuccessful.
def create(attr = {}) def create(attrs = {})
@attributes.merge!(attr) @attributes.merge!(attrs)
save :create_page, title, content, format, message save(page_details: title) do
wiki.create_page(title, content, format, message)
end
end end
# Updates an existing Wiki Page, creating a new version. # Updates an existing Wiki Page, creating a new version.
# #
# new_content - The raw markup content to replace the existing. # attrs - Hash of attributes to be updated on the page.
# format - Optional symbol representing the content format. # :content - The raw markup content to replace the existing.
# See ProjectWiki::MARKUPS Hash for available formats. # :format - Optional symbol representing the content format.
# message - Optional commit message to set on the new version. # See ProjectWiki::MARKUPS Hash for available formats.
# last_commit_sha - Optional last commit sha to validate the page unchanged. # :message - Optional commit message to set on the new version.
# :last_commit_sha - Optional last commit sha to validate the page unchanged.
# :title - The Title to replace existing title
# #
# Returns the String SHA1 of the newly created page # Returns the String SHA1 of the newly created page
# or False if the save was unsuccessful. # or False if the save was unsuccessful.
def update(new_content, format: :markdown, message: nil, last_commit_sha: nil) def update(attrs = {})
@attributes[:content] = new_content last_commit_sha = attrs.delete(:last_commit_sha)
@attributes[:format] = format
if last_commit_sha && last_commit_sha != self.last_commit_sha if last_commit_sha && last_commit_sha != self.last_commit_sha
raise PageChangedError.new("You are attempting to update a page that has changed since you started editing it.") raise PageChangedError.new("You are attempting to update a page that has changed since you started editing it.")
end end
save :update_page, @page, content, format, message attrs.slice!(:content, :format, :message, :title)
@attributes.merge!(attrs)
page_details =
if title.present? && @page.title != title
title
else
@page.url_path
end
save(page_details: page_details) do
wiki.update_page(
@page,
content: content,
format: format,
message: attrs[:message],
title: title
)
end
end end
# Destroys the Wiki Page. # Destroys the Wiki Page.
...@@ -236,30 +255,19 @@ class WikiPage ...@@ -236,30 +255,19 @@ class WikiPage
attributes[:format] = @page.format attributes[:format] = @page.format
end end
def save(method, *args) def save(page_details:)
saved = false return unless valid?
project_wiki = wiki unless yield
if valid? && project_wiki.send(method, *args) errors.add(:base, wiki.error_message)
return false
page_details = if method == :update_page end
# Use url_path instead of path to omit format extension
@page.url_path
else
title
end
page_title, page_dir = project_wiki.page_title_and_dir(page_details)
gollum_wiki = project_wiki.wiki
@page = gollum_wiki.paged(page_title, page_dir)
set_attributes page_title, page_dir = wiki.page_title_and_dir(page_details)
gollum_wiki = wiki.wiki
@page = gollum_wiki.paged(page_title, page_dir)
@persisted = true set_attributes
saved = true @persisted = errors.blank?
else
errors.add(:base, project_wiki.error_message) if project_wiki.error_message
end
saved
end end
end end
module WikiPages module WikiPages
class UpdateService < WikiPages::BaseService class UpdateService < WikiPages::BaseService
def execute(page) def execute(page)
if page.update(@params[:content], format: @params[:format], message: @params[:message], last_commit_sha: @params[:last_commit_sha]) if page.update(@params)
execute_hooks(page, 'update') execute_hooks(page, 'update')
end end
......
...@@ -3,9 +3,12 @@ ...@@ -3,9 +3,12 @@
= form_for [@project.namespace.becomes(Namespace), @project, @page], method: @page.persisted? ? :put : :post, html: { class: 'form-horizontal wiki-form common-note-form prepend-top-default js-quick-submit' } do |f| = form_for [@project.namespace.becomes(Namespace), @project, @page], method: @page.persisted? ? :put : :post, html: { class: 'form-horizontal wiki-form common-note-form prepend-top-default js-quick-submit' } do |f|
= form_errors(@page) = form_errors(@page)
= f.hidden_field :title, value: @page.title
- if @page.persisted? - if @page.persisted?
= f.hidden_field :last_commit_sha, value: @page.last_commit_sha = f.hidden_field :last_commit_sha, value: @page.last_commit_sha
.form-group
.col-sm-12= f.label :title, class: 'control-label-full-width'
.col-sm-12= f.text_field :title, class: 'form-control', value: @page.title
.form-group .form-group
.col-sm-12= f.label :format, class: 'control-label-full-width' .col-sm-12= f.label :format, class: 'control-label-full-width'
.col-sm-12 .col-sm-12
......
---
title: Allow wiki pages to be renamed in the UI
merge_request: 10069
author: wendy0402
...@@ -63,7 +63,7 @@ class Spinach::Features::ProjectWiki < Spinach::FeatureSteps ...@@ -63,7 +63,7 @@ class Spinach::Features::ProjectWiki < Spinach::FeatureSteps
end end
step 'That page has two revisions' do step 'That page has two revisions' do
@page.update("new content", message: "second commit") @page.update(content: "new content", message: "second commit")
end end
step 'I click the History button' do step 'I click the History button' do
......
...@@ -55,7 +55,7 @@ feature 'Projects > Wiki > User updates wiki page' do ...@@ -55,7 +55,7 @@ feature 'Projects > Wiki > User updates wiki page' do
scenario 'page has been updated since the user opened the edit page' do scenario 'page has been updated since the user opened the edit page' do
click_link 'Edit' click_link 'Edit'
wiki_page.update('Update') wiki_page.update(content: 'Update')
click_button 'Save changes' click_button 'Save changes'
......
...@@ -223,7 +223,12 @@ describe ProjectWiki do ...@@ -223,7 +223,12 @@ describe ProjectWiki do
before do before do
create_page("update-page", "some content") create_page("update-page", "some content")
@gollum_page = subject.wiki.paged("update-page") @gollum_page = subject.wiki.paged("update-page")
subject.update_page(@gollum_page, "some other content", :markdown, "updated page") subject.update_page(
@gollum_page,
content: "some other content",
format: :markdown,
message: "updated page"
)
@page = subject.pages.first.page @page = subject.pages.first.page
end end
...@@ -240,7 +245,12 @@ describe ProjectWiki do ...@@ -240,7 +245,12 @@ describe ProjectWiki do
end end
it 'updates project activity' do it 'updates project activity' do
subject.update_page(@gollum_page, 'Yet more content', :markdown, 'Updated page again') subject.update_page(
@gollum_page,
content: 'Yet more content',
format: :markdown,
message: 'Updated page again'
)
project.reload project.reload
......
...@@ -178,12 +178,12 @@ describe WikiPage do ...@@ -178,12 +178,12 @@ describe WikiPage do
end end
it "updates the content of the page" do it "updates the content of the page" do
@page.update("new content") @page.update(content: "new content")
@page = wiki.find_page(title) @page = wiki.find_page(title)
end end
it "returns true" do it "returns true" do
expect(@page.update("more content")).to be_truthy expect(@page.update(content: "more content")).to be_truthy
end end
end end
end end
...@@ -195,29 +195,42 @@ describe WikiPage do ...@@ -195,29 +195,42 @@ describe WikiPage do
end end
after do after do
destroy_page("Update") destroy_page(@page.title)
end end
context "with valid attributes" do context "with valid attributes" do
it "updates the content of the page" do it "updates the content of the page" do
@page.update("new content") new_content = "new content"
@page.update(content: new_content)
@page = wiki.find_page("Update") @page = wiki.find_page("Update")
expect(@page.content).to eq("new content")
end
it "updates the title of the page" do
new_title = "Index v.1.2.4"
@page.update(title: new_title)
@page = wiki.find_page(new_title)
expect(@page.title).to eq(new_title)
end end
it "returns true" do it "returns true" do
expect(@page.update("more content")).to be_truthy expect(@page.update(content: "more content")).to be_truthy
end end
end end
context 'with same last commit sha' do context 'with same last commit sha' do
it 'returns true' do it 'returns true' do
expect(@page.update('more content', last_commit_sha: @page.last_commit_sha)).to be_truthy expect(@page.update(content: 'more content', last_commit_sha: @page.last_commit_sha)).to be_truthy
end end
end end
context 'with different last commit sha' do context 'with different last commit sha' do
it 'raises exception' do it 'raises exception' do
expect { @page.update('more content', last_commit_sha: 'xxx') }.to raise_error(WikiPage::PageChangedError) expect { @page.update(content: 'more content', last_commit_sha: 'xxx') }.to raise_error(WikiPage::PageChangedError)
end end
end end
end end
...@@ -249,7 +262,7 @@ describe WikiPage do ...@@ -249,7 +262,7 @@ describe WikiPage do
end end
it "returns an array of all commits for the page" do it "returns an array of all commits for the page" do
3.times { |i| @page.update("content #{i}") } 3.times { |i| @page.update(content: "content #{i}") }
expect(@page.versions.count).to eq(4) expect(@page.versions.count).to eq(4)
end end
end end
...@@ -294,7 +307,7 @@ describe WikiPage do ...@@ -294,7 +307,7 @@ describe WikiPage do
before do before do
create_page('Update', 'content') create_page('Update', 'content')
@page = wiki.find_page('Update') @page = wiki.find_page('Update')
3.times { |i| @page.update("content #{i}") } 3.times { |i| @page.update(content: "content #{i}") }
end end
after do after do
...@@ -338,7 +351,7 @@ describe WikiPage do ...@@ -338,7 +351,7 @@ describe WikiPage do
end end
it 'returns false for updated wiki page' do it 'returns false for updated wiki page' do
updated_wiki_page = original_wiki_page.update("Updated content") updated_wiki_page = original_wiki_page.update(content: "Updated content")
expect(original_wiki_page).not_to eq(updated_wiki_page) expect(original_wiki_page).not_to eq(updated_wiki_page)
end end
end end
...@@ -360,7 +373,7 @@ describe WikiPage do ...@@ -360,7 +373,7 @@ describe WikiPage do
it 'is changed after page updated' do it 'is changed after page updated' do
last_commit_sha_before_update = @page.last_commit_sha last_commit_sha_before_update = @page.last_commit_sha
@page.update("new content") @page.update(content: "new content")
@page = wiki.find_page("Update") @page = wiki.find_page("Update")
expect(@page.last_commit_sha).not_to eq last_commit_sha_before_update expect(@page.last_commit_sha).not_to eq last_commit_sha_before_update
......
...@@ -9,7 +9,8 @@ describe WikiPages::UpdateService do ...@@ -9,7 +9,8 @@ describe WikiPages::UpdateService do
{ {
content: 'New content for wiki page', content: 'New content for wiki page',
format: 'markdown', format: 'markdown',
message: 'New wiki message' message: 'New wiki message',
title: 'New Title'
} }
end end
...@@ -27,6 +28,7 @@ describe WikiPages::UpdateService do ...@@ -27,6 +28,7 @@ describe WikiPages::UpdateService do
expect(updated_page.message).to eq(opts[:message]) expect(updated_page.message).to eq(opts[:message])
expect(updated_page.content).to eq(opts[:content]) expect(updated_page.content).to eq(opts[:content])
expect(updated_page.format).to eq(opts[:format].to_sym) expect(updated_page.format).to eq(opts[:format].to_sym)
expect(updated_page.title).to eq(opts[:title])
end end
it 'executes webhooks' do it 'executes webhooks' do
......
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