Factor out common code for looking up users

This commit is contained in:
Tom Hughes 2012-03-21 22:22:33 +00:00
parent a9824dbc2e
commit 311f7ddd6e
5 changed files with 40 additions and 63 deletions

View file

@ -362,6 +362,14 @@ class ApplicationController < ActionController::Base
!@user.nil?
end
##
# ensure that there is a "this_user" instance variable
def lookup_this_user
unless @this_user = User.active.find_by_display_name(params[:display_name])
render_unknown_user params[:display_name]
end
end
##
# render a "no such user" page
def render_unknown_user(name)

View file

@ -4,6 +4,7 @@ class DiaryEntryController < ApplicationController
before_filter :authorize_web
before_filter :set_locale
before_filter :require_user, :only => [:new, :edit, :comment, :hide, :hidecomment]
before_filter :lookup_this_user, :only => [:view, :comments]
before_filter :check_database_readable
before_filter :check_database_writable, :only => [:new, :edit]
before_filter :require_administrator, :only => [:hide, :hidecomment]
@ -164,19 +165,13 @@ class DiaryEntryController < ApplicationController
end
def view
user = User.active.find_by_display_name(params[:display_name])
if user
@entry = user.diary_entries.visible.where(:id => params[:id]).first
@entry = @this_user.diary_entries.visible.where(:id => params[:id]).first
if @entry
@title = t 'diary_entry.view.title', :user => params[:display_name], :title => @entry.title
else
@title = t 'diary_entry.no_such_entry.title', :id => params[:id]
render :action => 'no_such_entry', :status => :not_found
end
else
render_unknown_user params[:display_name]
end
end
def hide
@ -192,17 +187,11 @@ class DiaryEntryController < ApplicationController
end
def comments
@this_user = User.active.find_by_display_name(params[:display_name])
if @this_user
@comment_pages, @comments = paginate(:diary_comments,
:conditions => { :user_id => @this_user },
:order => 'created_at DESC',
:per_page => 20)
@page = (params[:page] || 1).to_i
else
render_unknown_user params[:display_name]
end
end
private
##

View file

@ -4,6 +4,7 @@ class MessageController < ApplicationController
before_filter :authorize_web
before_filter :set_locale
before_filter :require_user
before_filter :lookup_this_user, :only => [:new]
before_filter :check_database_readable
before_filter :check_database_writable, :only => [:new, :reply, :mark]
@ -12,14 +13,12 @@ class MessageController < ApplicationController
# clicks send.
# The display_name param is the display name of the user that the message is being sent to.
def new
@to_user = User.find_by_display_name(params[:display_name])
if @to_user
if params[:message]
if @user.sent_messages.where("sent_on >= ?", Time.now.getutc - 1.hour).count >= MAX_MESSAGES_PER_HOUR
flash[:error] = t 'message.new.limit_exceeded'
else
@message = Message.new(params[:message])
@message.to_user_id = @to_user.id
@message.to_user_id = @this_user.id
@message.from_user_id = @user.id
@message.sent_on = Time.now.getutc
@ -39,9 +38,6 @@ class MessageController < ApplicationController
@title = t 'message.new.title'
end
end
else
render_unknown_user params[:display_name]
end
end
# Allow the user to reply to another message.

View file

@ -120,14 +120,6 @@ class UserBlocksController < ApplicationController
end
end
##
# ensure that there is a "this_user" instance variable
def lookup_this_user
unless @this_user = User.find_by_display_name(params[:display_name])
render_unknown_user params[:display_name]
end
end
##
# ensure that there is a "user_block" instance variable
def lookup_user_block

View file

@ -32,14 +32,6 @@ class UserRolesController < ApplicationController
end
end
##
# ensure that there is a "this_user" instance variable
def lookup_this_user
unless @this_user = User.find_by_display_name(params[:display_name])
render_unknown_user params[:display_name]
end
end
##
# require that the given role is valid. the role is a URL
# parameter, so should always be present.