wikis_controller.rb 1.59 KB
Newer Older
Valery Sizov's avatar
Valery Sizov committed
1 2
class WikisController < ApplicationController
  before_filter :project
Valery Sizov's avatar
Valery Sizov committed
3
  before_filter :add_project_abilities
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
4
  before_filter :authorize_read_wiki!
5
  before_filter :authorize_write_wiki!, :only => [:edit, :create, :history]
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
6
  before_filter :authorize_admin_wiki!, :only => :destroy
Valery Sizov's avatar
Valery Sizov committed
7 8 9
  layout "project"
  
  def show
Valery Sizov's avatar
Valery Sizov committed
10 11 12 13 14
    if params[:old_page_id]
      @wiki = @project.wikis.find(params[:old_page_id])
    else
      @wiki = @project.wikis.where(:slug => params[:id]).order("created_at").last
    end
15 16 17 18 19

    unless @wiki
      return render_404 unless can?(current_user, :write_wiki, @project)
    end

Ian Morgan's avatar
Ian Morgan committed
20 21
    @note = @project.notes.new(:noteable => @wiki)

Valery Sizov's avatar
Valery Sizov committed
22
    respond_to do |format|
23 24 25 26 27 28
      if @wiki
        format.html
      else
        @wiki = @project.wikis.new(:slug => params[:id])
        format.html { render "edit" }
      end
Valery Sizov's avatar
Valery Sizov committed
29 30 31 32
    end
  end

  def edit
33 34
    @wiki = @project.wikis.where(:slug => params[:id]).order("created_at").last
    @wiki = Wiki.regenerate_from @wiki
Valery Sizov's avatar
Valery Sizov committed
35 36 37
  end

  def create
38
    @wiki = @project.wikis.new(params[:wiki])
Valery Sizov's avatar
Valery Sizov committed
39
    @wiki.user = current_user
Valery Sizov's avatar
Valery Sizov committed
40 41 42

    respond_to do |format|
      if @wiki.save
43
        format.html { redirect_to [@project, @wiki], notice: 'Wiki was successfully updated.' }
Valery Sizov's avatar
Valery Sizov committed
44 45 46 47 48
      else
        format.html { render action: "edit" }
      end
    end
  end
Valery Sizov's avatar
Valery Sizov committed
49 50 51 52

  def history
    @wikis = @project.wikis.where(:slug => params[:id]).order("created_at")
  end
53
  
Valery Sizov's avatar
Valery Sizov committed
54
  def destroy
vsizov's avatar
vsizov committed
55
    @wikis = @project.wikis.where(:slug => params[:id]).delete_all
Valery Sizov's avatar
Valery Sizov committed
56 57

    respond_to do |format|
vsizov's avatar
vsizov committed
58
      format.html { redirect_to project_wiki_path(@project, :index), notice: "Page was successfully deleted" }
Valery Sizov's avatar
Valery Sizov committed
59 60 61
    end
  end
end