diff --git a/app/models/query.rb b/app/models/query.rb index 4689f5fb..23742cfa 100644 --- a/app/models/query.rb +++ b/app/models/query.rb @@ -165,6 +165,10 @@ class Query < ActiveRecord::Base end @available_filters["assigned_to_id"] = { :type => :list_optional, :order => 4, :values => user_values } unless user_values.empty? @available_filters["author_id"] = { :type => :list, :order => 5, :values => user_values } unless user_values.empty? + + if User.current.logged? + @available_filters["watcher_id"] = { :type => :list, :order => 15, :values => [["<< #{l(:label_me)} >>", "me"]] } + end if project # project specific filters @@ -288,31 +292,34 @@ class Query < ActiveRecord::Base next if field == "subproject_id" v = values_for(field).clone next unless v and !v.empty? - + operator = operator_for(field) + + # "me" value subsitution + if %w(assigned_to_id author_id watcher_id).include?(field) + v.push(User.current.logged? ? User.current.id.to_s : "0") if v.delete("me") + end + sql = '' - is_custom_filter = false if field =~ /^cf_(\d+)$/ # custom field db_table = CustomValue.table_name db_field = 'value' is_custom_filter = true sql << "#{Issue.table_name}.id IN (SELECT #{Issue.table_name}.id FROM #{Issue.table_name} LEFT OUTER JOIN #{db_table} ON #{db_table}.customized_type='Issue' AND #{db_table}.customized_id=#{Issue.table_name}.id AND #{db_table}.custom_field_id=#{$1} WHERE " + sql << sql_for_field(field, operator, v, db_table, db_field, true) + ')' + elsif field == 'watcher_id' + db_table = Watcher.table_name + db_field = 'user_id' + sql << "#{Issue.table_name}.id #{ operator == '=' ? 'IN' : 'NOT IN' } (SELECT #{db_table}.watchable_id FROM #{db_table} WHERE #{db_table}.watchable_type='Issue' AND " + sql << sql_for_field(field, '=', v, db_table, db_field) + ')' else # regular field db_table = Issue.table_name db_field = field - sql << '(' + sql << '(' + sql_for_field(field, operator, v, db_table, db_field) + ')' end - - # "me" value subsitution - if %w(assigned_to_id author_id).include?(field) - v.push(User.current.logged? ? User.current.id.to_s : "0") if v.delete("me") - end - - sql = sql + sql_for_field(field, v, db_table, db_field, is_custom_filter) - - sql << ')' filters_clauses << sql + end if filters and valid? (filters_clauses << project_statement).join(' AND ') @@ -320,10 +327,10 @@ class Query < ActiveRecord::Base private - # Helper method to generate the WHERE sql for a +field+ with a +value+ - def sql_for_field(field, value, db_table, db_field, is_custom_filter) + # Helper method to generate the WHERE sql for a +field+, +operator+ and a +value+ + def sql_for_field(field, operator, value, db_table, db_field, is_custom_filter=false) sql = '' - case operator_for field + case operator when "=" sql = "#{db_table}.#{db_field} IN (" + value.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + ")" when "!" diff --git a/lang/bg.yml b/lang/bg.yml index 379ff4fc..0079c4ab 100644 --- a/lang/bg.yml +++ b/lang/bg.yml @@ -706,3 +706,4 @@ setting_repository_log_display_limit: Maximum number of revisions displayed on f field_identity_url: OpenID URL setting_openid: Allow OpenID login and registration label_login_with_open_id_option: or login with OpenID +field_watcher: Watcher diff --git a/lang/ca.yml b/lang/ca.yml index f4e5375e..770cc305 100644 --- a/lang/ca.yml +++ b/lang/ca.yml @@ -707,3 +707,4 @@ setting_repository_log_display_limit: Maximum number of revisions displayed on f field_identity_url: OpenID URL setting_openid: Allow OpenID login and registration label_login_with_open_id_option: or login with OpenID +field_watcher: Watcher diff --git a/lang/cs.yml b/lang/cs.yml index 947c9e46..bbfdfae4 100644 --- a/lang/cs.yml +++ b/lang/cs.yml @@ -711,3 +711,4 @@ setting_repository_log_display_limit: Maximum number of revisions displayed on f field_identity_url: OpenID URL setting_openid: Allow OpenID login and registration label_login_with_open_id_option: or login with OpenID +field_watcher: Watcher diff --git a/lang/da.yml b/lang/da.yml index 910a51b1..c07d6be8 100644 --- a/lang/da.yml +++ b/lang/da.yml @@ -708,3 +708,4 @@ setting_repository_log_display_limit: Maximum number of revisions displayed on f field_identity_url: OpenID URL setting_openid: Allow OpenID login and registration label_login_with_open_id_option: or login with OpenID +field_watcher: Watcher diff --git a/lang/de.yml b/lang/de.yml index fef1f796..366b7dd0 100644 --- a/lang/de.yml +++ b/lang/de.yml @@ -709,3 +709,4 @@ setting_repository_log_display_limit: Maximum number of revisions displayed on f field_identity_url: OpenID URL setting_openid: Allow OpenID login and registration label_login_with_open_id_option: or login with OpenID +field_watcher: Watcher diff --git a/lang/en.yml b/lang/en.yml index 1bb9fff8..f4f59df2 100644 --- a/lang/en.yml +++ b/lang/en.yml @@ -188,6 +188,7 @@ field_default_value: Default value field_comments_sorting: Display comments field_parent_title: Parent page field_editable: Editable +field_watcher: Watcher setting_app_title: Application title setting_app_subtitle: Application subtitle diff --git a/lang/es.yml b/lang/es.yml index 52eac46c..67f10341 100644 --- a/lang/es.yml +++ b/lang/es.yml @@ -691,3 +691,4 @@ setting_repository_log_display_limit: Maximum number of revisions displayed on f field_identity_url: OpenID URL setting_openid: Allow OpenID login and registration label_login_with_open_id_option: or login with OpenID +field_watcher: Watcher diff --git a/lang/fi.yml b/lang/fi.yml index 973ff7ee..65478776 100644 --- a/lang/fi.yml +++ b/lang/fi.yml @@ -706,3 +706,4 @@ setting_repository_log_display_limit: Maximum number of revisions displayed on f field_identity_url: OpenID URL setting_openid: Allow OpenID login and registration label_login_with_open_id_option: or login with OpenID +field_watcher: Watcher diff --git a/lang/fr.yml b/lang/fr.yml index 71f44de8..4a7c5170 100644 --- a/lang/fr.yml +++ b/lang/fr.yml @@ -188,6 +188,7 @@ field_comments_sorting: Afficher les commentaires field_parent_title: Page parent field_editable: Modifiable field_identity_url: URL OpenID +field_watcher: Observateur setting_app_title: Titre de l'application setting_app_subtitle: Sous-titre de l'application diff --git a/lang/gl.yml b/lang/gl.yml index 119e8606..701fe22c 100644 --- a/lang/gl.yml +++ b/lang/gl.yml @@ -691,3 +691,4 @@ setting_repository_log_display_limit: Maximum number of revisions displayed on f field_identity_url: OpenID URL setting_openid: Allow OpenID login and registration label_login_with_open_id_option: or login with OpenID +field_watcher: Watcher diff --git a/lang/he.yml b/lang/he.yml index dba80c69..7a1519ae 100644 --- a/lang/he.yml +++ b/lang/he.yml @@ -706,3 +706,4 @@ setting_repository_log_display_limit: Maximum number of revisions displayed on f field_identity_url: OpenID URL setting_openid: Allow OpenID login and registration label_login_with_open_id_option: or login with OpenID +field_watcher: Watcher diff --git a/lang/hu.yml b/lang/hu.yml index bdf110d7..697b26a1 100644 --- a/lang/hu.yml +++ b/lang/hu.yml @@ -707,3 +707,4 @@ setting_repository_log_display_limit: Maximum hány revízió jelenjen meg a fá field_identity_url: OpenID URL setting_openid: Allow OpenID login and registration label_login_with_open_id_option: or login with OpenID +field_watcher: Watcher diff --git a/lang/it.yml b/lang/it.yml index 07e41cbb..418f3529 100644 --- a/lang/it.yml +++ b/lang/it.yml @@ -706,3 +706,4 @@ setting_repository_log_display_limit: Maximum number of revisions displayed on f field_identity_url: OpenID URL setting_openid: Allow OpenID login and registration label_login_with_open_id_option: or login with OpenID +field_watcher: Watcher diff --git a/lang/ja.yml b/lang/ja.yml index b450380d..b9228767 100644 --- a/lang/ja.yml +++ b/lang/ja.yml @@ -707,3 +707,4 @@ setting_repository_log_display_limit: Maximum number of revisions displayed on f field_identity_url: OpenID URL setting_openid: Allow OpenID login and registration label_login_with_open_id_option: or login with OpenID +field_watcher: Watcher diff --git a/lang/ko.yml b/lang/ko.yml index fd8ba9f9..008fe866 100644 --- a/lang/ko.yml +++ b/lang/ko.yml @@ -706,3 +706,4 @@ setting_repository_log_display_limit: Maximum number of revisions displayed on f field_identity_url: OpenID URL setting_openid: Allow OpenID login and registration label_login_with_open_id_option: or login with OpenID +field_watcher: Watcher diff --git a/lang/lt.yml b/lang/lt.yml index 6bcd87b9..0a732a59 100644 --- a/lang/lt.yml +++ b/lang/lt.yml @@ -708,3 +708,4 @@ setting_repository_log_display_limit: Maximum number of revisions displayed on f field_identity_url: OpenID URL setting_openid: Allow OpenID login and registration label_login_with_open_id_option: or login with OpenID +field_watcher: Watcher diff --git a/lang/mk.yml b/lang/mk.yml index b2e7d4be..269d4ddd 100644 --- a/lang/mk.yml +++ b/lang/mk.yml @@ -708,3 +708,4 @@ text_custom_field_possible_values_info: 'One line for each value' field_identity_url: OpenID URL setting_openid: Allow OpenID login and registration label_login_with_open_id_option: or login with OpenID +field_watcher: Watcher diff --git a/lang/nl.yml b/lang/nl.yml index 122779fe..859972ee 100644 --- a/lang/nl.yml +++ b/lang/nl.yml @@ -691,3 +691,4 @@ setting_repository_log_display_limit: Maximum number of revisions displayed on f field_identity_url: OpenID URL setting_openid: Allow OpenID login and registration label_login_with_open_id_option: or login with OpenID +field_watcher: Watcher diff --git a/lang/no.yml b/lang/no.yml index e00e3650..10bf4842 100644 --- a/lang/no.yml +++ b/lang/no.yml @@ -707,3 +707,4 @@ setting_repository_log_display_limit: Maximum number of revisions displayed on f field_identity_url: OpenID URL setting_openid: Allow OpenID login and registration label_login_with_open_id_option: or login with OpenID +field_watcher: Watcher diff --git a/lang/pl.yml b/lang/pl.yml index f8d6a5ef..1ca3da32 100644 --- a/lang/pl.yml +++ b/lang/pl.yml @@ -725,3 +725,4 @@ setting_repository_log_display_limit: Maximum number of revisions displayed on f field_identity_url: OpenID URL setting_openid: Allow OpenID login and registration label_login_with_open_id_option: or login with OpenID +field_watcher: Watcher diff --git a/lang/pt-br.yml b/lang/pt-br.yml index eb18eb33..073c8e37 100644 --- a/lang/pt-br.yml +++ b/lang/pt-br.yml @@ -707,3 +707,4 @@ setting_repository_log_display_limit: Número máximo de revisões exibidas no a field_identity_url: OpenID URL setting_openid: Allow OpenID login and registration label_login_with_open_id_option: or login with OpenID +field_watcher: Watcher diff --git a/lang/pt.yml b/lang/pt.yml index cf76ccb1..7836f0fc 100644 --- a/lang/pt.yml +++ b/lang/pt.yml @@ -708,3 +708,4 @@ setting_repository_log_display_limit: Maximum number of revisions displayed on f field_identity_url: OpenID URL setting_openid: Allow OpenID login and registration label_login_with_open_id_option: or login with OpenID +field_watcher: Watcher diff --git a/lang/ro.yml b/lang/ro.yml index 4c6f3cba..ac06ccda 100644 --- a/lang/ro.yml +++ b/lang/ro.yml @@ -706,3 +706,4 @@ setting_repository_log_display_limit: Maximum number of revisions displayed on f field_identity_url: OpenID URL setting_openid: Allow OpenID login and registration label_login_with_open_id_option: or login with OpenID +field_watcher: Watcher diff --git a/lang/ru.yml b/lang/ru.yml index 1d067286..87be71e2 100644 --- a/lang/ru.yml +++ b/lang/ru.yml @@ -741,3 +741,4 @@ setting_repository_log_display_limit: Maximum number of revisions displayed on f field_identity_url: OpenID URL setting_openid: Allow OpenID login and registration label_login_with_open_id_option: or login with OpenID +field_watcher: Watcher diff --git a/lang/sk.yml b/lang/sk.yml index 4d41041e..351173a3 100644 --- a/lang/sk.yml +++ b/lang/sk.yml @@ -711,3 +711,4 @@ setting_repository_log_display_limit: Maximum number of revisions displayed on f field_identity_url: OpenID URL setting_openid: Allow OpenID login and registration label_login_with_open_id_option: or login with OpenID +field_watcher: Watcher diff --git a/lang/sl.yml b/lang/sl.yml index ea142ce4..bd51bda3 100644 --- a/lang/sl.yml +++ b/lang/sl.yml @@ -708,3 +708,4 @@ text_custom_field_possible_values_info: 'One line for each value' field_identity_url: OpenID URL setting_openid: Allow OpenID login and registration label_login_with_open_id_option: or login with OpenID +field_watcher: Watcher diff --git a/lang/sr.yml b/lang/sr.yml index 86ca019e..7455d20e 100644 --- a/lang/sr.yml +++ b/lang/sr.yml @@ -707,3 +707,4 @@ setting_repository_log_display_limit: Maximum number of revisions displayed on f field_identity_url: OpenID URL setting_openid: Allow OpenID login and registration label_login_with_open_id_option: or login with OpenID +field_watcher: Watcher diff --git a/lang/sv.yml b/lang/sv.yml index ad20e0d3..884dd16a 100644 --- a/lang/sv.yml +++ b/lang/sv.yml @@ -708,3 +708,4 @@ enumeration_activities: Aktiviteter (tidsuppföljning) field_identity_url: OpenID URL setting_openid: Allow OpenID login and registration label_login_with_open_id_option: or login with OpenID +field_watcher: Watcher diff --git a/lang/th.yml b/lang/th.yml index d1910c72..7cb03a1b 100644 --- a/lang/th.yml +++ b/lang/th.yml @@ -709,3 +709,4 @@ setting_repository_log_display_limit: Maximum number of revisions displayed on f field_identity_url: OpenID URL setting_openid: Allow OpenID login and registration label_login_with_open_id_option: or login with OpenID +field_watcher: Watcher diff --git a/lang/tr.yml b/lang/tr.yml index cc3d67fc..8766b112 100644 --- a/lang/tr.yml +++ b/lang/tr.yml @@ -707,3 +707,4 @@ setting_repository_log_display_limit: Maximum number of revisions displayed on f field_identity_url: OpenID URL setting_openid: Allow OpenID login and registration label_login_with_open_id_option: or login with OpenID +field_watcher: Watcher diff --git a/lang/uk.yml b/lang/uk.yml index 779c9438..235bbadb 100644 --- a/lang/uk.yml +++ b/lang/uk.yml @@ -708,3 +708,4 @@ text_custom_field_possible_values_info: 'One line for each value' field_identity_url: OpenID URL setting_openid: Allow OpenID login and registration label_login_with_open_id_option: or login with OpenID +field_watcher: Watcher diff --git a/lang/vn.yml b/lang/vn.yml index 75f294ed..e4f4fd47 100644 --- a/lang/vn.yml +++ b/lang/vn.yml @@ -709,3 +709,4 @@ setting_repository_log_display_limit: Maximum number of revisions displayed on f field_identity_url: OpenID URL setting_openid: Allow OpenID login and registration label_login_with_open_id_option: or login with OpenID +field_watcher: Watcher diff --git a/lang/zh-tw.yml b/lang/zh-tw.yml index 598023c0..9f3788a8 100644 --- a/lang/zh-tw.yml +++ b/lang/zh-tw.yml @@ -709,3 +709,4 @@ enumeration_activities: 活動 (時間追蹤) field_identity_url: OpenID URL setting_openid: Allow OpenID login and registration label_login_with_open_id_option: or login with OpenID +field_watcher: Watcher diff --git a/lang/zh.yml b/lang/zh.yml index cac4377b..4be80b3b 100644 --- a/lang/zh.yml +++ b/lang/zh.yml @@ -709,3 +709,4 @@ enumeration_activities: 活动(时间跟踪) field_identity_url: OpenID URL setting_openid: Allow OpenID login and registration label_login_with_open_id_option: or login with OpenID +field_watcher: Watcher diff --git a/test/fixtures/watchers.yml b/test/fixtures/watchers.yml index 6c8cdfb5..803b03e5 100644 --- a/test/fixtures/watchers.yml +++ b/test/fixtures/watchers.yml @@ -7,4 +7,8 @@ watchers_002: watchable_type: Message watchable_id: 1 user_id: 1 +watchers_003: + watchable_type: Issue + watchable_id: 2 + user_id: 1 \ No newline at end of file diff --git a/test/unit/query_test.rb b/test/unit/query_test.rb index 3bdc4a7a..d568604c 100644 --- a/test/unit/query_test.rb +++ b/test/unit/query_test.rb @@ -18,7 +18,7 @@ require File.dirname(__FILE__) + '/../test_helper' class QueryTest < Test::Unit::TestCase - fixtures :projects, :enabled_modules, :users, :members, :roles, :trackers, :issue_statuses, :issue_categories, :enumerations, :issues, :custom_fields, :custom_values, :versions, :queries + fixtures :projects, :enabled_modules, :users, :members, :roles, :trackers, :issue_statuses, :issue_categories, :enumerations, :issues, :watchers, :custom_fields, :custom_values, :versions, :queries def test_custom_fields_for_all_projects_should_be_available_in_global_queries query = Query.new(:project => nil, :name => '_') @@ -162,6 +162,26 @@ class QueryTest < Test::Unit::TestCase find_issues_with_query(query) end + def test_filter_watched_issues + User.current = User.find(1) + query = Query.new(:name => '_', :filters => { 'watcher_id' => {:operator => '=', :values => ['me']}}) + result = find_issues_with_query(query) + assert_not_nil result + assert !result.empty? + assert_equal Issue.visible.watched_by(User.current).sort_by(&:id), result.sort_by(&:id) + User.current = nil + end + + def test_filter_unwatched_issues + User.current = User.find(1) + query = Query.new(:name => '_', :filters => { 'watcher_id' => {:operator => '!', :values => ['me']}}) + result = find_issues_with_query(query) + assert_not_nil result + assert !result.empty? + assert_equal((Issue.visible - Issue.watched_by(User.current)).sort_by(&:id).size, result.sort_by(&:id).size) + User.current = nil + end + def test_default_columns q = Query.new assert !q.columns.empty?