Skip to content
This repository has been archived by the owner on Apr 26, 2022. It is now read-only.

Commit

Permalink
Merge pull request #94 from dragosmiron/filter-refactorings
Browse files Browse the repository at this point in the history
Refactored object.find in before_filters
  • Loading branch information
mezis committed May 20, 2013
2 parents 931c321 + 4c87d3c commit c905053
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
10 changes: 7 additions & 3 deletions app/controllers/accounts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
class AccountsController < ApplicationController
include Traits::RequiresLogin

before_filter :load_account, :only => [:edit, :update, :destroy]

def index
redirect_to root_path
end
Expand Down Expand Up @@ -34,12 +36,10 @@ def create
end

def edit
@account = Account.find(params[:id])
authorize! :update, @account
end

def update
@account = Account.find(params[:id])
authorize! :update, @account

categories = params[:account].andand.delete(:categories)
Expand All @@ -55,10 +55,14 @@ def update
end

def destroy
@account = Account.find(params[:id])
authorize! :destroy, @account
reset_login if @account == current_account
@account.destroy
redirect_to root_path, :notice => _("Successfully destroyed team %{name}.") % { name:@account.name }
end

private
def load_account
@account = Account.find(params[:id])
end
end
11 changes: 7 additions & 4 deletions app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
class CommentsController < ApplicationController
include Traits::RequiresLogin

before_filter :load_comment, :only => [:show, :update, :destroy]

def create
@comment = Comment.new(params[:comment])
@comment.author = current_user
Expand All @@ -28,8 +30,6 @@ def create


def show
@comment = Comment.find(params[:id])

if request.xhr?
case params['part']
when 'attachments'
Expand All @@ -46,7 +46,6 @@ def show


def update
@comment = Comment.find(params[:id])
if @comment.update_attributes(params[:comment])
flash[:success] = _("Successfully updated comment.")
else
Expand All @@ -57,7 +56,6 @@ def update


def destroy
@comment = Comment.find(params[:id])
@comment.destroy

respond_to do |format|
Expand All @@ -68,4 +66,9 @@ def destroy
format.js
end
end

private
def load_comment
@comment = Comment.find(params[:id])
end
end
11 changes: 7 additions & 4 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
class UsersController < ApplicationController
include Traits::RequiresLogin

before_filter :load_user, :only => [:show, :edit, :update, :destroy]

def index
unless current_account
render_error_page :not_found,
Expand All @@ -12,17 +14,14 @@ def index
end

def show
@user = User.find(params[:id])
authorize! :read, @user
end

def edit
@user = User.find(params[:id])
authorize! :update, @user
end

def update
@user = User.find(params[:id])
authorize! :update, @user

if @user.update_attributes(params[:user])
Expand All @@ -33,10 +32,14 @@ def update
end

def destroy
@user = User.find(params[:id])
authorize! :destroy, @user

@user.destroy
redirect_to users_url, :notice => "Successfully destroyed user."
end

private
def load_user
@user = User.find(params[:id])
end
end

0 comments on commit c905053

Please sign in to comment.