Adds named scopes for projects index.

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@8082 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Jean-Philippe Lang 2011-12-04 22:31:02 +00:00
parent f52410be19
commit ff0f141126
4 changed files with 25 additions and 9 deletions

View File

@ -26,14 +26,12 @@ class AdminController < ApplicationController
end end
def projects def projects
@status = params[:status] ? params[:status].to_i : 1 @status = params[:status] || 1
c = ARCondition.new(@status == 0 ? "status <> 0" : ["status = ?", @status])
unless params[:name].blank? scope = Project.status(@status)
name = "%#{params[:name].strip.downcase}%" scope = scope.like(params[:name]) if params[:name].present?
c << ["LOWER(identifier) LIKE ? OR LOWER(name) LIKE ?", name, name]
end @projects = scope.all(:order => 'lft')
@projects = Project.find :all, :order => 'lft',
:conditions => c.conditions
render :action => "projects", :layout => false if request.xhr? render :action => "projects", :layout => false if request.xhr?
end end

View File

@ -18,6 +18,6 @@
module AdminHelper module AdminHelper
def project_status_options_for_select(selected) def project_status_options_for_select(selected)
options_for_select([[l(:label_all), ''], options_for_select([[l(:label_all), ''],
[l(:status_active), 1]], selected) [l(:status_active), '1']], selected.to_s)
end end
end end

View File

@ -83,8 +83,17 @@ class Project < ActiveRecord::Base
named_scope :has_module, lambda { |mod| { :conditions => ["#{Project.table_name}.id IN (SELECT em.project_id FROM #{EnabledModule.table_name} em WHERE em.name=?)", mod.to_s] } } named_scope :has_module, lambda { |mod| { :conditions => ["#{Project.table_name}.id IN (SELECT em.project_id FROM #{EnabledModule.table_name} em WHERE em.name=?)", mod.to_s] } }
named_scope :active, { :conditions => "#{Project.table_name}.status = #{STATUS_ACTIVE}"} named_scope :active, { :conditions => "#{Project.table_name}.status = #{STATUS_ACTIVE}"}
named_scope :status, lambda {|arg| arg.blank? ? {} : {:conditions => {:status => arg.to_i}} }
named_scope :all_public, { :conditions => { :is_public => true } } named_scope :all_public, { :conditions => { :is_public => true } }
named_scope :visible, lambda {|*args| {:conditions => Project.visible_condition(args.shift || User.current, *args) }} named_scope :visible, lambda {|*args| {:conditions => Project.visible_condition(args.shift || User.current, *args) }}
named_scope :like, lambda {|arg|
if arg.blank?
{}
else
pattern = "%#{arg.to_s.strip.downcase}%"
{:conditions => ["LOWER(identifier) LIKE :p OR LOWER(name) LIKE :p", {:p => pattern}]}
end
}
def initialize(attributes = nil) def initialize(attributes = nil)
super super

View File

@ -54,6 +54,15 @@ class AdminControllerTest < ActionController::TestCase
assert_nil assigns(:projects).detect {|u| !u.active?} assert_nil assigns(:projects).detect {|u| !u.active?}
end end
def test_projects_with_status_filter
get :projects, :status => 1
assert_response :success
assert_template 'projects'
assert_not_nil assigns(:projects)
# active projects only
assert_nil assigns(:projects).detect {|u| !u.active?}
end
def test_projects_with_name_filter def test_projects_with_name_filter
get :projects, :name => 'store', :status => '' get :projects, :name => 'store', :status => ''
assert_response :success assert_response :success