wikis_controller.rb 2.85 KB
Newer Older
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
1 2
require 'project_wiki'

3
class Projects::WikisController < Projects::ApplicationController
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
4
  before_filter :authorize_read_wiki!
5 6
  before_filter :authorize_write_wiki!, only: [:edit, :create, :history]
  before_filter :authorize_admin_wiki!, only: :destroy
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
7
  before_filter :load_project_wiki
8
  include WikiHelper
9

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
10
  def pages
11
    @wiki_pages = Kaminari.paginate_array(@project_wiki.pages).page(params[:page]).per(PER_PAGE)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
12 13
  end

Valery Sizov's avatar
Valery Sizov committed
14
  def show
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
15
    @page = @project_wiki.find_page(params[:id], params[:version_id])
16

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
17
    if @page
18
      render 'show'
19
    elsif file = @project_wiki.find_file(params[:id], params[:version_id])
20 21 22 23 24 25 26 27 28 29
      if file.on_disk?
        send_file file.on_disk_path, disposition: 'inline'
      else
        send_data(
          file.raw_data,
          type: file.mime_type,
          disposition: 'inline',
          filename: file.name
        )
      end
30
    else
31
      return render('empty') unless can?(current_user, :write_wiki, @project)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
32 33
      @page = WikiPage.new(@project_wiki)
      @page.title = params[:id]
34 35

      render 'edit'
Valery Sizov's avatar
Valery Sizov committed
36 37 38 39
    end
  end

  def edit
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
40
    @page = @project_wiki.find_page(params[:id])
41 42 43
  end

  def update
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
44
    @page = @project_wiki.find_page(params[:id])
45 46 47

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

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
48
    if @page.update(content, format, message)
49 50 51 52
      redirect_to(
        namespace_project_wiki_path(@project.namespace, @project, @page),
        notice: 'Wiki was successfully updated.'
      )
53 54 55
    else
      render 'edit'
    end
Valery Sizov's avatar
Valery Sizov committed
56 57 58
  end

  def create
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
59
    @page = WikiPage.new(@project_wiki)
60

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
61
    if @page.create(wiki_params)
Vinnie Okada's avatar
Vinnie Okada committed
62 63 64 65
      redirect_to(
        namespace_project_wiki_path(@project.namespace, @project, @page),
        notice: 'Wiki was successfully updated.'
      )
66 67
    else
      render action: "edit"
Valery Sizov's avatar
Valery Sizov committed
68 69
    end
  end
Valery Sizov's avatar
Valery Sizov committed
70 71

  def history
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
72
    @page = @project_wiki.find_page(params[:id])
73

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
74
    unless @page
Vinnie Okada's avatar
Vinnie Okada committed
75 76 77 78
      redirect_to(
        namespace_project_wiki_path(@project.namespace, @project, :home),
        notice: "Page not found"
      )
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
79
    end
Valery Sizov's avatar
Valery Sizov committed
80
  end
81

Valery Sizov's avatar
Valery Sizov committed
82
  def destroy
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
83 84 85
    @page = @project_wiki.find_page(params[:id])
    @page.delete if @page

Vinnie Okada's avatar
Vinnie Okada committed
86 87 88 89
    redirect_to(
      namespace_project_wiki_path(@project.namespace, @project, :home),
      notice: "Page was successfully deleted"
    )
90
  end
Valery Sizov's avatar
Valery Sizov committed
91

92
  def git_access
Valery Sizov's avatar
Valery Sizov committed
93
  end
94 95 96

  private

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
97 98
  def load_project_wiki
    @project_wiki = ProjectWiki.new(@project, current_user)
99 100

    # Call #wiki to make sure the Wiki Repo is initialized
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
101 102
    @project_wiki.wiki
  rescue ProjectWiki::CouldNotCreateWikiError => ex
103
    flash[:notice] = "Could not create Wiki Repository at this time. Please try again later."
104
    redirect_to project_path(@project)
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
    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
123
end