Commit e90aebe259b1f81cb2523099cab4626b9276e751
1 parent
9bd1983e
Exists in
master
and in
4 other branches
Personal snippets controlelr refactored
Showing
2 changed files
with
33 additions
and
31 deletions
Show diff stats
app/controllers/snippets_controller.rb
1 | -class SnippetsController < ProjectResourceController | |
2 | - before_filter :module_enabled | |
1 | +class SnippetsController < ApplicationController | |
3 | 2 | before_filter :snippet, only: [:show, :edit, :destroy, :update, :raw] |
4 | 3 | |
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 | 4 | # Allow modify snippet |
12 | 5 | before_filter :authorize_modify_snippet!, only: [:edit, :update] |
13 | 6 | |
... | ... | @@ -17,22 +10,39 @@ class SnippetsController < ProjectResourceController |
17 | 10 | respond_to :html |
18 | 11 | |
19 | 12 | def index |
20 | - @snippets = @project.snippets.fresh.non_expired | |
13 | + @snippets = Snippet.public.fresh.non_expired.page(params[:page]).per(20) | |
14 | + end | |
15 | + | |
16 | + def user_index | |
17 | + @user = User.find_by_username(params[:username]) | |
18 | + | |
19 | + @snippets = @current_user.snippets.fresh.non_expired | |
20 | + | |
21 | + @snippets = case params[:scope] | |
22 | + when 'public' then | |
23 | + @snippets.public | |
24 | + when 'private' then | |
25 | + @snippets.private | |
26 | + else | |
27 | + @snippets | |
28 | + end | |
29 | + | |
30 | + @snippets = @snippets.page(params[:page]).per(20) | |
21 | 31 | end |
22 | 32 | |
23 | 33 | def new |
24 | - @snippet = @project.snippets.new | |
34 | + @snippet = PersonalSnippet.new | |
25 | 35 | end |
26 | 36 | |
27 | 37 | def create |
28 | - @snippet = @project.snippets.new(params[:snippet]) | |
38 | + @snippet = PersonalSnippet.new(params[:personal_snippet]) | |
29 | 39 | @snippet.author = current_user |
30 | 40 | @snippet.save |
31 | 41 | |
32 | 42 | if @snippet.valid? |
33 | - redirect_to [@project, @snippet] | |
43 | + redirect_to snippet_path(@snippet) | |
34 | 44 | else |
35 | - respond_with(@snippet) | |
45 | + respond_with @snippet | |
36 | 46 | end |
37 | 47 | end |
38 | 48 | |
... | ... | @@ -40,27 +50,24 @@ class SnippetsController < ProjectResourceController |
40 | 50 | end |
41 | 51 | |
42 | 52 | def update |
43 | - @snippet.update_attributes(params[:snippet]) | |
53 | + @snippet.update_attributes(params[:personal_snippet]) | |
44 | 54 | |
45 | 55 | if @snippet.valid? |
46 | - redirect_to [@project, @snippet] | |
56 | + redirect_to snippet_path(@snippet) | |
47 | 57 | else |
48 | - respond_with(@snippet) | |
58 | + respond_with @snippet | |
49 | 59 | end |
50 | 60 | end |
51 | 61 | |
52 | 62 | def show |
53 | - @note = @project.notes.new(noteable: @snippet) | |
54 | - @target_type = :snippet | |
55 | - @target_id = @snippet.id | |
56 | 63 | end |
57 | 64 | |
58 | 65 | def destroy |
59 | - return access_denied! unless can?(current_user, :admin_snippet, @snippet) | |
66 | + return access_denied! unless can?(current_user, :admin_personal_snippet, @snippet) | |
60 | 67 | |
61 | 68 | @snippet.destroy |
62 | 69 | |
63 | - redirect_to project_snippet_path(@project) | |
70 | + redirect_to snippets_path | |
64 | 71 | end |
65 | 72 | |
66 | 73 | def raw |
... | ... | @@ -75,18 +82,14 @@ class SnippetsController < ProjectResourceController |
75 | 82 | protected |
76 | 83 | |
77 | 84 | def snippet |
78 | - @snippet ||= @project.snippets.find(params[:id]) | |
85 | + @snippet ||= PersonalSnippet.find(params[:id]) | |
79 | 86 | end |
80 | 87 | |
81 | 88 | def authorize_modify_snippet! |
82 | - return render_404 unless can?(current_user, :modify_snippet, @snippet) | |
89 | + return render_404 unless can?(current_user, :modify_personal_snippet, @snippet) | |
83 | 90 | end |
84 | 91 | |
85 | 92 | 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 | |
93 | + return render_404 unless can?(current_user, :admin_personal_snippet, @snippet) | |
91 | 94 | end |
92 | 95 | end | ... | ... |
app/models/ability.rb
... | ... | @@ -8,7 +8,7 @@ class Ability |
8 | 8 | when "Issue" then issue_abilities(user, subject) |
9 | 9 | when "Note" then note_abilities(user, subject) |
10 | 10 | when "ProjectSnippet" then project_snippet_abilities(user, subject) |
11 | - when "Snippet" then snippet_abilities(user, subject) | |
11 | + when "PersonalSnippet" then personal_snippet_abilities(user, subject) | |
12 | 12 | when "MergeRequest" then merge_request_abilities(user, subject) |
13 | 13 | when "Group", "Namespace" then group_abilities(user, subject) |
14 | 14 | when "UserTeam" then user_team_abilities(user, subject) |
... | ... | @@ -135,8 +135,7 @@ class Ability |
135 | 135 | rules.flatten |
136 | 136 | end |
137 | 137 | |
138 | - | |
139 | - [:issue, :note, :project_snippet, :snippet, :merge_request].each do |name| | |
138 | + [:issue, :note, :project_snippet, :personal_snippet, :merge_request].each do |name| | |
140 | 139 | define_method "#{name}_abilities" do |user, subject| |
141 | 140 | if subject.author == user |
142 | 141 | [ | ... | ... |