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? !@user.nil?
end 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 # render a "no such user" page
def render_unknown_user(name) def render_unknown_user(name)

View file

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

View file

@ -4,6 +4,7 @@ class MessageController < ApplicationController
before_filter :authorize_web before_filter :authorize_web
before_filter :set_locale before_filter :set_locale
before_filter :require_user before_filter :require_user
before_filter :lookup_this_user, :only => [:new]
before_filter :check_database_readable before_filter :check_database_readable
before_filter :check_database_writable, :only => [:new, :reply, :mark] before_filter :check_database_writable, :only => [:new, :reply, :mark]
@ -12,35 +13,30 @@ class MessageController < ApplicationController
# clicks send. # clicks send.
# The display_name param is the display name of the user that the message is being sent to. # The display_name param is the display name of the user that the message is being sent to.
def new def new
@to_user = User.find_by_display_name(params[:display_name]) if params[:message]
if @to_user if @user.sent_messages.where("sent_on >= ?", Time.now.getutc - 1.hour).count >= MAX_MESSAGES_PER_HOUR
if params[:message] flash[:error] = t 'message.new.limit_exceeded'
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.from_user_id = @user.id
@message.sent_on = Time.now.getutc
if @message.save
flash[:notice] = t 'message.new.message_sent'
Notifier.message_notification(@message).deliver
redirect_to :controller => 'message', :action => 'inbox', :display_name => @user.display_name
end
end
else else
if params[:title] @message = Message.new(params[:message])
# ?title= is set when someone reponds to this user's diary @message.to_user_id = @this_user.id
# entry. Then we pre-fill out the subject and the <title> @message.from_user_id = @user.id
@title = @subject = params[:title] @message.sent_on = Time.now.getutc
else
# The default /message/new/$user view if @message.save
@title = t 'message.new.title' flash[:notice] = t 'message.new.message_sent'
Notifier.message_notification(@message).deliver
redirect_to :controller => 'message', :action => 'inbox', :display_name => @user.display_name
end end
end end
else else
render_unknown_user params[:display_name] if params[:title]
# ?title= is set when someone reponds to this user's diary
# entry. Then we pre-fill out the subject and the <title>
@title = @subject = params[:title]
else
# The default /message/new/$user view
@title = t 'message.new.title'
end
end end
end end

View file

@ -120,14 +120,6 @@ class UserBlocksController < ApplicationController
end end
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 # ensure that there is a "user_block" instance variable
def lookup_user_block def lookup_user_block

View file

@ -32,14 +32,6 @@ class UserRolesController < ApplicationController
end end
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 # require that the given role is valid. the role is a URL
# parameter, so should always be present. # parameter, so should always be present.