From d9136d59816464906cf3f0834162d024975d2230 Mon Sep 17 00:00:00 2001 From: Marius BALTEANU Date: Sun, 20 Oct 2019 17:05:55 +0300 Subject: [PATCH] Option to filter after my bookmarked projects --- app/models/query.rb | 8 +++++-- app/models/user.rb | 7 ++++++ config/locales/en.yml | 1 + test/fixtures/user_preferences.yml | 35 +++++++++++++++--------------- test/unit/query_test.rb | 21 ++++++++++++++++++ test/unit/user_test.rb | 7 ++++++ 6 files changed, 60 insertions(+), 19 deletions(-) diff --git a/app/models/query.rb b/app/models/query.rb index 19db899d8..82418edea 100644 --- a/app/models/query.rb +++ b/app/models/query.rb @@ -528,8 +528,9 @@ class Query < ActiveRecord::Base def project_values project_values = [] - if User.current.logged? && User.current.memberships.any? - project_values << ["<< #{l(:label_my_projects).downcase} >>", "mine"] + if User.current.logged? + project_values << ["<< #{l(:label_my_projects).downcase} >>", "mine"] if User.current.memberships.any? + project_values << ["<< #{l(:label_my_bookmarks).downcase} >>", "bookmarks"] if User.current.bookmarked_project_ids.any? end project_values += all_projects_values project_values @@ -915,6 +916,9 @@ class Query < ActiveRecord::Base if v.delete('mine') v += User.current.memberships.map(&:project_id).map(&:to_s) end + if v.delete('bookmarks') + v += User.current.bookmarked_project_ids + end end if field =~ /^cf_(\d+)\.cf_(\d+)$/ diff --git a/app/models/user.rb b/app/models/user.rb index 84832de39..e4291580e 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -840,6 +840,13 @@ class User < Principal end end + def bookmarked_project_ids + project_ids = [] + bookmarked_project_ids = self.pref[:bookmarked_project_ids] + project_ids = bookmarked_project_ids.split(',') unless bookmarked_project_ids.nil? + project_ids.map(&:to_i) + end + protected def validate_password_length diff --git a/config/locales/en.yml b/config/locales/en.yml index 6ab780552..ba4b4b5ee 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1076,6 +1076,7 @@ en: label_display_type: Display results as label_display_type_list: List label_display_type_board: Board + label_my_bookmarks: My bookmarks button_login: Login button_submit: Submit diff --git a/test/fixtures/user_preferences.yml b/test/fixtures/user_preferences.yml index 14243e0bb..0e739956a 100644 --- a/test/fixtures/user_preferences.yml +++ b/test/fixtures/user_preferences.yml @@ -1,41 +1,42 @@ ---- -user_preferences_001: +--- +user_preferences_001: others: | - --- + --- :no_self_notified: false - :my_page_layout: - left: + :my_page_layout: + left: - latestnews - documents - right: + right: - issuesassignedtome - top: + top: - calendar + :bookmarked_project_ids: "1,5" id: 1 user_id: 1 hide_mail: true -user_preferences_002: +user_preferences_002: others: | - --- + --- :no_self_notified: false - + id: 2 user_id: 2 hide_mail: true -user_preferences_003: +user_preferences_003: others: | - --- + --- :no_self_notified: false - :my_page_layout: - left: + :my_page_layout: + left: - latestnews - documents - right: + right: - issuesassignedtome - top: + top: - calendar - + id: 3 user_id: 3 hide_mail: false diff --git a/test/unit/query_test.rb b/test/unit/query_test.rb index db0d4c884..3524338eb 100644 --- a/test/unit/query_test.rb +++ b/test/unit/query_test.rb @@ -922,6 +922,27 @@ class QueryTest < ActiveSupport::TestCase assert_nil result.detect {|issue| !User.current.member_of?(issue.project)} end + def test_filter_my_bookmarks + User.current = User.find(1) + query = ProjectQuery.new(:name => '_') + filter = query.available_filters['id'] + assert_not_nil filter + assert_include 'bookmarks', filter[:values].map{|v| v[1]} + + query.filters = { 'id' => {:operator => '=', :values => ['bookmarks']}} + result = query.results_scope + + assert_equal [1,5], result.map(&:id).sort + end + + def test_filter_my_bookmarks_for_user_without_bookmarked_projects + User.current = User.find(2) + query = ProjectQuery.new(:name => '_') + filter = query.available_filters['id'] + + assert_not_include 'bookmarks', filter[:values].map{|v| v[1]} + end + def test_filter_watched_issues User.current = User.find(1) query = IssueQuery.new(:name => '_', :filters => { 'watcher_id' => {:operator => '=', :values => ['me']}}) diff --git a/test/unit/user_test.rb b/test/unit/user_test.rb index 20e34d99c..d44f35958 100644 --- a/test/unit/user_test.rb +++ b/test/unit/user_test.rb @@ -1280,6 +1280,13 @@ class UserTest < ActiveSupport::TestCase assert_equal user, User.try_to_login(user.login, "unsalted") end + def test_bookmarked_project_ids + # User with bookmarked projects + assert_equal [1, 5], User.find(1).bookmarked_project_ids + # User without bookmarked projects + assert_equal [], User.find(2).bookmarked_project_ids + end + if Object.const_defined?(:OpenID) def test_setting_identity_url normalized_open_id_url = 'http://example.com/' -- 2.22.0