BigW Consortium Gitlab

wikis_controller.rb 2.59 KB
Newer Older
1 2
require 'project_wiki'

3
class Projects::WikisController < Projects::ApplicationController
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
7
  before_filter :load_project_wiki
8

Dmitriy Zaporozhets committed
9
  def pages
10
    @wiki_pages = Kaminari.paginate_array(@project_wiki.pages).page(params[:page]).per(30)
Dmitriy Zaporozhets committed
11 12
  end

Valery Sizov committed
13
  def show
14
    @page = @project_wiki.find_page(params[:id], params[:version_id])
15

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

      render 'edit'
Valery Sizov committed
35 36 37 38
    end
  end

  def edit
39
    @page = @project_wiki.find_page(params[:id])
40 41 42
  end

  def update
43
    @page = @project_wiki.find_page(params[:id])
44 45 46

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

47 48
    if @page.update(content, format, message)
      redirect_to [@project, @page], notice: 'Wiki was successfully updated.'
49 50 51
    else
      render 'edit'
    end
Valery Sizov committed
52 53 54
  end

  def create
55
    @page = WikiPage.new(@project_wiki)
56

57 58
    if @page.create(wiki_params)
      redirect_to project_wiki_path(@project, @page), notice: 'Wiki was successfully updated.'
59 60
    else
      render action: "edit"
Valery Sizov committed
61 62
    end
  end
Valery Sizov committed
63 64

  def history
65
    @page = @project_wiki.find_page(params[:id])
66

67 68 69
    unless @page
      redirect_to(project_wiki_path(@project, :home), notice: "Page not found")
    end
Valery Sizov committed
70
  end
71

Valery Sizov committed
72
  def destroy
73 74 75
    @page = @project_wiki.find_page(params[:id])
    @page.delete if @page

76 77
    redirect_to project_wiki_path(@project, :home), notice: "Page was successfully deleted"
  end
Valery Sizov committed
78

79
  def git_access
Valery Sizov committed
80
  end
81 82 83

  private

84 85
  def load_project_wiki
    @project_wiki = ProjectWiki.new(@project, current_user)
86 87

    # Call #wiki to make sure the Wiki Repo is initialized
88 89
    @project_wiki.wiki
  rescue ProjectWiki::CouldNotCreateWikiError => ex
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
    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 committed
110
end