summaryrefslogtreecommitdiff
path: root/app/controllers/snippets_controller.rb
blob: 45b3f529c4ce7c71b210dde3b87bf800b9dc596f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
class SnippetsController < ApplicationController
  before_filter :authenticate_user!
  before_filter :project
  layout "project"

  # Authorize
  before_filter :add_project_abilities

  # Allow read any snippet
  before_filter :authorize_read_snippet!

  # Allow write(create) snippet
  before_filter :authorize_write_snippet!, :only => [:new, :create]

  # Allow modify snippet
  before_filter :authorize_modify_snippet!, :only => [:edit, :update]

  # Allow destroy snippet
  before_filter :authorize_admin_snippet!, :only => [:destroy]

  respond_to :html

  def index
    @snippets = @project.snippets
  end

  def new
    @snippet = @project.snippets.new
  end

  def create
    @snippet = @project.snippets.new(params[:snippet])
    @snippet.author = current_user
    @snippet.save

    if @snippet.valid?
      redirect_to [@project, @snippet]
    else
      respond_with(@snippet)
    end
  end

  def edit
    @snippet = @project.snippets.find(params[:id])
  end

  def update
    @snippet = @project.snippets.find(params[:id])
    @snippet.update_attributes(params[:snippet])

    if @snippet.valid?
      redirect_to [@project, @snippet]
    else
      respond_with(@snippet)
    end
  end

  def show
    @snippet = @project.snippets.find(params[:id])
    @notes = @snippet.notes
    @note = @project.notes.new(:noteable => @snippet)
  end

  def destroy
    @snippet = @project.snippets.find(params[:id])

    return access_denied! unless can?(current_user, :admin_snippet, @snippet)

    @snippet.destroy

    redirect_to project_snippets_path(@project)
  end

  protected

  def authorize_modify_snippet!
    can?(current_user, :modify_snippet, @snippet)
  end

  def authorize_admin_snippet!
    can?(current_user, :admin_snippet, @snippet)
  end
end