OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
avoid UserPreference responding to every call
ulfertsf3478b1f2b59
diff --git a/.rubocop.yml b/.rubocop.ymlindex 5512adcdaba..41016453f22 100644--- a/.rubocop.yml+++ b/.rubocop.yml@@ -179,9 +179,14 @@ RSpec/LeadingSubject:RSpec/NamedSubject:Enabled: false-RSpec/FilePath:- Enabled: false-+RSpec/ContextWording:+ Prefixes:+ - when+ - with+ - without+ - if+ - unless+ - forStyle/Alias:Enabled: falsediff --git a/app/models/user_preference.rb b/app/models/user_preference.rbindex faeee96a44d..9b660b90b38 100644--- a/app/models/user_preference.rb+++ b/app/models/user_preference.rb@@ -56,8 +56,8 @@ class UserPreference < ApplicationRecord### We respond to all methods as we retrieve# the key from settings- def respond_to_missing?(*)- true+ def respond_to_missing?(method_name, include_private = false)+ UserPreferences::Schema.properties.include?(method_name.to_s.gsub(/\?|=\z/, '')) || superenddef [](attr_name)diff --git a/app/models/user_preferences/schema.rb b/app/models/user_preferences/schema.rbnew file mode 100644index 00000000000..d47ca35e98a--- /dev/null+++ b/app/models/user_preferences/schema.rb@@ -0,0 +1,43 @@+#-- encoding: UTF-8++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2021 the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See docs/COPYRIGHT.rdoc for more details.+#++++class UserPreferences::Schema+ class << self+ PATH = Rails.root.join('config/schemas/api/user_preferences.schema.json')++ def schema+ @schema ||= JSON::load(PATH)+ end++ def properties+ @properties ||= schema.dig('definitions', 'UserPreferences', 'properties')&.keys || []+ end+ end+enddiff --git a/spec/models/user_preference_spec.rb b/spec/models/user_preference_spec.rbindex dfb0de7c4a0..364377fe7dc 100644--- a/spec/models/user_preference_spec.rb+++ b/spec/models/user_preference_spec.rb@@ -29,7 +29,7 @@require 'spec_helper'describe UserPreference do- subject { FactoryBot.build(:user_preference, user: user) }+ subject(:preference) { FactoryBot.build(:user_preference, user: user) }let(:user) { FactoryBot.build_stubbed(:user) }@@ -73,6 +73,15 @@ describe UserPreference doendend+ describe '#respond_to?' do+ context 'for created_at (key not in the schema)' do+ it 'is does not respond' do+ expect(preference)+ .not_to respond_to(:created_at)+ end+ end+ end+describe 'sort order' doit_behaves_like 'accepts real and false booleans',:comments_in_reverse_order=,