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

Extract base `GlobalCreateService` and `GlobalCreateContract`

Aaron Contreras38789c5f15fb
diff --git a/app/contracts/queries/global_create_contract.rb b/app/contracts/queries/global_create_contract.rb
new file mode 100644
index 00000000000..586679b9675
--- /dev/null
+++ b/app/contracts/queries/global_create_contract.rb
@@ -0,0 +1,15 @@
+# frozen_string_literal: true
+
+require 'queries/create_contract'
+
+module Queries
+ class GlobalCreateContract < CreateContract
+ validate :validate_project_present
+
+ private
+
+ def validate_project_present
+ errors.add :project_id, :blank if model.project_id.blank?
+ end
+ end
+end
diff --git a/modules/calendar/app/contracts/calendar/views/global_create_contract.rb b/app/services/views/global_create_service.rb
similarity index 68%
rename from modules/calendar/app/contracts/calendar/views/global_create_contract.rb
rename to app/services/views/global_create_service.rb
index f3d93194c32..59e6742e767 100644
--- a/modules/calendar/app/contracts/calendar/views/global_create_contract.rb
+++ b/app/services/views/global_create_service.rb
@@ -28,16 +28,33 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-module Calendar
- module Views
- class GlobalCreateContract < ::Queries::CreateContract
- validate :validate_project_present
+class Views::GlobalCreateService < BaseServices::Create
+ def initialize(user:,
+ contract_class: Queries::GlobalCreateContract,
+ contract_options: nil)
+ super
+ end
+
+ def after_perform(call)
+ create_view_from_query(call)
+ end
+
+ def instance_class
+ ::Query
+ end
- private
+ private
+
+ def create_view_from_query(call)
+ ::Views::CreateService.new(user: @user)
+ .call(view_params(call))
+ end
+
+ def view_params(call)
+ { query_id: call.result.id, type: view_type }
+ end
- def validate_project_present
- errors.add :project_id, :blank if model.project_id.blank?
- end
- end
+ def view_type
+ raise 'Implement me'
end
end
diff --git a/modules/calendar/app/services/calendar/views/global_create_service.rb b/modules/calendar/app/services/calendar/views/global_create_service.rb
index ea3095eb3cb..a4797292b8d 100644
--- a/modules/calendar/app/services/calendar/views/global_create_service.rb
+++ b/modules/calendar/app/services/calendar/views/global_create_service.rb
@@ -30,28 +30,11 @@
module Calendar
module Views
- class GlobalCreateService < ::Queries::CreateService
- def initialize(user:,
- contract_class: Calendar::Views::GlobalCreateContract,
- contract_options: nil)
- super
- end
-
- def after_perform(call)
- create_view_from_query(call)
- end
-
- def instance_class
- ::Query
- end
-
+ class GlobalCreateService < ::Views::GlobalCreateService
private
- def create_view_from_query(call)
- view_params = { query_id: call.result.id, type: 'work_packages_calendar' }
-
- ::Views::CreateService.new(user: @user)
- .call(view_params)
+ def view_type
+ 'work_packages_calendar'
end
end
end
diff --git a/modules/team_planner/app/contracts/team_planner/views/global_create_contract.rb b/modules/calendar/spec/services/global_create_service_spec.rb
similarity index 59%
rename from modules/team_planner/app/contracts/team_planner/views/global_create_contract.rb
rename to modules/calendar/spec/services/global_create_service_spec.rb
index 8e9c08ea20c..2c7e26f5567 100644
--- a/modules/team_planner/app/contracts/team_planner/views/global_create_contract.rb
+++ b/modules/calendar/spec/services/global_create_service_spec.rb
@@ -28,16 +28,39 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-module TeamPlanner
- module Views
- class GlobalCreateContract < ::Queries::CreateContract
- validate :validate_project_present
+require 'spec_helper'
- private
+RSpec.describe Calendar::Views::GlobalCreateService do
+ shared_let(:project) { create(:project) }
+ shared_let(:user) { build_stubbed(:admin) }
+ shared_let(:instance) { described_class.new(user:) }
- def validate_project_present
- errors.add :project_id, :blank if model.project_id.blank?
- end
+ subject { instance.call(params) }
+
+ context 'with all valid params' do
+ let(:params) do
+ {
+ name: "Batman's Itinerary",
+ project_id: project.id,
+ public: true,
+ starred: false
+ }
+ end
+
+ it 'is successful' do
+ expect(subject).to be_success
+ end
+
+ it 'creates a calendar view and its query' do
+ view = subject.result
+ query = view.query
+
+ expect(view.type).to eq 'work_packages_calendar'
+
+ expect(query.name).to eq "Batman's Itinerary"
+ expect(query.project).to eql(project)
+ expect(query).to be_public
+ expect(query).not_to be_starred
end
end
end
diff --git a/modules/team_planner/app/services/team_planner/views/global_create_service.rb b/modules/team_planner/app/services/team_planner/views/global_create_service.rb
index cba31a3fa28..84f9ccaff5c 100644
--- a/modules/team_planner/app/services/team_planner/views/global_create_service.rb
+++ b/modules/team_planner/app/services/team_planner/views/global_create_service.rb
@@ -30,28 +30,11 @@
module TeamPlanner
module Views
- class GlobalCreateService < ::Queries::CreateService
- def initialize(user:,
- contract_class: TeamPlanner::Views::GlobalCreateContract,
- contract_options: nil)
- super
- end
-
- def after_perform(call)
- create_view_from_query(call)
- end
-
- def instance_class
- ::Query
- end
-
+ class GlobalCreateService < ::Views::GlobalCreateService
private
- def create_view_from_query(call)
- view_params = { query_id: call.result.id, type: 'team_planner' }
-
- ::Views::CreateService.new(user: @user)
- .call(view_params)
+ def view_type
+ 'team_planner'
end
end
end
diff --git a/modules/team_planner/spec/contracts/views/team_planner_global_create_contract_spec.rb b/spec/contracts/queries/global_create_contract_spec.rb
similarity index 96%
rename from modules/team_planner/spec/contracts/views/team_planner_global_create_contract_spec.rb
rename to spec/contracts/queries/global_create_contract_spec.rb
index f366a9d5399..975b0b49d8b 100644
--- a/modules/team_planner/spec/contracts/views/team_planner_global_create_contract_spec.rb
+++ b/spec/contracts/queries/global_create_contract_spec.rb
@@ -32,7 +32,7 @@ require 'spec_helper'
require 'contracts/shared/model_contract_shared_context'
require 'contracts/queries/shared_contract_examples'
-RSpec.describe TeamPlanner::Views::GlobalCreateContract do
+RSpec.describe Queries::GlobalCreateContract do
include_context 'ModelContract shared context'
include_context 'with queries contract'