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

Add Global Create Form for Calendars

Aaron Contrerasf5e9d4110f4d
diff --git a/modules/calendar/app/contracts/calendar/views/global_create_contract.rb b/modules/calendar/app/contracts/calendar/views/global_create_contract.rb
new file mode 100644
index 00000000000..f3d93194c32
--- /dev/null
+++ b/modules/calendar/app/contracts/calendar/views/global_create_contract.rb
@@ -0,0 +1,43 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2023 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.
+#++
+
+module Calendar
+ module Views
+ class GlobalCreateContract < ::Queries::CreateContract
+ validate :validate_project_present
+
+ private
+
+ def validate_project_present
+ errors.add :project_id, :blank if model.project_id.blank?
+ end
+ end
+ end
+end
diff --git a/modules/calendar/app/controllers/calendar/calendars_controller.rb b/modules/calendar/app/controllers/calendar/calendars_controller.rb
index d1ace74b6ab..6bbe71d7e61 100644
--- a/modules/calendar/app/controllers/calendar/calendars_controller.rb
+++ b/modules/calendar/app/controllers/calendar/calendars_controller.rb
@@ -29,8 +29,9 @@
module ::Calendar
class CalendarsController < ApplicationController
before_action :find_optional_project
- before_action :authorize, except: %i[index]
- before_action :authorize_global, only: %i[index]
+ before_action :build_calendar_view, only: %i[new]
+ before_action :authorize, except: %i[index new create]
+ before_action :authorize_global, only: %i[index new create]
before_action :find_calendar, only: %i[destroy]
menu_item :calendar_view
@@ -50,7 +51,20 @@ module ::Calendar
def new; end
- def create; end
+ def create
+ service_result = create_service_class.new(user: User.current)
+ .call(calendar_view_params)
+
+ @view = service_result.result
+
+ if service_result.success?
+ flash[:notice] = I18n.t(:notice_successful_create)
+ redirect_to project_calendar_path(@project, @view.query)
+ else
+ @errors = service_result.errors
+ render action: :new
+ end
+ end
def destroy
if @view.destroy
@@ -64,13 +78,16 @@ module ::Calendar
private
- def find_optional_project
- return unless params[:project_id]
+ def build_calendar_view
+ @view = Query.new
+ end
- @project = Project.find(params[:project_id])
- authorize
- rescue ActiveRecord::RecordNotFound
- render_404
+ def create_service_class
+ Calendar::Views::GlobalCreateService
+ end
+
+ def calendar_view_params
+ params.require(:query).permit(:name, :public, :starred).merge(project_id: @project&.id)
end
def visible_views
diff --git a/modules/calendar/app/services/calendar/views/global_create_service.rb b/modules/calendar/app/services/calendar/views/global_create_service.rb
new file mode 100644
index 00000000000..ea3095eb3cb
--- /dev/null
+++ b/modules/calendar/app/services/calendar/views/global_create_service.rb
@@ -0,0 +1,58 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2023 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.
+#++
+
+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
+
+ 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)
+ end
+ end
+ end
+end
diff --git a/modules/calendar/app/services/calendar/views/set_attributes_service.rb b/modules/calendar/app/services/calendar/views/set_attributes_service.rb
new file mode 100644
index 00000000000..5c1adb7ebfc
--- /dev/null
+++ b/modules/calendar/app/services/calendar/views/set_attributes_service.rb
@@ -0,0 +1,36 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2023 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.
+#++
+
+module Calendar
+ module Views
+ class SetAttributesService < ::Queries::SetAttributesService
+ end
+ end
+end
diff --git a/modules/calendar/app/views/calendar/calendars/_form.html.erb b/modules/calendar/app/views/calendar/calendars/_form.html.erb
new file mode 100644
index 00000000000..18303baf315
--- /dev/null
+++ b/modules/calendar/app/views/calendar/calendars/_form.html.erb
@@ -0,0 +1,93 @@
+<%#-- copyright
+OpenProject is an open source project management software.
+Copyright (C) 2012-2023 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.
+
+++#%>
+
+<%= error_messages_for_contract @view, @errors %>
+
+<section class="form--section">
+
+ <div class="form--field -required">
+ <%= f.text_field :name,
+ label: t(:label_title),
+ required: true,
+ size: 60,
+ container_class: '-wide' %>
+ </div>
+
+ <div class="form--field -required">
+ <label class="form--label" for="project_id"><%= Query.human_attribute_name(:project) %>:</label>
+ <div class="form--field-container">
+ <%= angular_component_tag 'op-project-autocompleter',
+ inputs: {
+ apiFilters: [['user_action', '=', ['calendars/create']]],
+ name: 'project_id',
+ value: @project,
+ appendTo: 'body',
+ },
+ id: 'project_id',
+ class: 'form--select-container -wide remote-field--input',
+ data: {
+ 'qa-selector': 'project_id'
+ }
+ %>
+ </div>
+ <div class="form--field-instructions">
+ <p><%= t 'help_texts.views.project',
+ singular: t(:label_calendar).downcase,
+ plural: t(:label_calendar_plural)%></p>
+ </div>
+ </div>
+
+ <div class="form--field">
+ <%= styled_label_tag 'query[public]',
+ Query.human_attribute_name(:public) %>
+ <div class="form--field-container">
+ <%= styled_check_box_tag 'query[public]',
+ '1',
+ @view.public
+ %>
+ </div>
+ <div class="form--field-instructions">
+ <p><%= t 'help_texts.views.public' %></p>
+ </div>
+ </div>
+
+ <div class="form--field">
+ <%= styled_label_tag 'query[starred]',
+ t(:label_favoured) %>
+ <div class="form--field-container">
+ <%= styled_check_box_tag 'query[starred]',
+ '1',
+ @view.starred
+ %>
+ </div>
+ <div class="form--field-instructions">
+ <p><%= t 'help_texts.views.favoured' %></p>
+ </div>
+ </div>
+</section>
diff --git a/modules/calendar/app/views/calendar/calendars/new.html.erb b/modules/calendar/app/views/calendar/calendars/new.html.erb
new file mode 100644
index 00000000000..13ca5bd706a
--- /dev/null
+++ b/modules/calendar/app/views/calendar/calendars/new.html.erb
@@ -0,0 +1,37 @@
+<%#-- copyright
+OpenProject is an open source project management software.
+Copyright (C) 2012-2023 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.
+
+++#%>
+
+<% html_title t(:label_new_calendar) %>
+<%= toolbar title: t(:label_new_calendar) %>
+<%= labelled_tabular_form_for @view, url: { controller: '/calendar/calendars', action: 'create' }, :html => {:id => 'calendar-form'} do |f| -%>
+ <%= render :partial => 'form', :locals => {:f => f} %>
+ <%= styled_button_tag t(:button_create), class: '-highlight' %>
+ <%= link_to t(:button_cancel), { controller: 'calendar/calendars', action: 'index' },
+ class: 'button' %>
+<% end %>
diff --git a/modules/calendar/config/locales/en.yml b/modules/calendar/config/locales/en.yml
index 3035fdf5c06..2e9040e5e8e 100644
--- a/modules/calendar/config/locales/en.yml
+++ b/modules/calendar/config/locales/en.yml
@@ -1,6 +1,8 @@
# English strings go here
en:
+ label_calendar: "Calendar"
label_calendar_plural: "Calendars"
+ label_new_calendar: "New calendar"
permission_view_calendar: "View calendars"
permission_manage_calendars: "Manage calendars"
permission_share_calendars: "Subscribe to iCalendars"
diff --git a/modules/calendar/lib/open_project/calendar/engine.rb b/modules/calendar/lib/open_project/calendar/engine.rb
index 6c5864cec94..5d0533a7c52 100644
--- a/modules/calendar/lib/open_project/calendar/engine.rb
+++ b/modules/calendar/lib/open_project/calendar/engine.rb
@@ -33,7 +33,7 @@ module OpenProject::Calendar
dependencies: %i[view_work_packages],
contract_actions: { calendar: %i[read] }
permission :manage_calendars,
- { 'calendar/calendars': %i[index show new destroy] },
+ { 'calendar/calendars': %i[index show new create destroy] },
dependencies: %i[view_calendar add_work_packages edit_work_packages save_queries manage_public_queries],
contract_actions: { calendar: %i[create update destroy] }
permission :share_calendars,
diff --git a/modules/calendar/spec/features/calendars_global_create_spec.rb b/modules/calendar/spec/features/calendars_global_create_spec.rb
new file mode 100644
index 00000000000..8e362de83f7
--- /dev/null
+++ b/modules/calendar/spec/features/calendars_global_create_spec.rb
@@ -0,0 +1,135 @@
+# frozen_string_literal: true
+
+# -- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2023 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'
+require_relative '../support/pages/calendar'
+require_relative 'shared_context'
+
+RSpec.describe 'Calendar',
+ 'Creating a view from a Global Context',
+ :js,
+ :with_cuprite,
+ with_flag: { more_global_index_pages_active: true } do
+ include_context 'with calendar full access'
+
+ let(:calendars_page) { Pages::Calendar.new nil }
+
+ before do
+ login_as user
+ end
+
+ context 'within the global index page' do
+ before do
+ visit calendars_path
+ end
+
+ context 'when clicking on the create button' do
+ before do
+ calendars_page.click_on_create_button
+ end
+
+ it 'navigates to the global create form' do
+ expect(page).to have_current_path new_calendar_path
+ expect(page).to have_content I18n.t(:label_new_calendar)
+ end
+ end
+ end
+
+ context 'within the global create page' do
+ before do
+ visit new_calendar_path
+ end
+
+ context 'with all fields set' do
+ before do
+ wait_for_reload # Halt until the project autocompleter is ready
+
+ calendars_page.set_title("Batman's Itinerary")
+ calendars_page.set_project(project)
+ calendars_page.set_public
+ calendars_page.set_favoured
+ calendars_page.click_on_submit
+
+ wait_for_reload
+ end
+
+ it 'creates a view and redirects me to it' do
+ expect(page).to have_text(I18n.t(:notice_successful_create))
+ expect(page).to have_current_path(project_calendar_path(project, Query.last), ignore_query: true)
+ expect(page).to have_text("Batman's Itinerary")
+ end
+ end
+
+ context 'when missing a required field' do
+ describe 'title' do
+ before do
+ wait_for_reload # Halt until the project autocompleter is ready
+
+ calendars_page.set_project(project)
+ calendars_page.click_on_submit
+ end
+
+ it 'renders a required attribute validation error' do
+ expect(Query.all).to be_empty
+
+ # Required HTML attribute just warns
+ expect(page).to have_current_path(new_calendar_path)
+ end
+ end
+
+ describe 'project_id' do
+ before do
+ calendars_page.set_title("Batman's Itinerary")
+ calendars_page.click_on_submit
+
+ wait_for_reload
+ end
+
+ it 'renders a required attribute validation error' do
+ expect(Query.all).to be_empty
+
+ expect(page).to have_text("Project can't be blank.")
+ end
+ end
+ end
+
+ describe 'cancel button' do
+ context "when it's clicked" do
+ before do
+ calendars_page.click_on_cancel_button
+ end
+
+ it 'navigates back to the global index page' do
+ expect(page).to have_current_path(calendars_path)
+ end
+ end
+ end
+ end
+end
diff --git a/modules/calendar/spec/features/shared_context.rb b/modules/calendar/spec/features/shared_context.rb
index 7d720fbc3a3..0a361378a15 100644
--- a/modules/calendar/spec/features/shared_context.rb
+++ b/modules/calendar/spec/features/shared_context.rb
@@ -39,7 +39,8 @@ RSpec.shared_context 'with calendar full access' do
member_in_project: project,
member_with_permissions: %w[
view_work_packages edit_work_packages add_work_packages
- view_calendar
+ manage_calendars view_calendar
+ manage_public_queries
])
end
diff --git a/modules/calendar/spec/support/pages/calendar.rb b/modules/calendar/spec/support/pages/calendar.rb
index 18658dd1e6e..ddf5dd75e55 100644
--- a/modules/calendar/spec/support/pages/calendar.rb
+++ b/modules/calendar/spec/support/pages/calendar.rb
@@ -30,6 +30,8 @@ require 'support/pages/page'
module Pages
class Calendar < ::Pages::Page
+ include ::Components::Autocompleter::NgSelectAutocompleteHelpers
+
attr_reader :project,
:filters,
:query
@@ -112,6 +114,39 @@ module Pages
expect(page).to have_selector('.fc-event:not(.fc-event-draggable)', text: work_package.subject)
end
+ def set_title(title)
+ fill_in 'Title', with: title
+ end
+
+ def set_project(project)
+ select_autocomplete(find('[data-qa-selector="project_id"]'),
+ query: project,
+ results_selector: 'body',
+ wait_for_fetched_options: false)
+ end
+
+ def set_public
+ check 'Public'
+ end
+
+ def set_favoured
+ check 'Favoured'
+ end
+
+ def click_on_submit
+ click_on 'Create'
+ end
+
+ def click_on_create_button
+ within '.toolbar-items' do
+ click_link 'Calendar'
+ end
+ end
+
+ def click_on_cancel_button
+ click_on 'Cancel'
+ end
+
def expect_create_button
expect(page).to have_selector '.button', text: 'Calendar'
end