wikis_controller.rb 2.16 KB
Newer Older
1
class Projects::WikisController < Projects::ApplicationController
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
2
  before_filter :authorize_read_wiki!
3 4
  before_filter :authorize_write_wiki!, only: [:edit, :create, :history]
  before_filter :authorize_admin_wiki!, only: :destroy
5
  before_filter :load_gollum_wiki
6

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
7
  def pages
8
    @wiki_pages = @gollum_wiki.pages
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
9 10
  end

Valery Sizov's avatar
Valery Sizov committed
11
  def show
12
    @wiki = @gollum_wiki.find_page(params[:id], params[:version_id])
13

14 15 16
    if @wiki
      render 'show'
    else
17 18 19 20 21
      return render('empty') unless can?(current_user, :write_wiki, @project)
      @wiki = WikiPage.new(@gollum_wiki)
      @wiki.title = params[:id]

      render 'edit'
Valery Sizov's avatar
Valery Sizov committed
22 23 24 25
    end
  end

  def edit
26 27 28 29 30 31 32 33 34 35 36 37 38
    @wiki = @gollum_wiki.find_page(params[:id])
  end

  def update
    @wiki = @gollum_wiki.find_page(params[:id])

    return render('empty') unless can?(current_user, :write_wiki, @project)

    if @wiki.update(content, format, message)
      redirect_to [@project, @wiki], notice: 'Wiki was successfully updated.'
    else
      render 'edit'
    end
Valery Sizov's avatar
Valery Sizov committed
39 40 41
  end

  def create
42 43 44 45 46 47
    @wiki = WikiPage.new(@gollum_wiki)

    if @wiki.create(wiki_params)
      redirect_to project_wiki_path(@project, @wiki), notice: 'Wiki was successfully updated.'
    else
      render action: "edit"
Valery Sizov's avatar
Valery Sizov committed
48 49
    end
  end
Valery Sizov's avatar
Valery Sizov committed
50 51

  def history
52 53 54
    @wiki = @gollum_wiki.find_page(params[:id])

    redirect_to(project_wiki_path(@project, :home), notice: "Page not found") unless @wiki
Valery Sizov's avatar
Valery Sizov committed
55
  end
56

Valery Sizov's avatar
Valery Sizov committed
57
  def destroy
58 59 60 61
    @wiki = @gollum_wiki.find_page(params[:id])
    @wiki.delete if @wiki
    redirect_to project_wiki_path(@project, :home), notice: "Page was successfully deleted"
  end
Valery Sizov's avatar
Valery Sizov committed
62

63
  def git_access
Valery Sizov's avatar
Valery Sizov committed
64
  end
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

  private

  def load_gollum_wiki
    @gollum_wiki = GollumWiki.new(@project, current_user)

    # Call #wiki to make sure the Wiki Repo is initialized
    @gollum_wiki.wiki
  rescue GollumWiki::CouldNotCreateWikiError => ex
    flash[:notice] = "Could not create Wiki Repository at this time. Please try again later."
    redirect_to @project
    return false
  end

  def wiki_params
    params[:wiki].slice(:title, :content, :format, :message)
  end

  def content
    params[:wiki][:content]
  end

  def format
    params[:wiki][:format]
  end

  def message
    params[:wiki][:message]
  end

Valery Sizov's avatar
Valery Sizov committed
95
end