From: Tom Hughes Date: Wed, 21 Mar 2012 22:22:33 +0000 (+0000) Subject: Factor out common code for looking up users X-Git-Tag: live~6192 X-Git-Url: https://git.openstreetmap.org./rails.git/commitdiff_plain/311f7ddd6e6516232d3884e9765b966190814882 Factor out common code for looking up users --- diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 25f4054a3..7aace7364 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -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) diff --git a/app/controllers/diary_entry_controller.rb b/app/controllers/diary_entry_controller.rb index 6666d6017..3e012f23c 100644 --- a/app/controllers/diary_entry_controller.rb +++ b/app/controllers/diary_entry_controller.rb @@ -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,18 +165,12 @@ 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 - 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 + @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 - 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 @@ -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 + @comment_pages, @comments = paginate(:diary_comments, + :conditions => { :user_id => @this_user }, + :order => 'created_at DESC', + :per_page => 20) + @page = (params[:page] || 1).to_i end private ## diff --git a/app/controllers/message_controller.rb b/app/controllers/message_controller.rb index 5db412524..79a0b861a 100644 --- a/app/controllers/message_controller.rb +++ b/app/controllers/message_controller.rb @@ -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,35 +13,30 @@ 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.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 + 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 - 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 = @subject = params[:title] - else - # The default /message/new/$user view - @title = t 'message.new.title' + @message = Message.new(params[:message]) + @message.to_user_id = @this_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 - 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 diff --git a/app/controllers/user_blocks_controller.rb b/app/controllers/user_blocks_controller.rb index e681a194a..103909e38 100644 --- a/app/controllers/user_blocks_controller.rb +++ b/app/controllers/user_blocks_controller.rb @@ -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 diff --git a/app/controllers/user_roles_controller.rb b/app/controllers/user_roles_controller.rb index f38ba82ac..7dad891e2 100644 --- a/app/controllers/user_roles_controller.rb +++ b/app/controllers/user_roles_controller.rb @@ -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.