Commit cc869d5dc101ea1175e308d8532064f06072d08b
1 parent
77faffbd
Exists in
master
and in
4 other branches
Private field added to snippet
Showing
3 changed files
with
99 additions
and
1 deletions
Show diff stats
| ... | ... | @@ -0,0 +1,92 @@ |
| 1 | +class SnippetsController < ProjectResourceController | |
| 2 | + before_filter :module_enabled | |
| 3 | + before_filter :snippet, only: [:show, :edit, :destroy, :update, :raw] | |
| 4 | + | |
| 5 | + # Allow read any snippet | |
| 6 | + before_filter :authorize_read_snippet! | |
| 7 | + | |
| 8 | + # Allow write(create) snippet | |
| 9 | + before_filter :authorize_write_snippet!, only: [:new, :create] | |
| 10 | + | |
| 11 | + # Allow modify snippet | |
| 12 | + before_filter :authorize_modify_snippet!, only: [:edit, :update] | |
| 13 | + | |
| 14 | + # Allow destroy snippet | |
| 15 | + before_filter :authorize_admin_snippet!, only: [:destroy] | |
| 16 | + | |
| 17 | + respond_to :html | |
| 18 | + | |
| 19 | + def index | |
| 20 | + @snippets = @project.snippets.fresh.non_expired | |
| 21 | + end | |
| 22 | + | |
| 23 | + def new | |
| 24 | + @snippet = @project.snippets.new | |
| 25 | + end | |
| 26 | + | |
| 27 | + def create | |
| 28 | + @snippet = @project.snippets.new(params[:snippet]) | |
| 29 | + @snippet.author = current_user | |
| 30 | + @snippet.save | |
| 31 | + | |
| 32 | + if @snippet.valid? | |
| 33 | + redirect_to [@project, @snippet] | |
| 34 | + else | |
| 35 | + respond_with(@snippet) | |
| 36 | + end | |
| 37 | + end | |
| 38 | + | |
| 39 | + def edit | |
| 40 | + end | |
| 41 | + | |
| 42 | + def update | |
| 43 | + @snippet.update_attributes(params[:snippet]) | |
| 44 | + | |
| 45 | + if @snippet.valid? | |
| 46 | + redirect_to [@project, @snippet] | |
| 47 | + else | |
| 48 | + respond_with(@snippet) | |
| 49 | + end | |
| 50 | + end | |
| 51 | + | |
| 52 | + def show | |
| 53 | + @note = @project.notes.new(noteable: @snippet) | |
| 54 | + @target_type = :snippet | |
| 55 | + @target_id = @snippet.id | |
| 56 | + end | |
| 57 | + | |
| 58 | + def destroy | |
| 59 | + return access_denied! unless can?(current_user, :admin_snippet, @snippet) | |
| 60 | + | |
| 61 | + @snippet.destroy | |
| 62 | + | |
| 63 | + redirect_to project_snippets_path(@project) | |
| 64 | + end | |
| 65 | + | |
| 66 | + def raw | |
| 67 | + send_data( | |
| 68 | + @snippet.content, | |
| 69 | + type: "text/plain", | |
| 70 | + disposition: 'inline', | |
| 71 | + filename: @snippet.file_name | |
| 72 | + ) | |
| 73 | + end | |
| 74 | + | |
| 75 | + protected | |
| 76 | + | |
| 77 | + def snippet | |
| 78 | + @snippet ||= @project.snippets.find(params[:id]) | |
| 79 | + end | |
| 80 | + | |
| 81 | + def authorize_modify_snippet! | |
| 82 | + return render_404 unless can?(current_user, :modify_snippet, @snippet) | |
| 83 | + end | |
| 84 | + | |
| 85 | + def authorize_admin_snippet! | |
| 86 | + return render_404 unless can?(current_user, :admin_snippet, @snippet) | |
| 87 | + end | |
| 88 | + | |
| 89 | + def module_enabled | |
| 90 | + return render_404 unless @project.snippets_enabled | |
| 91 | + end | |
| 92 | +end | ... | ... |
db/schema.rb
| ... | ... | @@ -11,7 +11,7 @@ |
| 11 | 11 | # |
| 12 | 12 | # It's strongly recommended to check this file into your version control system. |
| 13 | 13 | |
| 14 | -ActiveRecord::Schema.define(:version => 20130318212250) do | |
| 14 | +ActiveRecord::Schema.define(:version => 20130323174317) do | |
| 15 | 15 | |
| 16 | 16 | create_table "events", :force => true do |t| |
| 17 | 17 | t.string "target_type" |
| ... | ... | @@ -190,6 +190,7 @@ ActiveRecord::Schema.define(:version => 20130318212250) do |
| 190 | 190 | t.datetime "updated_at", :null => false |
| 191 | 191 | t.string "file_name" |
| 192 | 192 | t.datetime "expires_at" |
| 193 | + t.boolean "private" | |
| 193 | 194 | end |
| 194 | 195 | |
| 195 | 196 | add_index "snippets", ["created_at"], :name => "index_snippets_on_created_at" | ... | ... |