Refactored duplicate logic to a before_filter. #1914

This commit is contained in:
Eric Davis
2009-01-19 11:17:13 -08:00
parent 86c1990faa
commit 2fe0bd3052

View File

@@ -1,25 +1,16 @@
class RatesController < ApplicationController
helper :users
before_filter :require_admin
before_filter :require_user_id, :only => [:index, :new]
# GET /rates?user_id=1
# GET /rates.xml?user_id=1
def index
begin
@user = User.find(params[:user_id])
@rates = Rate.history_for_user(@user)
@rates = Rate.history_for_user(@user)
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @rates }
end
rescue ActiveRecord::RecordNotFound
respond_to do |format|
flash[:error] = l(:rate_error_user_not_found)
format.html { redirect_to(home_url) }
format.xml { render :xml => "User not found", :status => :not_found }
end
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @rates }
end
end
@@ -37,20 +28,11 @@ class RatesController < ApplicationController
# GET /rates/new?user_id=1
# GET /rates/new.xml?user_id=1
def new
begin
@user = User.find(params[:user_id])
@rate = Rate.new(:user_id => @user.id)
@rate = Rate.new(:user_id => @user.id)
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @rate }
end
rescue ActiveRecord::RecordNotFound
respond_to do |format|
flash[:error] = l(:rate_error_user_not_found)
format.html { redirect_to(home_url) }
format.xml { render :xml => "User not found", :status => :not_found }
end
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @rate }
end
end
@@ -104,4 +86,18 @@ class RatesController < ApplicationController
format.xml { head :ok }
end
end
private
def require_user_id
begin
@user = User.find(params[:user_id])
rescue ActiveRecord::RecordNotFound
respond_to do |format|
flash[:error] = l(:rate_error_user_not_found)
format.html { redirect_to(home_url) }
format.xml { render :xml => "User not found", :status => :not_found }
end
end
end
end