Commit 68f4b59738b43d9d6969528188ec74a87546a29f
Exists in
master
and in
4 other branches
Merge pull request #1569 from jouve/simplify_controllers2
Simplify controllers and layout handling
Showing
29 changed files
with
93 additions
and
202 deletions
Show diff stats
app/controllers/application_controller.rb
| ... | ... | @@ -10,19 +10,17 @@ class ApplicationController < ActionController::Base |
| 10 | 10 | helper_method :abilities, :can? |
| 11 | 11 | |
| 12 | 12 | rescue_from Gitlab::Gitolite::AccessDenied do |exception| |
| 13 | - render "errors/gitolite", layout: "error", status: 500 | |
| 13 | + render "errors/gitolite", layout: "errors", status: 500 | |
| 14 | 14 | end |
| 15 | 15 | |
| 16 | 16 | rescue_from Encoding::CompatibilityError do |exception| |
| 17 | - render "errors/encoding", layout: "error", status: 500 | |
| 17 | + render "errors/encoding", layout: "errors", status: 500 | |
| 18 | 18 | end |
| 19 | 19 | |
| 20 | 20 | rescue_from ActiveRecord::RecordNotFound do |exception| |
| 21 | - render "errors/not_found", layout: "error", status: 404 | |
| 21 | + render "errors/not_found", layout: "errors", status: 404 | |
| 22 | 22 | end |
| 23 | 23 | |
| 24 | - layout :layout_by_resource | |
| 25 | - | |
| 26 | 24 | protected |
| 27 | 25 | |
| 28 | 26 | def reject_blocked! |
| ... | ... | @@ -43,14 +41,6 @@ class ApplicationController < ActionController::Base |
| 43 | 41 | end |
| 44 | 42 | end |
| 45 | 43 | |
| 46 | - def layout_by_resource | |
| 47 | - if devise_controller? | |
| 48 | - "devise_layout" | |
| 49 | - else | |
| 50 | - "application" | |
| 51 | - end | |
| 52 | - end | |
| 53 | - | |
| 54 | 44 | def set_current_user_for_mailer |
| 55 | 45 | MailerObserver.current_user = current_user |
| 56 | 46 | end |
| ... | ... | @@ -68,7 +58,7 @@ class ApplicationController < ActionController::Base |
| 68 | 58 | end |
| 69 | 59 | |
| 70 | 60 | def project |
| 71 | - @project ||= current_user.projects.find_by_code(params[:project_id]) | |
| 61 | + @project ||= current_user.projects.find_by_code(params[:project_id] || params[:id]) | |
| 72 | 62 | @project || render_404 |
| 73 | 63 | end |
| 74 | 64 | |
| ... | ... | @@ -85,15 +75,15 @@ class ApplicationController < ActionController::Base |
| 85 | 75 | end |
| 86 | 76 | |
| 87 | 77 | def access_denied! |
| 88 | - render "errors/access_denied", layout: "error", status: 404 | |
| 78 | + render "errors/access_denied", layout: "errors", status: 404 | |
| 89 | 79 | end |
| 90 | 80 | |
| 91 | 81 | def not_found! |
| 92 | - render "errors/not_found", layout: "error", status: 404 | |
| 82 | + render "errors/not_found", layout: "errors", status: 404 | |
| 93 | 83 | end |
| 94 | 84 | |
| 95 | 85 | def git_not_found! |
| 96 | - render "errors/git_not_found", layout: "error", status: 404 | |
| 86 | + render "errors/git_not_found", layout: "errors", status: 404 | |
| 97 | 87 | end |
| 98 | 88 | |
| 99 | 89 | def method_missing(method_sym, *arguments, &block) | ... | ... |
app/controllers/blame_controller.rb
| 1 | 1 | # Controller for viewing a file's blame |
| 2 | -class BlameController < ApplicationController | |
| 2 | +class BlameController < ProjectResourceController | |
| 3 | 3 | include ExtractsPath |
| 4 | 4 | |
| 5 | - layout "project" | |
| 6 | - | |
| 7 | - before_filter :project | |
| 8 | - | |
| 9 | 5 | # Authorize |
| 10 | - before_filter :add_project_abilities | |
| 11 | 6 | before_filter :authorize_read_project! |
| 12 | 7 | before_filter :authorize_code_access! |
| 13 | 8 | before_filter :require_non_empty_project | ... | ... |
app/controllers/blob_controller.rb
| 1 | 1 | # Controller for viewing a file's blame |
| 2 | -class BlobController < ApplicationController | |
| 2 | +class BlobController < ProjectResourceController | |
| 3 | 3 | include ExtractsPath |
| 4 | 4 | include Gitlab::Encode |
| 5 | 5 | |
| 6 | - layout "project" | |
| 7 | - | |
| 8 | - before_filter :project | |
| 9 | - | |
| 10 | 6 | # Authorize |
| 11 | - before_filter :add_project_abilities | |
| 12 | 7 | before_filter :authorize_read_project! |
| 13 | 8 | before_filter :authorize_code_access! |
| 14 | 9 | before_filter :require_non_empty_project | ... | ... |
app/controllers/commit_controller.rb
| 1 | 1 | # Controller for a specific Commit |
| 2 | 2 | # |
| 3 | 3 | # Not to be confused with CommitsController, plural. |
| 4 | -class CommitController < ApplicationController | |
| 5 | - before_filter :project | |
| 6 | - layout "project" | |
| 7 | - | |
| 4 | +class CommitController < ProjectResourceController | |
| 8 | 5 | # Authorize |
| 9 | - before_filter :add_project_abilities | |
| 10 | 6 | before_filter :authorize_read_project! |
| 11 | 7 | before_filter :authorize_code_access! |
| 12 | 8 | before_filter :require_non_empty_project | ... | ... |
app/controllers/commits_controller.rb
| 1 | 1 | require "base64" |
| 2 | 2 | |
| 3 | -class CommitsController < ApplicationController | |
| 4 | - before_filter :project | |
| 5 | - layout "project" | |
| 6 | - | |
| 3 | +class CommitsController < ProjectResourceController | |
| 7 | 4 | include ExtractsPath |
| 8 | 5 | |
| 9 | 6 | # Authorize |
| 10 | - before_filter :add_project_abilities | |
| 11 | 7 | before_filter :authorize_read_project! |
| 12 | 8 | before_filter :authorize_code_access! |
| 13 | 9 | before_filter :require_non_empty_project | ... | ... |
app/controllers/compare_controller.rb
| 1 | -class CompareController < ApplicationController | |
| 2 | - before_filter :project | |
| 3 | - layout "project" | |
| 4 | - | |
| 1 | +class CompareController < ProjectResourceController | |
| 5 | 2 | # Authorize |
| 6 | - before_filter :add_project_abilities | |
| 7 | 3 | before_filter :authorize_read_project! |
| 8 | 4 | before_filter :authorize_code_access! |
| 9 | 5 | before_filter :require_non_empty_project | ... | ... |
app/controllers/deploy_keys_controller.rb
| 1 | -class DeployKeysController < ApplicationController | |
| 1 | +class DeployKeysController < ProjectResourceController | |
| 2 | 2 | respond_to :html |
| 3 | - layout "project" | |
| 4 | - before_filter :project | |
| 5 | 3 | |
| 6 | 4 | # Authorize |
| 7 | - before_filter :add_project_abilities | |
| 8 | 5 | before_filter :authorize_admin_project! |
| 9 | 6 | |
| 10 | - def project | |
| 11 | - @project ||= Project.find_by_code(params[:project_id]) | |
| 12 | - end | |
| 13 | - | |
| 14 | 7 | def index |
| 15 | 8 | @keys = @project.deploy_keys.all |
| 16 | 9 | end | ... | ... |
app/controllers/errors_controller.rb
app/controllers/hooks_controller.rb
| 1 | -class HooksController < ApplicationController | |
| 2 | - before_filter :project | |
| 3 | - layout "project" | |
| 4 | - | |
| 1 | +class HooksController < ProjectResourceController | |
| 5 | 2 | # Authorize |
| 6 | - before_filter :add_project_abilities | |
| 7 | 3 | before_filter :authorize_read_project! |
| 8 | 4 | before_filter :authorize_admin_project!, only: [:new, :create, :destroy] |
| 9 | 5 | ... | ... |
app/controllers/issues_controller.rb
| 1 | -class IssuesController < ApplicationController | |
| 2 | - before_filter :project | |
| 1 | +class IssuesController < ProjectResourceController | |
| 3 | 2 | before_filter :module_enabled |
| 4 | 3 | before_filter :issue, only: [:edit, :update, :destroy, :show] |
| 5 | 4 | helper_method :issues_filter |
| 6 | 5 | |
| 7 | - layout "project" | |
| 8 | - | |
| 9 | - # Authorize | |
| 10 | - before_filter :add_project_abilities | |
| 11 | - | |
| 12 | 6 | # Allow read any issue |
| 13 | 7 | before_filter :authorize_read_issue! |
| 14 | 8 | ... | ... |
app/controllers/labels_controller.rb
| 1 | -class LabelsController < ApplicationController | |
| 2 | - before_filter :project | |
| 1 | +class LabelsController < ProjectResourceController | |
| 3 | 2 | before_filter :module_enabled |
| 4 | 3 | |
| 5 | - layout "project" | |
| 6 | - | |
| 7 | - # Authorize | |
| 8 | - before_filter :add_project_abilities | |
| 9 | - | |
| 10 | 4 | # Allow read any issue |
| 11 | 5 | before_filter :authorize_read_issue! |
| 12 | 6 | ... | ... |
app/controllers/merge_requests_controller.rb
| 1 | -class MergeRequestsController < ApplicationController | |
| 2 | - before_filter :project | |
| 1 | +class MergeRequestsController < ProjectResourceController | |
| 3 | 2 | before_filter :module_enabled |
| 4 | 3 | before_filter :merge_request, only: [:edit, :update, :destroy, :show, :commits, :diffs, :automerge, :automerge_check, :raw] |
| 5 | 4 | before_filter :validates_merge_request, only: [:show, :diffs, :raw] |
| 6 | 5 | before_filter :define_show_vars, only: [:show, :diffs] |
| 7 | - layout "project" | |
| 8 | - | |
| 9 | - # Authorize | |
| 10 | - before_filter :add_project_abilities | |
| 11 | 6 | |
| 12 | 7 | # Allow read any merge_request |
| 13 | 8 | before_filter :authorize_read_merge_request! | ... | ... |
app/controllers/milestones_controller.rb
| 1 | -class MilestonesController < ApplicationController | |
| 2 | - before_filter :project | |
| 1 | +class MilestonesController < ProjectResourceController | |
| 3 | 2 | before_filter :module_enabled |
| 4 | 3 | before_filter :milestone, only: [:edit, :update, :destroy, :show] |
| 5 | - layout "project" | |
| 6 | - | |
| 7 | - # Authorize | |
| 8 | - before_filter :add_project_abilities | |
| 9 | 4 | |
| 10 | 5 | # Allow read any milestone |
| 11 | 6 | before_filter :authorize_read_milestone! | ... | ... |
app/controllers/notes_controller.rb
| 1 | -class NotesController < ApplicationController | |
| 2 | - before_filter :project | |
| 3 | - | |
| 1 | +class NotesController < ProjectResourceController | |
| 4 | 2 | # Authorize |
| 5 | - before_filter :add_project_abilities | |
| 6 | - | |
| 7 | 3 | before_filter :authorize_read_note! |
| 8 | 4 | before_filter :authorize_write_note!, only: [:create] |
| 9 | 5 | ... | ... |
app/controllers/profile_controller.rb
app/controllers/projects_controller.rb
| 1 | 1 | require Rails.root.join('lib', 'gitlab', 'graph_commit') |
| 2 | 2 | |
| 3 | -class ProjectsController < ApplicationController | |
| 4 | - before_filter :project, except: [:index, :new, :create] | |
| 5 | - layout :determine_layout | |
| 3 | +class ProjectsController < ProjectResourceController | |
| 4 | + skip_before_filter :project, only: [:new, :create] | |
| 6 | 5 | |
| 7 | 6 | # Authorize |
| 8 | - before_filter :add_project_abilities | |
| 9 | 7 | before_filter :authorize_read_project!, except: [:index, :new, :create] |
| 10 | 8 | before_filter :authorize_admin_project!, only: [:edit, :update, :destroy] |
| 11 | 9 | before_filter :require_non_empty_project, only: [:blob, :tree, :graph] |
| 12 | 10 | |
| 11 | + layout 'application', only: [:new, :create] | |
| 12 | + | |
| 13 | 13 | def new |
| 14 | 14 | @project = Project.new |
| 15 | 15 | end |
| ... | ... | @@ -93,19 +93,4 @@ class ProjectsController < ApplicationController |
| 93 | 93 | format.html { redirect_to root_path } |
| 94 | 94 | end |
| 95 | 95 | end |
| 96 | - | |
| 97 | - protected | |
| 98 | - | |
| 99 | - def project | |
| 100 | - @project ||= Project.find_by_code(params[:id]) | |
| 101 | - @project || render_404 | |
| 102 | - end | |
| 103 | - | |
| 104 | - def determine_layout | |
| 105 | - if @project && !@project.new_record? | |
| 106 | - "project" | |
| 107 | - else | |
| 108 | - "application" | |
| 109 | - end | |
| 110 | - end | |
| 111 | 96 | end | ... | ... |
app/controllers/protected_branches_controller.rb
| 1 | -class ProtectedBranchesController < ApplicationController | |
| 2 | - before_filter :project | |
| 3 | - | |
| 1 | +class ProtectedBranchesController < ProjectResourceController | |
| 4 | 2 | # Authorize |
| 5 | - before_filter :add_project_abilities | |
| 6 | 3 | before_filter :authorize_read_project! |
| 7 | 4 | before_filter :require_non_empty_project |
| 8 | 5 | |
| 9 | 6 | before_filter :authorize_admin_project!, only: [:destroy, :create] |
| 10 | 7 | |
| 11 | - layout "project" | |
| 12 | - | |
| 13 | 8 | def index |
| 14 | 9 | @branches = @project.protected_branches.all |
| 15 | 10 | @protected_branch = @project.protected_branches.new | ... | ... |
app/controllers/refs_controller.rb
| 1 | -class RefsController < ApplicationController | |
| 1 | +class RefsController < ProjectResourceController | |
| 2 | 2 | include Gitlab::Encode |
| 3 | - before_filter :project | |
| 4 | 3 | |
| 5 | 4 | # Authorize |
| 6 | - before_filter :add_project_abilities | |
| 7 | 5 | before_filter :authorize_read_project! |
| 8 | 6 | before_filter :authorize_code_access! |
| 9 | 7 | before_filter :require_non_empty_project |
| ... | ... | @@ -11,11 +9,9 @@ class RefsController < ApplicationController |
| 11 | 9 | before_filter :ref |
| 12 | 10 | before_filter :define_tree_vars, only: [:blob, :logs_tree] |
| 13 | 11 | |
| 14 | - layout "project" | |
| 15 | - | |
| 16 | - def switch | |
| 17 | - respond_to do |format| | |
| 18 | - format.html do | |
| 12 | + def switch | |
| 13 | + respond_to do |format| | |
| 14 | + format.html do | |
| 19 | 15 | new_path = if params[:destination] == "tree" |
| 20 | 16 | project_tree_path(@project, @ref) |
| 21 | 17 | else | ... | ... |
app/controllers/repositories_controller.rb
| 1 | -class RepositoriesController < ApplicationController | |
| 2 | - before_filter :project | |
| 3 | - | |
| 1 | +class RepositoriesController < ProjectResourceController | |
| 4 | 2 | # Authorize |
| 5 | - before_filter :add_project_abilities | |
| 6 | 3 | before_filter :authorize_read_project! |
| 7 | 4 | before_filter :authorize_code_access! |
| 8 | 5 | before_filter :require_non_empty_project |
| 9 | 6 | |
| 10 | - layout "project" | |
| 11 | - | |
| 12 | 7 | def show |
| 13 | 8 | @activities = @project.commits_with_refs(20) |
| 14 | 9 | end | ... | ... |
app/controllers/snippets_controller.rb
| 1 | -class SnippetsController < ApplicationController | |
| 2 | - before_filter :project | |
| 1 | +class SnippetsController < ProjectResourceController | |
| 3 | 2 | before_filter :snippet, only: [:show, :edit, :destroy, :update, :raw] |
| 4 | - layout "project" | |
| 5 | - | |
| 6 | - # Authorize | |
| 7 | - before_filter :add_project_abilities | |
| 8 | 3 | |
| 9 | 4 | # Allow read any snippet |
| 10 | 5 | before_filter :authorize_read_snippet! | ... | ... |
app/controllers/team_members_controller.rb
| 1 | -class TeamMembersController < ApplicationController | |
| 2 | - before_filter :project | |
| 3 | - layout "project" | |
| 4 | - | |
| 1 | +class TeamMembersController < ProjectResourceController | |
| 5 | 2 | # Authorize |
| 6 | - before_filter :add_project_abilities | |
| 7 | 3 | before_filter :authorize_read_project! |
| 8 | 4 | before_filter :authorize_admin_project!, except: [:index, :show] |
| 9 | 5 | ... | ... |
app/controllers/tree_controller.rb
| 1 | 1 | # Controller for viewing a repository's file structure |
| 2 | -class TreeController < ApplicationController | |
| 2 | +class TreeController < ProjectResourceController | |
| 3 | 3 | include ExtractsPath |
| 4 | 4 | |
| 5 | - layout "project" | |
| 6 | - | |
| 7 | - before_filter :project | |
| 8 | - | |
| 9 | 5 | # Authorize |
| 10 | - before_filter :add_project_abilities | |
| 11 | 6 | before_filter :authorize_read_project! |
| 12 | 7 | before_filter :authorize_code_access! |
| 13 | 8 | before_filter :require_non_empty_project | ... | ... |
app/controllers/wikis_controller.rb
| 1 | -class WikisController < ApplicationController | |
| 2 | - before_filter :project | |
| 3 | - before_filter :add_project_abilities | |
| 1 | +class WikisController < ProjectResourceController | |
| 4 | 2 | before_filter :authorize_read_wiki! |
| 5 | 3 | before_filter :authorize_write_wiki!, only: [:edit, :create, :history] |
| 6 | 4 | before_filter :authorize_admin_wiki!, only: :destroy |
| 7 | - layout "project" | |
| 8 | 5 | |
| 9 | 6 | def pages |
| 10 | 7 | @wikis = @project.wikis.group(:slug).order("created_at") | ... | ... |
app/views/layouts/error.html.haml
app/views/layouts/project.html.haml
| ... | ... | @@ -1,41 +0,0 @@ |
| 1 | -!!! 5 | |
| 2 | -%html{ lang: "en"} | |
| 3 | - = render "layouts/head" | |
| 4 | - %body{class: "#{app_theme} project"} | |
| 5 | - = render "layouts/flash" | |
| 6 | - = render "layouts/head_panel", title: @project.name | |
| 7 | - .container | |
| 8 | - %ul.main_menu | |
| 9 | - = nav_link(html_options: {class: "home #{project_tab_class}"}) do | |
| 10 | - = link_to @project.code, project_path(@project), title: "Project" | |
| 11 | - | |
| 12 | - - if @project.repo_exists? | |
| 13 | - - if can? current_user, :download_code, @project | |
| 14 | - = nav_link(controller: %w(tree blob blame)) do | |
| 15 | - = link_to 'Files', project_tree_path(@project, @ref || @project.root_ref) | |
| 16 | - = nav_link(controller: %w(commit commits compare repositories protected_branches)) do | |
| 17 | - = link_to "Commits", project_commits_path(@project, @ref || @project.root_ref) | |
| 18 | - = nav_link(path: 'projects#graph') do | |
| 19 | - = link_to "Network", graph_project_path(@project) | |
| 20 | - | |
| 21 | - - if @project.issues_enabled | |
| 22 | - = nav_link(controller: %w(issues milestones labels)) do | |
| 23 | - = link_to project_issues_filter_path(@project) do | |
| 24 | - Issues | |
| 25 | - %span.count.issue_counter= @project.issues.opened.count | |
| 26 | - | |
| 27 | - - if @project.repo_exists? && @project.merge_requests_enabled | |
| 28 | - = nav_link(controller: :merge_requests) do | |
| 29 | - = link_to project_merge_requests_path(@project) do | |
| 30 | - Merge Requests | |
| 31 | - %span.count.merge_counter= @project.merge_requests.opened.count | |
| 32 | - | |
| 33 | - - if @project.wall_enabled | |
| 34 | - = nav_link(path: 'projects#wall') do | |
| 35 | - = link_to 'Wall', wall_project_path(@project) | |
| 36 | - | |
| 37 | - - if @project.wiki_enabled | |
| 38 | - = nav_link(controller: :wikis) do | |
| 39 | - = link_to 'Wiki', project_wiki_path(@project, :index) | |
| 40 | - | |
| 41 | - .content= yield |
| ... | ... | @@ -0,0 +1,41 @@ |
| 1 | +!!! 5 | |
| 2 | +%html{ lang: "en"} | |
| 3 | + = render "layouts/head" | |
| 4 | + %body{class: "#{app_theme} project"} | |
| 5 | + = render "layouts/flash" | |
| 6 | + = render "layouts/head_panel", title: @project.name | |
| 7 | + .container | |
| 8 | + %ul.main_menu | |
| 9 | + = nav_link(html_options: {class: "home #{project_tab_class}"}) do | |
| 10 | + = link_to @project.code, project_path(@project), title: "Project" | |
| 11 | + | |
| 12 | + - if @project.repo_exists? | |
| 13 | + - if can? current_user, :download_code, @project | |
| 14 | + = nav_link(controller: %w(tree blob blame)) do | |
| 15 | + = link_to 'Files', project_tree_path(@project, @ref || @project.root_ref) | |
| 16 | + = nav_link(controller: %w(commit commits compare repositories protected_branches)) do | |
| 17 | + = link_to "Commits", project_commits_path(@project, @ref || @project.root_ref) | |
| 18 | + = nav_link(path: 'projects#graph') do | |
| 19 | + = link_to "Network", graph_project_path(@project) | |
| 20 | + | |
| 21 | + - if @project.issues_enabled | |
| 22 | + = nav_link(controller: %w(issues milestones labels)) do | |
| 23 | + = link_to project_issues_filter_path(@project) do | |
| 24 | + Issues | |
| 25 | + %span.count.issue_counter= @project.issues.opened.count | |
| 26 | + | |
| 27 | + - if @project.repo_exists? && @project.merge_requests_enabled | |
| 28 | + = nav_link(controller: :merge_requests) do | |
| 29 | + = link_to project_merge_requests_path(@project) do | |
| 30 | + Merge Requests | |
| 31 | + %span.count.merge_counter= @project.merge_requests.opened.count | |
| 32 | + | |
| 33 | + - if @project.wall_enabled | |
| 34 | + = nav_link(path: 'projects#wall') do | |
| 35 | + = link_to 'Wall', wall_project_path(@project) | |
| 36 | + | |
| 37 | + - if @project.wiki_enabled | |
| 38 | + = nav_link(controller: :wikis) do | |
| 39 | + = link_to 'Wiki', project_wiki_path(@project, :index) | |
| 40 | + | |
| 41 | + .content= yield | ... | ... |
config/application.rb