Browse Source

Adds named scopes for projects index.

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@8082 e93f8b46-1217-0410-a6f0-8f06a7374b81
tags/1.4.0
Jean-Philippe Lang 12 years ago
parent
commit
ff0f141126

+ 6
- 8
app/controllers/admin_controller.rb View File

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

scope = Project.status(@status)
scope = scope.like(params[:name]) if params[:name].present?

@projects = scope.all(:order => 'lft')

render :action => "projects", :layout => false if request.xhr?
end

+ 1
- 1
app/helpers/admin_helper.rb View File

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

+ 9
- 0
app/models/project.rb 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 :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 :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)
super

+ 9
- 0
test/functional/admin_controller_test.rb View File

@@ -54,6 +54,15 @@ class AdminControllerTest < ActionController::TestCase
assert_nil assigns(:projects).detect {|u| !u.active?}
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
get :projects, :name => 'store', :status => ''
assert_response :success

Loading…
Cancel
Save