Skip to content

Commit

Permalink
Merge pull request #1569 from jouve/simplify_controllers2
Browse files Browse the repository at this point in the history
Simplify controllers and layout handling
  • Loading branch information
dzaporozhets committed Sep 27, 2012
2 parents 3e9836a + be18397 commit 68f4b59
Show file tree
Hide file tree
Showing 27 changed files with 42 additions and 151 deletions.
24 changes: 7 additions & 17 deletions app/controllers/application_controller.rb
Expand Up @@ -10,19 +10,17 @@ class ApplicationController < ActionController::Base
helper_method :abilities, :can?

rescue_from Gitlab::Gitolite::AccessDenied do |exception|
render "errors/gitolite", layout: "error", status: 500
render "errors/gitolite", layout: "errors", status: 500
end

rescue_from Encoding::CompatibilityError do |exception|
render "errors/encoding", layout: "error", status: 500
render "errors/encoding", layout: "errors", status: 500
end

rescue_from ActiveRecord::RecordNotFound do |exception|
render "errors/not_found", layout: "error", status: 404
render "errors/not_found", layout: "errors", status: 404
end

layout :layout_by_resource

protected

def reject_blocked!
Expand All @@ -43,14 +41,6 @@ def after_sign_in_path_for resource
end
end

def layout_by_resource
if devise_controller?
"devise_layout"
else
"application"
end
end

def set_current_user_for_mailer
MailerObserver.current_user = current_user
end
Expand All @@ -68,7 +58,7 @@ def can?(object, action, subject)
end

def project
@project ||= current_user.projects.find_by_code(params[:project_id])
@project ||= current_user.projects.find_by_code(params[:project_id] || params[:id])
@project || render_404
end

Expand All @@ -85,15 +75,15 @@ def authorize_code_access!
end

def access_denied!
render "errors/access_denied", layout: "error", status: 404
render "errors/access_denied", layout: "errors", status: 404
end

def not_found!
render "errors/not_found", layout: "error", status: 404
render "errors/not_found", layout: "errors", status: 404
end

def git_not_found!
render "errors/git_not_found", layout: "error", status: 404
render "errors/git_not_found", layout: "errors", status: 404
end

def method_missing(method_sym, *arguments, &block)
Expand Down
7 changes: 1 addition & 6 deletions app/controllers/blame_controller.rb
@@ -1,13 +1,8 @@
# Controller for viewing a file's blame
class BlameController < ApplicationController
class BlameController < ProjectResourceController
include ExtractsPath

layout "project"

before_filter :project

# Authorize
before_filter :add_project_abilities
before_filter :authorize_read_project!
before_filter :authorize_code_access!
before_filter :require_non_empty_project
Expand Down
7 changes: 1 addition & 6 deletions app/controllers/blob_controller.rb
@@ -1,14 +1,9 @@
# Controller for viewing a file's blame
class BlobController < ApplicationController
class BlobController < ProjectResourceController
include ExtractsPath
include Gitlab::Encode

layout "project"

before_filter :project

# Authorize
before_filter :add_project_abilities
before_filter :authorize_read_project!
before_filter :authorize_code_access!
before_filter :require_non_empty_project
Expand Down
6 changes: 1 addition & 5 deletions app/controllers/commit_controller.rb
@@ -1,12 +1,8 @@
# Controller for a specific Commit
#
# Not to be confused with CommitsController, plural.
class CommitController < ApplicationController
before_filter :project
layout "project"

class CommitController < ProjectResourceController
# Authorize
before_filter :add_project_abilities
before_filter :authorize_read_project!
before_filter :authorize_code_access!
before_filter :require_non_empty_project
Expand Down
6 changes: 1 addition & 5 deletions app/controllers/commits_controller.rb
@@ -1,13 +1,9 @@
require "base64"

class CommitsController < ApplicationController
before_filter :project
layout "project"

class CommitsController < ProjectResourceController
include ExtractsPath

# Authorize
before_filter :add_project_abilities
before_filter :authorize_read_project!
before_filter :authorize_code_access!
before_filter :require_non_empty_project
Expand Down
6 changes: 1 addition & 5 deletions app/controllers/compare_controller.rb
@@ -1,9 +1,5 @@
class CompareController < ApplicationController
before_filter :project
layout "project"

class CompareController < ProjectResourceController
# Authorize
before_filter :add_project_abilities
before_filter :authorize_read_project!
before_filter :authorize_code_access!
before_filter :require_non_empty_project
Expand Down
9 changes: 1 addition & 8 deletions app/controllers/deploy_keys_controller.rb
@@ -1,16 +1,9 @@
class DeployKeysController < ApplicationController
class DeployKeysController < ProjectResourceController
respond_to :html
layout "project"
before_filter :project

# Authorize
before_filter :add_project_abilities
before_filter :authorize_admin_project!

def project
@project ||= Project.find_by_code(params[:project_id])
end

def index
@keys = @project.deploy_keys.all
end
Expand Down
2 changes: 0 additions & 2 deletions app/controllers/errors_controller.rb
@@ -1,6 +1,4 @@
class ErrorsController < ApplicationController
layout "error"

def githost
render "errors/gitolite"
end
Expand Down
6 changes: 1 addition & 5 deletions app/controllers/hooks_controller.rb
@@ -1,9 +1,5 @@
class HooksController < ApplicationController
before_filter :project
layout "project"

class HooksController < ProjectResourceController
# Authorize
before_filter :add_project_abilities
before_filter :authorize_read_project!
before_filter :authorize_admin_project!, only: [:new, :create, :destroy]

Expand Down
8 changes: 1 addition & 7 deletions app/controllers/issues_controller.rb
@@ -1,14 +1,8 @@
class IssuesController < ApplicationController
before_filter :project
class IssuesController < ProjectResourceController
before_filter :module_enabled
before_filter :issue, only: [:edit, :update, :destroy, :show]
helper_method :issues_filter

layout "project"

# Authorize
before_filter :add_project_abilities

# Allow read any issue
before_filter :authorize_read_issue!

Expand Down
8 changes: 1 addition & 7 deletions app/controllers/labels_controller.rb
@@ -1,12 +1,6 @@
class LabelsController < ApplicationController
before_filter :project
class LabelsController < ProjectResourceController
before_filter :module_enabled

layout "project"

# Authorize
before_filter :add_project_abilities

# Allow read any issue
before_filter :authorize_read_issue!

Expand Down
7 changes: 1 addition & 6 deletions app/controllers/merge_requests_controller.rb
@@ -1,13 +1,8 @@
class MergeRequestsController < ApplicationController
before_filter :project
class MergeRequestsController < ProjectResourceController
before_filter :module_enabled
before_filter :merge_request, only: [:edit, :update, :destroy, :show, :commits, :diffs, :automerge, :automerge_check, :raw]
before_filter :validates_merge_request, only: [:show, :diffs, :raw]
before_filter :define_show_vars, only: [:show, :diffs]
layout "project"

# Authorize
before_filter :add_project_abilities

# Allow read any merge_request
before_filter :authorize_read_merge_request!
Expand Down
7 changes: 1 addition & 6 deletions app/controllers/milestones_controller.rb
@@ -1,11 +1,6 @@
class MilestonesController < ApplicationController
before_filter :project
class MilestonesController < ProjectResourceController
before_filter :module_enabled
before_filter :milestone, only: [:edit, :update, :destroy, :show]
layout "project"

# Authorize
before_filter :add_project_abilities

# Allow read any milestone
before_filter :authorize_read_milestone!
Expand Down
6 changes: 1 addition & 5 deletions app/controllers/notes_controller.rb
@@ -1,9 +1,5 @@
class NotesController < ApplicationController
before_filter :project

class NotesController < ProjectResourceController
# Authorize
before_filter :add_project_abilities

before_filter :authorize_read_note!
before_filter :authorize_write_note!, only: [:create]

Expand Down
1 change: 0 additions & 1 deletion app/controllers/profile_controller.rb
@@ -1,5 +1,4 @@
class ProfileController < ApplicationController
layout "profile"
before_filter :user

def show
Expand Down
5 changes: 5 additions & 0 deletions app/controllers/project_resource_controller.rb
@@ -0,0 +1,5 @@
class ProjectResourceController < ApplicationController
before_filter :project
# Authorize
before_filter :add_project_abilities
end
23 changes: 4 additions & 19 deletions app/controllers/projects_controller.rb
@@ -1,15 +1,15 @@
require Rails.root.join('lib', 'gitlab', 'graph_commit')

class ProjectsController < ApplicationController
before_filter :project, except: [:index, :new, :create]
layout :determine_layout
class ProjectsController < ProjectResourceController
skip_before_filter :project, only: [:new, :create]

# Authorize
before_filter :add_project_abilities
before_filter :authorize_read_project!, except: [:index, :new, :create]
before_filter :authorize_admin_project!, only: [:edit, :update, :destroy]
before_filter :require_non_empty_project, only: [:blob, :tree, :graph]

layout 'application', only: [:new, :create]

def new
@project = Project.new
end
Expand Down Expand Up @@ -93,19 +93,4 @@ def destroy
format.html { redirect_to root_path }
end
end

protected

def project
@project ||= Project.find_by_code(params[:id])
@project || render_404
end

def determine_layout
if @project && !@project.new_record?
"project"
else
"application"
end
end
end
7 changes: 1 addition & 6 deletions app/controllers/protected_branches_controller.rb
@@ -1,15 +1,10 @@
class ProtectedBranchesController < ApplicationController
before_filter :project

class ProtectedBranchesController < ProjectResourceController
# Authorize
before_filter :add_project_abilities
before_filter :authorize_read_project!
before_filter :require_non_empty_project

before_filter :authorize_admin_project!, only: [:destroy, :create]

layout "project"

def index
@branches = @project.protected_branches.all
@protected_branch = @project.protected_branches.new
Expand Down
12 changes: 4 additions & 8 deletions app/controllers/refs_controller.rb
@@ -1,21 +1,17 @@
class RefsController < ApplicationController
class RefsController < ProjectResourceController
include Gitlab::Encode
before_filter :project

# Authorize
before_filter :add_project_abilities
before_filter :authorize_read_project!
before_filter :authorize_code_access!
before_filter :require_non_empty_project

before_filter :ref
before_filter :define_tree_vars, only: [:blob, :logs_tree]

layout "project"

def switch
respond_to do |format|
format.html do
def switch
respond_to do |format|
format.html do
new_path = if params[:destination] == "tree"
project_tree_path(@project, @ref)
else
Expand Down
7 changes: 1 addition & 6 deletions app/controllers/repositories_controller.rb
@@ -1,14 +1,9 @@
class RepositoriesController < ApplicationController
before_filter :project

class RepositoriesController < ProjectResourceController
# Authorize
before_filter :add_project_abilities
before_filter :authorize_read_project!
before_filter :authorize_code_access!
before_filter :require_non_empty_project

layout "project"

def show
@activities = @project.commits_with_refs(20)
end
Expand Down
7 changes: 1 addition & 6 deletions app/controllers/snippets_controller.rb
@@ -1,10 +1,5 @@
class SnippetsController < ApplicationController
before_filter :project
class SnippetsController < ProjectResourceController
before_filter :snippet, only: [:show, :edit, :destroy, :update, :raw]
layout "project"

# Authorize
before_filter :add_project_abilities

# Allow read any snippet
before_filter :authorize_read_snippet!
Expand Down
6 changes: 1 addition & 5 deletions app/controllers/team_members_controller.rb
@@ -1,9 +1,5 @@
class TeamMembersController < ApplicationController
before_filter :project
layout "project"

class TeamMembersController < ProjectResourceController
# Authorize
before_filter :add_project_abilities
before_filter :authorize_read_project!
before_filter :authorize_admin_project!, except: [:index, :show]

Expand Down
7 changes: 1 addition & 6 deletions app/controllers/tree_controller.rb
@@ -1,13 +1,8 @@
# Controller for viewing a repository's file structure
class TreeController < ApplicationController
class TreeController < ProjectResourceController
include ExtractsPath

layout "project"

before_filter :project

# Authorize
before_filter :add_project_abilities
before_filter :authorize_read_project!
before_filter :authorize_code_access!
before_filter :require_non_empty_project
Expand Down

0 comments on commit 68f4b59

Please sign in to comment.