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
Use UserPreferences::SetAttributes in Users::SetAttributes
Oliver Günthere54ec53b13d6
diff --git a/app/contracts/user_preferences/base_contract.rb b/app/contracts/user_preferences/base_contract.rbindex ebc64433b9a..6cbbb0e8913 100644--- a/app/contracts/user_preferences/base_contract.rb+++ b/app/contracts/user_preferences/base_contract.rb@@ -49,7 +49,7 @@ module UserPreferencesif: -> { model.daily_reminders.present? }validate :no_duplicate_workdays,- if: -> { model.workdays.present? }+ if: -> { model.workdays.is_a?(Array) }validate :valid_pause_days,if: -> { model.pause_reminders.present? && model.pause_reminders[:enabled] }diff --git a/app/models/user_preference.rb b/app/models/user_preference.rbindex 335501d1ddf..ba052f349f3 100644--- a/app/models/user_preference.rb+++ b/app/models/user_preference.rb@@ -134,6 +134,10 @@ class UserPreference < ApplicationRecordsuper.presence || [1, 2, 3, 4, 5]end+ def supported_settings_method?(method_name)+ UserPreferences::Schema.properties.include?(method_name.to_s.gsub(/\?|=\z/, ''))+ end+privatedef to_boolean(value)@@ -143,8 +147,4 @@ class UserPreference < ApplicationRecorddef attribute?(name)%i[user user_id].include?(name.to_sym)end-- def supported_settings_method?(method_name)- UserPreferences::Schema.properties.include?(method_name.to_s.gsub(/\?|=\z/, ''))- endenddiff --git a/app/services/user_preferences/set_attributes_service.rb b/app/services/user_preferences/set_attributes_service.rbindex d65f9c73b79..2aaca2606e8 100644--- a/app/services/user_preferences/set_attributes_service.rb+++ b/app/services/user_preferences/set_attributes_service.rb@@ -31,9 +31,17 @@module UserPreferencesclass SetAttributesService < ::BaseServices::SetAttributesdef set_attributes(params)+ set_boolean_value(:hide_mail, params.delete(:hide_mail)) if params.key?(:hide_mail)+params.each do |k, v|- model[k] = v+ model[k] = v if model.supported_settings_method?(k)endend++ private++ def set_boolean_value(key, value)+ model[key] = ActiveRecord::Type::Boolean.new.cast(value)+ endendenddiff --git a/app/services/users/set_attributes_service.rb b/app/services/users/set_attributes_service.rbindex d207401981d..184d3c055c2 100644--- a/app/services/users/set_attributes_service.rb+++ b/app/services/users/set_attributes_service.rb@@ -34,12 +34,20 @@ module Usersprivate+ attr_accessor :pref+def set_attributes(params)- set_preferences params.delete(:pref)+ self.pref = params.delete(:pref)super(params)end+ def validate_and_result+ super.tap do |result|+ result.merge!(set_preferences) if pref.present?+ end+ end+def set_default_attributes(params)# Assign values other than mail to new_user when invitedif model.invited? && model.valid_attribute?(:mail)@@ -49,8 +57,10 @@ module Usersinitialize_notification_settings unless model.notification_settings.any?end- def set_preferences(user_preferences)- model.pref.attributes = user_preferences if user_preferences+ def set_preferences+ ::UserPreferences::SetAttributesService+ .new(user: user, model: model.pref, contract_class: ::UserPreferences::UpdateContract)+ .call(pref)enddef initialize_notification_settingsdiff --git a/spec/services/users/set_attributes_service_integration_spec.rb b/spec/services/users/set_attributes_service_integration_spec.rbnew file mode 100644index 00000000000..816959bf642--- /dev/null+++ b/spec/services/users/set_attributes_service_integration_spec.rb@@ -0,0 +1,73 @@+#-- 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 COPYRIGHT and LICENSE files for more details.+#++++require 'spec_helper'++describe ::Users::SetAttributesService, 'Integration', type: :model do+ let(:input_user) { FactoryBot.create(:user) }+ let(:actor) { FactoryBot.build_stubbed(:admin) }++ let(:instance) do+ described_class.new model: input_user,+ user: actor,+ contract_class: Users::UpdateContract+ end++ subject { instance.call(params) }++ context 'with a boolean castable preference' do+ let(:params) do+ { pref: { hide_mail: '0' } }+ end++ it 'returns an error for that' do+ expect(subject.errors).to be_empty+ end+ end++ context 'with an invalid parameter' do+ let(:params) do+ { pref: { workdays: 'foobar' } }+ end++ it 'returns an error for that' do+ expect(subject.errors[:workdays]).to include "is not of type 'array'"+ end+ end++ context 'with an unknown property' do+ let(:params) do+ { pref: { watwatwat: 'foobar' } }+ end++ it 'does not raise an error' do+ expect(subject).to be_success+ expect(subject.result.pref.settings).not_to be_key(:watwatwat)+ end+ end+end