Skip to content

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

Move `remind_at` building to set attributes service

Kabiru Mwenja5cca15cde8a4
diff --git a/app/controllers/work_packages/reminders_controller.rb b/app/controllers/work_packages/reminders_controller.rb
index 008aeba660a..5af0df3f16e 100644
--- a/app/controllers/work_packages/reminders_controller.rb
+++ b/app/controllers/work_packages/reminders_controller.rb
@@ -175,22 +175,8 @@ class WorkPackages::RemindersController < ApplicationController
end
def reminder_params
- params.require(:reminder)
- .permit(%i[remind_at_date remind_at_time note])
- .tap do |initial_params|
- date = initial_params.delete(:remind_at_date)
- time = initial_params.delete(:remind_at_time)
-
- initial_params[:remind_at] = build_remind_at_from_params(date, time)
- initial_params[:remindable] = @work_package
- initial_params[:creator] = User.current
- end
- end
-
- def build_remind_at_from_params(remind_at_date, remind_at_time)
- if remind_at_date.present? && remind_at_time.present?
- User.current.time_zone.parse("#{remind_at_date} #{remind_at_time}")
- end
+ params.expect(reminder: %i[remind_at_date remind_at_time note])
+ .merge(remindable: @work_package, creator: User.current)
end
def remind_at_date
diff --git a/app/services/reminders/set_attributes_service.rb b/app/services/reminders/set_attributes_service.rb
index 7c2d68d0074..453456e2b5d 100644
--- a/app/services/reminders/set_attributes_service.rb
+++ b/app/services/reminders/set_attributes_service.rb
@@ -1,3 +1,5 @@
+# frozen_string_literal: true
+
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) the OpenProject GmbH
@@ -28,5 +30,23 @@
module Reminders
class SetAttributesService < ::BaseServices::SetAttributes
+ private
+
+ def set_attributes(params)
+ remind_at_date = params.delete(:remind_at_date)
+ remind_at_time = params.delete(:remind_at_time)
+
+ if (remind_at_date && remind_at_time).present?
+ params[:remind_at] = build_remind_at_from_params(remind_at_date, remind_at_time)
+ end
+
+ super
+ end
+
+ def build_remind_at_from_params(remind_at_date, remind_at_time)
+ if remind_at_date.present? && remind_at_time.present?
+ User.current.time_zone.parse("#{remind_at_date} #{remind_at_time}")
+ end
+ end
end
end
diff --git a/spec/services/reminders/set_attributes_service_spec.rb b/spec/services/reminders/set_attributes_service_spec.rb
new file mode 100644
index 00000000000..4c418b82232
--- /dev/null
+++ b/spec/services/reminders/set_attributes_service_spec.rb
@@ -0,0 +1,84 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 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"
+
+RSpec.describe Reminders::SetAttributesService do
+ let(:user) { build_stubbed(:user) }
+ let(:model_instance) { Reminder.new }
+ let(:remindable) { build_stubbed(:work_package) }
+ let(:contract_class) { EmptyContract }
+
+ current_user { user }
+
+ subject(:service) do
+ described_class.new(user: user,
+ model: model_instance,
+ contract_class:)
+ end
+
+ it "sets the remind_at attribute from date and time params" do
+ params = {
+ remind_at_date: "2023-10-01",
+ remind_at_time: "12:00",
+ note: "Some notes",
+ remindable:,
+ creator: user
+ }
+
+ service.perform(params)
+
+ expect(model_instance).to have_attributes(
+ remind_at: current_user.time_zone.parse("2023-10-01 12:00"),
+ note: "Some notes",
+ remindable:,
+ creator: user
+ )
+ end
+
+ context "when remind_at_date or remind_at_time is not provided" do
+ it "does not set the remind_at attribute" do
+ aggregate_failures "one is nil" do
+ service.perform(remind_at_date: nil, remind_at_time: "12:00")
+ expect(model_instance.remind_at).to be_nil
+ end
+
+ aggregate_failures "both are nil" do
+ service.perform(remind_at_date: nil, remind_at_time: nil)
+ expect(model_instance.remind_at).to be_nil
+ end
+
+ aggregate_failures "none provided" do
+ service.perform({})
+ expect(model_instance.remind_at).to be_nil
+ end
+ end
+ end
+end