Added a "Member of Role" to the issues filters. #5869

This filter will check an issue's assigned to field for users who have
(or don't have) a specific Role(s).

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@4078 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Eric Davis 2010-09-10 19:44:45 +00:00
parent 109b42f482
commit 41f8d043eb
4 changed files with 96 additions and 1 deletions

View File

@ -198,6 +198,9 @@ class Query < ActiveRecord::Base
group_values = Group.all.collect {|g| [g.name, g.id] }
@available_filters["member_of_group"] = { :type => :list_optional, :order => 6, :values => group_values } unless group_values.empty?
role_values = Role.givable.collect {|r| [r.name, r.id] }
@available_filters["assigned_to_role"] = { :type => :list_optional, :order => 7, :values => role_values } unless role_values.empty?
if User.current.logged?
@available_filters["watcher_id"] = { :type => :list, :order => 15, :values => [["<< #{l(:label_me)} >>", "me"]] }
@ -451,6 +454,26 @@ class Query < ActiveRecord::Base
sql << '(' + sql_for_field("assigned_to_id", operator, members_of_groups, Issue.table_name, "assigned_to_id", false) + ')'
elsif field == "assigned_to_role" # named field
if operator == "*" # Any Role
roles = Role.givable
operator = '=' # Override the operator since we want to find by assigned_to
elsif operator == "!*" # No role
roles = Role.givable
operator = '!' # Override the operator since we want to find by assigned_to
else
roles = Role.givable.find_all_by_id(v)
end
roles ||= []
members_of_roles = roles.inject([]) {|user_ids, role|
if role && role.members
user_ids << role.members.collect(&:user_id)
end
user_ids.flatten.uniq.compact
}.sort.collect(&:to_s)
sql << '(' + sql_for_field("assigned_to_id", operator, members_of_roles, Issue.table_name, "assigned_to_id", false) + ')'
else
# regular field
db_table = Issue.table_name

View File

@ -294,6 +294,7 @@ en:
field_sharing: Sharing
field_parent_issue: Parent task
field_member_of_group: Member of Group
field_assigned_to_role: Member of Role
setting_app_title: Application title
setting_app_subtitle: Application subtitle

View File

@ -13,6 +13,11 @@ module ObjectDaddyHelpers
User.spawn(attributes)
end
def User.add_to_project(user, project, roles)
roles = [roles] unless roles.is_a?(Array)
Member.generate!(:principal => user, :project => project, :roles => roles)
end
# Generate the default Query
def Query.generate_default!(attributes={})
query = Query.spawn(attributes)

View File

@ -404,7 +404,29 @@ class QueryTest < ActiveSupport::TestCase
end
end
context "'assigned_to_role' filter" do
should "be present" do
assert @query.available_filters.keys.include?("assigned_to_role")
end
should "be an optional list" do
assert_equal :list_optional, @query.available_filters["assigned_to_role"][:type]
end
should "have a list of the Roles as values" do
assert @query.available_filters["assigned_to_role"][:values].include?(['Manager',1])
assert @query.available_filters["assigned_to_role"][:values].include?(['Developer',2])
assert @query.available_filters["assigned_to_role"][:values].include?(['Reporter',3])
end
should "not include the built in Roles as values" do
assert ! @query.available_filters["assigned_to_role"][:values].include?(['Non member',4])
assert ! @query.available_filters["assigned_to_role"][:values].include?(['Anonymous',5])
end
end
end
context "#statement" do
@ -453,6 +475,50 @@ class QueryTest < ActiveSupport::TestCase
end
end
context "with 'assigned_to_role' filter" do
setup do
# No fixtures
MemberRole.delete_all
Member.delete_all
Role.delete_all
@manager_role = Role.generate!(:name => 'Manager')
@developer_role = Role.generate!(:name => 'Developer')
@project = Project.generate!
@manager = User.generate!
@developer = User.generate!
@boss = User.generate!
User.add_to_project(@manager, @project, @manager_role)
User.add_to_project(@developer, @project, @developer_role)
User.add_to_project(@boss, @project, [@manager_role, @developer_role])
end
should "search assigned to for users with the Role" do
@query = Query.new(:name => '_')
@query.add_filter('assigned_to_role', '=', [@manager_role.id.to_s])
assert_query_statement_includes @query, "#{Issue.table_name}.assigned_to_id IN ('#{@manager.id}','#{@boss.id}')"
assert_find_issues_with_query_is_successful @query
end
should "search assigned to for users not assigned to any Role (none)" do
@query = Query.new(:name => '_')
@query.add_filter('assigned_to_role', '!*', [''])
assert_query_statement_includes @query, "#{Issue.table_name}.assigned_to_id IS NULL OR #{Issue.table_name}.assigned_to_id NOT IN ('#{@manager.id}','#{@developer.id}','#{@boss.id}')"
assert_find_issues_with_query_is_successful @query
end
should "search assigned to for users assigned to any Role (all)" do
@query = Query.new(:name => '_')
@query.add_filter('assigned_to_role', '*', [''])
assert_query_statement_includes @query, "#{Issue.table_name}.assigned_to_id IN ('#{@manager.id}','#{@developer.id}','#{@boss.id}')"
assert_find_issues_with_query_is_successful @query
end
end
end
end