From e7b6a56a972eaed14386d737929f84a7ef59e9a5 Mon Sep 17 00:00:00 2001
From: Jean-Philippe Lang <jp_lang@yahoo.fr>
Date: Sun, 30 Nov 2008 16:57:56 +0000
Subject: [PATCH] Replaces User.find_active with a named scope.

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@2079 e93f8b46-1217-0410-a6f0-8f06a7374b81
---
 app/controllers/account_controller.rb      |  2 +-
 app/controllers/application.rb             |  2 +-
 app/controllers/projects_controller.rb     |  2 +-
 app/models/mail_handler.rb                 |  4 ++--
 app/models/mailer.rb                       |  2 +-
 app/models/user.rb                         | 15 +++------------
 app/views/projects/settings/_members.rhtml |  2 +-
 7 files changed, 10 insertions(+), 19 deletions(-)

diff --git a/app/controllers/account_controller.rb b/app/controllers/account_controller.rb
index f327dd5b6..fe44740a4 100644
--- a/app/controllers/account_controller.rb
+++ b/app/controllers/account_controller.rb
@@ -24,7 +24,7 @@ class AccountController < ApplicationController
 
   # Show user's account
   def show
-    @user = User.find_active(params[:id])
+    @user = User.active.find(params[:id])
     @custom_values = @user.custom_values
     
     # show only public projects and private projects that the logged in user is also a member of
diff --git a/app/controllers/application.rb b/app/controllers/application.rb
index e5719a059..36123ba47 100644
--- a/app/controllers/application.rb
+++ b/app/controllers/application.rb
@@ -46,7 +46,7 @@ class ApplicationController < ActionController::Base
   def find_current_user
     if session[:user_id]
       # existing session
-      (User.find_active(session[:user_id]) rescue nil)
+      (User.active.find(session[:user_id]) rescue nil)
     elsif cookies[:autologin] && Setting.autologin?
       # auto-login feature
       User.find_by_autologin_key(cookies[:autologin])
diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb
index 43e1ae035..9b3316682 100644
--- a/app/controllers/projects_controller.rb
+++ b/app/controllers/projects_controller.rb
@@ -227,7 +227,7 @@ class ProjectsController < ApplicationController
     @date_to ||= Date.today + 1
     @date_from = @date_to - @days
     @with_subprojects = params[:with_subprojects].nil? ? Setting.display_subprojects_issues? : (params[:with_subprojects] == '1')
-    @author = (params[:user_id] ? User.find_active(params[:user_id]) : nil)
+    @author = (params[:user_id] ? User.active.find(params[:user_id]) : nil)
     
     @activity = Redmine::Activity::Fetcher.new(User.current, :project => @project, 
                                                              :with_subprojects => @with_subprojects,
diff --git a/app/models/mail_handler.rb b/app/models/mail_handler.rb
index fcb469c60..a716412fc 100644
--- a/app/models/mail_handler.rb
+++ b/app/models/mail_handler.rb
@@ -39,7 +39,7 @@ class MailHandler < ActionMailer::Base
   # Processes incoming emails
   def receive(email)
     @email = email
-    @user = User.find_active(:first, :conditions => ["LOWER(mail) = ?", email.from.first.to_s.strip.downcase])
+    @user = User.active.find(:first, :conditions => ["LOWER(mail) = ?", email.from.first.to_s.strip.downcase])
     unless @user
       # Unknown user => the email is ignored
       # TODO: ability to create the user's account
@@ -149,7 +149,7 @@ class MailHandler < ActionMailer::Base
     if user.allowed_to?("add_#{obj.class.name.underscore}_watchers".to_sym, obj.project)
       addresses = [email.to, email.cc].flatten.compact.uniq.collect {|a| a.strip.downcase}
       unless addresses.empty?
-        watchers = User.find_active(:all, :conditions => ['LOWER(mail) IN (?)', addresses])
+        watchers = User.active.find(:all, :conditions => ['LOWER(mail) IN (?)', addresses])
         watchers.each {|w| obj.add_watcher(w)}
       end
     end
diff --git a/app/models/mailer.rb b/app/models/mailer.rb
index 397807d16..f17ebbdbb 100644
--- a/app/models/mailer.rb
+++ b/app/models/mailer.rb
@@ -116,7 +116,7 @@ class Mailer < ActionMailer::Base
 
   def account_activation_request(user)
     # Send the email to all active administrators
-    recipients User.find_active(:all, :conditions => {:admin => true}).collect { |u| u.mail }.compact
+    recipients User.active.find(:all, :conditions => {:admin => true}).collect { |u| u.mail }.compact
     subject l(:mail_subject_account_activation_request, Setting.app_title)
     body :user => user,
          :url => url_for(:controller => 'users', :action => 'index', :status => User::STATUS_REGISTERED, :sort_key => 'created_on', :sort_order => 'desc')
diff --git a/app/models/user.rb b/app/models/user.rb
index 72c550b82..3722081d2 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -42,6 +42,9 @@ class User < ActiveRecord::Base
   has_one :rss_token, :dependent => :destroy, :class_name => 'Token', :conditions => "action='feeds'"
   belongs_to :auth_source
   
+  # Active non-anonymous users scope
+  named_scope :active, :conditions => "#{User.table_name}.status = #{STATUS_ACTIVE}"
+  
   acts_as_customizable
   
   attr_accessor :password, :password_confirmation
@@ -76,18 +79,6 @@ class User < ActiveRecord::Base
     @name = nil
     super
   end
-
-  def self.active
-    with_scope :find => { :conditions => [ "status = ?", STATUS_ACTIVE ] } do 
-      yield 
-    end 
-  end
-  
-  def self.find_active(*args)
-    active do
-      find(*args)
-    end
-  end
   
   # Returns the user that matches provided login and password, or nil
   def self.try_to_login(login, password)
diff --git a/app/views/projects/settings/_members.rhtml b/app/views/projects/settings/_members.rhtml
index 67c3fb505..20806fe2d 100644
--- a/app/views/projects/settings/_members.rhtml
+++ b/app/views/projects/settings/_members.rhtml
@@ -1,6 +1,6 @@
 <%= error_messages_for 'member' %>
 <% roles = Role.find_all_givable %>
-<% users = User.find_active(:all).sort - @project.users %>
+<% users = User.active.find(:all).sort - @project.users %>
 <% # members sorted by role position
    members = @project.members.find(:all, :include => [:role, :user]).sort %>
    
-- 
GitLab