Commit fae6eb63 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Merge branch 'wiki-refactoring' into 'master'

Wiki refactoring

Better names for variables and classes
parents 71678f08 1bd28994
require 'project_wiki'
class Projects::WikisController < Projects::ApplicationController class Projects::WikisController < Projects::ApplicationController
before_filter :authorize_read_wiki! before_filter :authorize_read_wiki!
before_filter :authorize_write_wiki!, only: [:edit, :create, :history] before_filter :authorize_write_wiki!, only: [:edit, :create, :history]
before_filter :authorize_admin_wiki!, only: :destroy before_filter :authorize_admin_wiki!, only: :destroy
before_filter :load_gollum_wiki before_filter :load_project_wiki
def pages def pages
@wiki_pages = @gollum_wiki.pages @wiki_pages = @project_wiki.pages
end end
def show def show
@wiki = @gollum_wiki.find_page(params[:id], params[:version_id]) @page = @project_wiki.find_page(params[:id], params[:version_id])
if @wiki if @page
render 'show' render 'show'
else else
return render('empty') unless can?(current_user, :write_wiki, @project) return render('empty') unless can?(current_user, :write_wiki, @project)
@wiki = WikiPage.new(@gollum_wiki) @page = WikiPage.new(@project_wiki)
@wiki.title = params[:id] @page.title = params[:id]
render 'edit' render 'edit'
end end
end end
def edit def edit
@wiki = @gollum_wiki.find_page(params[:id]) @page = @project_wiki.find_page(params[:id])
end end
def update def update
@wiki = @gollum_wiki.find_page(params[:id]) @page = @project_wiki.find_page(params[:id])
return render('empty') unless can?(current_user, :write_wiki, @project) return render('empty') unless can?(current_user, :write_wiki, @project)
if @wiki.update(content, format, message) if @page.update(content, format, message)
redirect_to [@project, @wiki], notice: 'Wiki was successfully updated.' redirect_to [@project, @page], notice: 'Wiki was successfully updated.'
else else
render 'edit' render 'edit'
end end
end end
def create def create
@wiki = WikiPage.new(@gollum_wiki) @page = WikiPage.new(@project_wiki)
if @wiki.create(wiki_params) if @page.create(wiki_params)
redirect_to project_wiki_path(@project, @wiki), notice: 'Wiki was successfully updated.' redirect_to project_wiki_path(@project, @page), notice: 'Wiki was successfully updated.'
else else
render action: "edit" render action: "edit"
end end
end end
def history def history
@wiki = @gollum_wiki.find_page(params[:id]) @page = @project_wiki.find_page(params[:id])
redirect_to(project_wiki_path(@project, :home), notice: "Page not found") unless @wiki unless @page
redirect_to(project_wiki_path(@project, :home), notice: "Page not found")
end
end end
def destroy def destroy
@wiki = @gollum_wiki.find_page(params[:id]) @page = @project_wiki.find_page(params[:id])
@wiki.delete if @wiki @page.delete if @page
redirect_to project_wiki_path(@project, :home), notice: "Page was successfully deleted" redirect_to project_wiki_path(@project, :home), notice: "Page was successfully deleted"
end end
...@@ -65,12 +70,12 @@ class Projects::WikisController < Projects::ApplicationController ...@@ -65,12 +70,12 @@ class Projects::WikisController < Projects::ApplicationController
private private
def load_gollum_wiki def load_project_wiki
@gollum_wiki = GollumWiki.new(@project, current_user) @project_wiki = ProjectWiki.new(@project, current_user)
# Call #wiki to make sure the Wiki Repo is initialized # Call #wiki to make sure the Wiki Repo is initialized
@gollum_wiki.wiki @project_wiki.wiki
rescue GollumWiki::CouldNotCreateWikiError => ex rescue ProjectWiki::CouldNotCreateWikiError => ex
flash[:notice] = "Could not create Wiki Repository at this time. Please try again later." flash[:notice] = "Could not create Wiki Repository at this time. Please try again later."
redirect_to @project redirect_to @project
return false return false
...@@ -91,5 +96,4 @@ class Projects::WikisController < Projects::ApplicationController ...@@ -91,5 +96,4 @@ class Projects::WikisController < Projects::ApplicationController
def message def message
params[:wiki][:message] params[:wiki][:message]
end end
end end
class GollumWiki class ProjectWiki
include Gitlab::ShellAdapter include Gitlab::ShellAdapter
MARKUPS = { MARKUPS = {
......
...@@ -19,7 +19,7 @@ class WikiPage ...@@ -19,7 +19,7 @@ class WikiPage
validates :title, presence: true validates :title, presence: true
validates :content, presence: true validates :content, presence: true
# The Gitlab GollumWiki instance. # The Gitlab ProjectWiki instance.
attr_reader :wiki attr_reader :wiki
# The raw Gollum::Page instance. # The raw Gollum::Page instance.
...@@ -118,7 +118,7 @@ class WikiPage ...@@ -118,7 +118,7 @@ class WikiPage
# :content - The raw markup content. # :content - The raw markup content.
# :format - Optional symbol representing the # :format - Optional symbol representing the
# content format. Can be any type # content format. Can be any type
# listed in the GollumWiki::MARKUPS # listed in the ProjectWiki::MARKUPS
# Hash. # Hash.
# :message - Optional commit message to set on # :message - Optional commit message to set on
# the new page. # the new page.
...@@ -135,7 +135,7 @@ class WikiPage ...@@ -135,7 +135,7 @@ class WikiPage
# #
# new_content - The raw markup content to replace the existing. # new_content - The raw markup content to replace the existing.
# format - Optional symbol representing the content format. # format - Optional symbol representing the content format.
# See GollumWiki::MARKUPS Hash for available formats. # See ProjectWiki::MARKUPS Hash for available formats.
# message - Optional commit message to set on the new version. # message - Optional commit message to set on the new version.
# #
# Returns the String SHA1 of the newly created page # Returns the String SHA1 of the newly created page
...@@ -181,5 +181,4 @@ class WikiPage ...@@ -181,5 +181,4 @@ class WikiPage
end end
@persisted @persisted
end end
end end
...@@ -74,8 +74,8 @@ module Projects ...@@ -74,8 +74,8 @@ module Projects
if @project.wiki_enabled? if @project.wiki_enabled?
begin begin
# force the creation of a wiki, # force the creation of a wiki,
GollumWiki.new(@project, @project.owner).wiki ProjectWiki.new(@project, @project.owner).wiki
rescue GollumWiki::CouldNotCreateWikiError => ex rescue ProjectWiki::CouldNotCreateWikiError => ex
# Prevent project observer crash # Prevent project observer crash
# if failed to create wiki # if failed to create wiki
nil nil
......
= form_for [@project, @wiki], method: @wiki.persisted? ? :put : :post, html: { class: 'form-horizontal' } do |f| = form_for [@project, @page], method: @page.persisted? ? :put : :post, html: { class: 'form-horizontal' } do |f|
-if @wiki.errors.any? -if @page.errors.any?
#error_explanation #error_explanation
%h2= "#{pluralize(@wiki.errors.count, "error")} prohibited this wiki from being saved:" %h2= "#{pluralize(@page.errors.count, "error")} prohibited this wiki from being saved:"
%ul %ul
- @wiki.errors.full_messages.each do |msg| - @page.errors.full_messages.each do |msg|
%li= msg %li= msg
= f.hidden_field :title, value: @wiki.title = f.hidden_field :title, value: @page.title
.form-group .form-group
= f.label :format, class: 'control-label' = f.label :format, class: 'control-label'
.col-sm-10 .col-sm-10
= f.select :format, options_for_select(GollumWiki::MARKUPS, {selected: @wiki.format}), {}, class: "form-control" = f.select :format, options_for_select(ProjectWiki::MARKUPS, {selected: @page.format}), {}, class: "form-control"
.row .row
.col-sm-2 .col-sm-2
...@@ -31,9 +31,9 @@ ...@@ -31,9 +31,9 @@
.col-sm-10= f.text_field :message, class: 'form-control', rows: 18 .col-sm-10= f.text_field :message, class: 'form-control', rows: 18
.form-actions .form-actions
- if @wiki && @wiki.persisted? - if @page && @page.persisted?
= f.submit 'Save changes', class: "btn-save btn" = f.submit 'Save changes', class: "btn-save btn"
= link_to "Cancel", project_wiki_path(@project, @wiki), class: "btn btn-cancel" = link_to "Cancel", project_wiki_path(@project, @page), class: "btn btn-cancel"
- else - else
= f.submit 'Create page', class: "btn-create btn" = f.submit 'Create page', class: "btn-create btn"
= link_to "Cancel", project_wiki_path(@project, :home), class: "btn btn-cancel" = link_to "Cancel", project_wiki_path(@project, :home), class: "btn btn-cancel"
%span.pull-right %span.pull-right
- if (@wiki && @wiki.persisted?) - if (@page && @page.persisted?)
= link_to history_project_wiki_path(@project, @wiki), class: "btn btn-grouped" do = link_to history_project_wiki_path(@project, @page), class: "btn btn-grouped" do
Page History Page History
- if can?(current_user, :write_wiki, @project) - if can?(current_user, :write_wiki, @project)
= link_to edit_project_wiki_path(@project, @wiki), class: "btn btn-grouped" do = link_to edit_project_wiki_path(@project, @page), class: "btn btn-grouped" do
%i.icon-edit %i.icon-edit
Edit Edit
...@@ -3,11 +3,11 @@ ...@@ -3,11 +3,11 @@
= render 'main_links' = render 'main_links'
%h3.page-title %h3.page-title
Editing - Editing -
%span.light #{@wiki.title.titleize} %span.light #{@page.title.titleize}
%hr %hr
= render 'form' = render 'form'
.pull-right .pull-right
- if @wiki.persisted? && can?(current_user, :admin_wiki, @project) - if @page.persisted? && can?(current_user, :admin_wiki, @project)
= link_to project_wiki_path(@project, @wiki), data: { confirm: "Are you sure you want to delete this page?"}, method: :delete, class: "btn btn-small btn-remove" do = link_to project_wiki_path(@project, @page), data: { confirm: "Are you sure you want to delete this page?"}, method: :delete, class: "btn btn-small btn-remove" do
Delete this page Delete this page
...@@ -3,10 +3,10 @@ ...@@ -3,10 +3,10 @@
.col-sm-6 .col-sm-6
%h3.page-title %h3.page-title
Git access for Git access for
%strong= @gollum_wiki.path_with_namespace %strong= @project_wiki.path_with_namespace
.col-sm-6 .col-sm-6
= render "shared/clone_panel", project: @gollum_wiki = render "shared/clone_panel", project: @project_wiki
.git-empty .git-empty
%fieldset %fieldset
...@@ -18,8 +18,8 @@ ...@@ -18,8 +18,8 @@
%legend Clone Your Wiki: %legend Clone Your Wiki:
%pre.dark %pre.dark
:preserve :preserve
git clone #{ content_tag(:span, default_url_to_repo(@gollum_wiki), class: 'clone')} git clone #{ content_tag(:span, default_url_to_repo(@project_wiki), class: 'clone')}
cd #{@gollum_wiki.path} cd #{@project_wiki.path}
%legend Start Gollum And Edit Locally: %legend Start Gollum And Edit Locally:
%pre.dark %pre.dark
......
= render 'nav' = render 'nav'
%h3.page-title %h3.page-title
%span.light History for %span.light History for
= link_to @wiki.title.titleize, project_wiki_path(@project, @wiki) = link_to @page.title.titleize, project_wiki_path(@project, @page)
%table.table %table.table
%thead %thead
...@@ -12,11 +12,11 @@ ...@@ -12,11 +12,11 @@
%th Last updated %th Last updated
%th Format %th Format
%tbody %tbody
- @wiki.versions.each do |version| - @page.versions.each do |version|
- commit = version - commit = version
%tr %tr
%td %td
= link_to project_wiki_path(@project, @wiki, version_id: commit.id) do = link_to project_wiki_path(@project, @page, version_id: commit.id) do
= commit.short_id = commit.short_id
%td %td
= commit_author_link(commit, avatar: true, size: 24) = commit_author_link(commit, avatar: true, size: 24)
...@@ -26,4 +26,4 @@ ...@@ -26,4 +26,4 @@
#{time_ago_with_tooltip(version.date)} #{time_ago_with_tooltip(version.date)}
%td %td
%strong %strong
= @wiki.page.wiki.page(@wiki.page.name, commit.id).try(:format) = @page.page.wiki.page(@page.page.name, commit.id).try(:format)
= render 'nav' = render 'nav'
%h3.page-title %h3.page-title
= @wiki.title.titleize = @page.title.titleize
= render 'main_links' = render 'main_links'
- if @wiki.historical? - if @page.historical?
.warning_message .warning_message
This is an old version of this page. This is an old version of this page.
You can view the #{link_to "most recent version", project_wiki_path(@project, @wiki)} or browse the #{link_to "history", history_project_wiki_path(@project, @wiki)}. You can view the #{link_to "most recent version", project_wiki_path(@project, @page)} or browse the #{link_to "history", history_project_wiki_path(@project, @page)}.
%hr %hr
.wiki-holder .wiki-holder
.wiki .wiki
= preserve do = preserve do
= render_wiki_content(@wiki) = render_wiki_content(@page)
%hr %hr
.wiki-last-edit-by .wiki-last-edit-by
Last edited by #{commit_author_link(@wiki.commit, avatar: true, size: 16)} #{time_ago_with_tooltip(@wiki.commit.created_at)} Last edited by #{commit_author_link(@page.commit, avatar: true, size: 16)} #{time_ago_with_tooltip(@page.commit.created_at)}
class ProjectWiki < Spinach::FeatureSteps class Spinach::Features::ProjectWiki < Spinach::FeatureSteps
include SharedAuthentication include SharedAuthentication
include SharedProject include SharedProject
include SharedNote include SharedNote
...@@ -16,7 +16,7 @@ class ProjectWiki < Spinach::FeatureSteps ...@@ -16,7 +16,7 @@ class ProjectWiki < Spinach::FeatureSteps
end end
Given 'I create the Wiki Home page' do Given 'I create the Wiki Home page' do
fill_in "Content", with: '[link test](test)' fill_in "wiki_content", with: '[link test](test)'
click_on "Create page" click_on "Create page"
end end
...@@ -87,6 +87,6 @@ class ProjectWiki < Spinach::FeatureSteps ...@@ -87,6 +87,6 @@ class ProjectWiki < Spinach::FeatureSteps
end end
def wiki def wiki
@gollum_wiki = GollumWiki.new(project, current_user) @project_wiki = ProjectWiki.new(project, current_user)
end end
end end
...@@ -24,7 +24,7 @@ module Backup ...@@ -24,7 +24,7 @@ module Backup
puts "[FAILED]".red puts "[FAILED]".red
end end
wiki = GollumWiki.new(project) wiki = ProjectWiki.new(project)
if File.exists?(path_to_repo(wiki)) if File.exists?(path_to_repo(wiki))
print " * #{wiki.path_with_namespace} ... " print " * #{wiki.path_with_namespace} ... "
...@@ -59,7 +59,7 @@ module Backup ...@@ -59,7 +59,7 @@ module Backup
puts "[FAILED]".red puts "[FAILED]".red
end end
wiki = GollumWiki.new(project) wiki = ProjectWiki.new(project)
if File.exists?(path_to_bundle(wiki)) if File.exists?(path_to_bundle(wiki))
print " * #{wiki.path_with_namespace} ... " print " * #{wiki.path_with_namespace} ... "
......
...@@ -60,6 +60,8 @@ class Redcarpet::Render::GitlabHTML < Redcarpet::Render::HTML ...@@ -60,6 +60,8 @@ class Redcarpet::Render::GitlabHTML < Redcarpet::Render::HTML
end end
def is_wiki? def is_wiki?
@template.instance_variable_get("@wiki") if @template.instance_variable_get("@project_wiki")
@template.instance_variable_get("@page")
end
end end
end end
require "spec_helper" require "spec_helper"
describe GollumWiki do describe ProjectWiki do
def remove_temp_repo(path) def remove_temp_repo(path)
FileUtils.rm_rf path FileUtils.rm_rf path
...@@ -23,7 +23,7 @@ describe GollumWiki do ...@@ -23,7 +23,7 @@ describe GollumWiki do
let(:user) { project.owner } let(:user) { project.owner }
let(:gitlab_shell) { Gitlab::Shell.new } let(:gitlab_shell) { Gitlab::Shell.new }
subject { GollumWiki.new(project, user) } subject { ProjectWiki.new(project, user) }
before do before do
create_temp_repo(subject.send(:path_to_repo)) create_temp_repo(subject.send(:path_to_repo))
...@@ -68,15 +68,15 @@ describe GollumWiki do ...@@ -68,15 +68,15 @@ describe GollumWiki do
end end
it "creates a new wiki repo if one does not yet exist" do it "creates a new wiki repo if one does not yet exist" do
wiki = GollumWiki.new(project, user) wiki = ProjectWiki.new(project, user)
wiki.create_page("index", "test content").should_not == false wiki.create_page("index", "test content").should_not == false
FileUtils.rm_rf wiki.send(:path_to_repo) FileUtils.rm_rf wiki.send(:path_to_repo)
end end
it "raises CouldNotCreateWikiError if it can't create the wiki repository" do it "raises CouldNotCreateWikiError if it can't create the wiki repository" do
GollumWiki.any_instance.stub(:init_repo).and_return(false) ProjectWiki.any_instance.stub(:init_repo).and_return(false)
expect { GollumWiki.new(project, user).wiki }.to raise_exception(GollumWiki::CouldNotCreateWikiError) expect { ProjectWiki.new(project, user).wiki }.to raise_exception(ProjectWiki::CouldNotCreateWikiError)
end end
end end
......
...@@ -22,7 +22,7 @@ describe WikiPage do ...@@ -22,7 +22,7 @@ describe WikiPage do
let(:project) { create(:project) } let(:project) { create(:project) }
let(:repository) { project.repository } let(:repository) { project.repository }
let(:user) { project.owner } let(:user) { project.owner }
let(:wiki) { GollumWiki.new(project, user) } let(:wiki) { ProjectWiki.new(project, user) }
subject { WikiPage.new(wiki) } subject { WikiPage.new(wiki) }
......
...@@ -42,7 +42,7 @@ describe Projects::CreateService do ...@@ -42,7 +42,7 @@ describe Projects::CreateService do
context 'wiki_enabled true creates wiki repository directory' do context 'wiki_enabled true creates wiki repository directory' do
before do before do
@project = create_project(@user, @opts) @project = create_project(@user, @opts)
@path = GollumWiki.new(@project, @user).send(:path_to_repo) @path = ProjectWiki.new(@project, @user).send(:path_to_repo)
end end
it { File.exists?(@path).should be_true } it { File.exists?(@path).should be_true }
...@@ -52,7 +52,7 @@ describe Projects::CreateService do ...@@ -52,7 +52,7 @@ describe Projects::CreateService do
before do before do
@opts.merge!(wiki_enabled: false) @opts.merge!(wiki_enabled: false)
@project = create_project(@user, @opts) @project = create_project(@user, @opts)
@path = GollumWiki.new(@project, @user).send(:path_to_repo) @path = ProjectWiki.new(@project, @user).send(:path_to_repo)
end end
it { File.exists?(@path).should be_false } it { File.exists?(@path).should be_false }
......
...@@ -52,7 +52,7 @@ module TestEnv ...@@ -52,7 +52,7 @@ module TestEnv
def setup_stubs() def setup_stubs()
# Use tmp dir for FS manipulations # Use tmp dir for FS manipulations
repos_path = testing_path() repos_path = testing_path()
GollumWiki.any_instance.stub(:init_repo) do |path| ProjectWiki.any_instance.stub(:init_repo) do |path|
create_temp_repo(File.join(repos_path, "#{path}.git")) create_temp_repo(File.join(repos_path, "#{path}.git"))
end end
......
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