From 02d2deeae6b44a1cb1941664ced005106f4c1d29 Mon Sep 17 00:00:00 2001 From: Holger Just Date: Wed, 20 Jun 2012 15:19:31 +0200 Subject: [PATCH] Add "me" to user custom fields filters #1046 Adapted from https://github.com/redmine/redmine/commit/28f9605fe2e242cc917108c277d8d6d6db3cdae1 by Jean-Philippe Lang --- app/models/query.rb | 14 ++++++++++---- test/unit/query_test.rb | 22 ++++++++++++++++++++++ 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/app/models/query.rb b/app/models/query.rb index 867ef25c..77ab8dba 100644 --- a/app/models/query.rb +++ b/app/models/query.rb @@ -383,8 +383,10 @@ class Query < ActiveRecord::Base next unless v and !v.empty? operator = operator_for(field) - # "me" value subsitution - if %w(assigned_to_id author_id watcher_id).include?(field) + # "me" value substitution + if %w(assigned_to_id author_id watcher_id).include?(field) || + # user custom fields + available_filters.has_key?(field) && available_filters[field][:format] == 'user' v.push(User.current.logged? ? User.current.id.to_s : "0") if v.delete("me") end @@ -640,11 +642,15 @@ class Query < ActiveRecord::Base options = { :type => :list, :values => [[l(:general_text_yes), "1"], [l(:general_text_no), "0"]], :order => 20 } when "user", "version" next unless project - options = { :type => :list_optional, :values => field.possible_values_options(project), :order => 20} + values = field.possible_values_options(project) + if User.current.logged? && field.field_format == 'user' + values.unshift ["<< #{l(:label_me)} >>", "me"] + end + options = { :type => :list_optional, :values => values, :order => 20} else options = { :type => :string, :order => 20 } end - @available_filters["cf_#{field.id}"] = options.merge({ :name => field.name }) + @available_filters["cf_#{field.id}"] = options.merge({ :name => field.name, :format => field.field_format }) end end diff --git a/test/unit/query_test.rb b/test/unit/query_test.rb index 398a2001..6b2acd41 100644 --- a/test/unit/query_test.rb +++ b/test/unit/query_test.rb @@ -222,6 +222,28 @@ class QueryTest < ActiveSupport::TestCase find_issues_with_query(query) end + def test_user_custom_field_filtered_on_me + User.current = User.find(2) + cf = IssueCustomField.create!(:field_format => 'user', :is_for_all => true, :is_filter => true, :name => 'User custom field', :tracker_ids => [1]) + + project = Project.find(1) + tracker = Tracker.find(1) + project.trackers << tracker unless project.trackers.include?(tracker) + + issue = Issue.create!(:project => project, :tracker => tracker, :subject => 'Test', :author_id => 1) + issue.update_attribute(:custom_field_values, {cf.id.to_s => '2'}) + + query = Query.new(:name => '_', :project => project) + filter = query.available_filters["cf_#{cf.id}"] + assert_not_nil filter + assert filter[:values].map{|v| v[1]}.include?('me') + + query.filters = { "cf_#{cf.id}" => {:operator => '=', :values => ['me']}} + result = query.issues + assert_equal 1, result.size + assert issue, result.first + end + def test_filter_watched_issues User.current = User.find(1) query = Query.new(:name => '_', :filters => { 'watcher_id' => {:operator => '=', :values => ['me']}})