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 an `AddButtonComponent` for Calendars

Aaron Contreras5759f8fea85c
diff --git a/modules/calendar/app/components/calendar/add_button_component.html.erb b/modules/calendar/app/components/calendar/add_button_component.html.erb
new file mode 100644
index 00000000000..9ba9cfb898b
--- /dev/null
+++ b/modules/calendar/app/components/calendar/add_button_component.html.erb
@@ -0,0 +1,10 @@
+<li class="<%= li_css_class %>">
+ <a href="<%= dynamic_path %>"
+ id="<%= id %>"
+ title="<%= title %>"
+ arial-label="<%= aria_label %>"
+ class="<%= link_css_class %>">
+ <%= icon %>
+ <%= label %>
+ </a>
+</li>
diff --git a/modules/calendar/app/components/calendar/add_button_component.rb b/modules/calendar/app/components/calendar/add_button_component.rb
new file mode 100644
index 00000000000..c005925d4c9
--- /dev/null
+++ b/modules/calendar/app/components/calendar/add_button_component.rb
@@ -0,0 +1,82 @@
+# 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.
+# ++
+#
+
+module Calendar
+ class AddButtonComponent < ::ApplicationComponent
+ options :current_project
+
+ def render?
+ if current_project
+ User.current.allowed_to?(:manage_calendars, current_project)
+ else
+ User.current.allowed_to_globally?(:manage_calendars)
+ end
+ end
+
+ def li_css_class
+ 'toolbar-item'
+ end
+
+ def dynamic_path
+ if current_project
+ new_project_calendars_path(current_project)
+ else
+ new_calendar_path
+ end
+ end
+
+ def id
+ 'add-calendar-button'
+ end
+
+ def title
+ I18n.t('js.calendar.create_new')
+ end
+
+ def aria_label
+ I18n.t(:label_calendar)
+ end
+
+ def link_css_class
+ 'button -alt-highlight'
+ end
+
+ def label
+ content_tag(:span,
+ I18n.t(:label_calendar),
+ class: 'button--text')
+ end
+
+ def icon
+ helpers.op_icon('button--icon icon-add')
+ end
+ end
+end
diff --git a/modules/calendar/app/views/calendar/calendars/index.html.erb b/modules/calendar/app/views/calendar/calendars/index.html.erb
index 9d7acbaa005..422d81bf034 100644
--- a/modules/calendar/app/views/calendar/calendars/index.html.erb
+++ b/modules/calendar/app/views/calendar/calendars/index.html.erb
@@ -29,16 +29,7 @@ See COPYRIGHT and LICENSE files for more details.
<% html_title(t(:label_calendar_plural)) -%>
<%= toolbar title: t(:label_calendar_plural) do %>
- <% if current_user.allowed_to?(:manage_calendars, @project) %>
- <li class="toolbar-item">
- <%= link_to new_project_calendars_path(@project),
- class: 'button -alt-highlight',
- title: t('js.calendar.create_new') do %>
- <%= op_icon('button--icon icon-add') %>
- <span class="button--text"><%= t(:label_calendar) %></span>
- <% end %>
- </li>
- <% end %>
+ <%= render Calendar::AddButtonComponent.new(current_project: @project) %>
<% end %>
<%= render ::Calendar::TableComponent.new(rows: @views, current_project: @project, current_user: current_user) %>
diff --git a/modules/calendar/spec/features/calendars_index_spec.rb b/modules/calendar/spec/features/calendars_index_spec.rb
index d11e9fbfe4d..99cb6172894 100644
--- a/modules/calendar/spec/features/calendars_index_spec.rb
+++ b/modules/calendar/spec/features/calendars_index_spec.rb
@@ -115,8 +115,8 @@ RSpec.describe 'Calendars', 'index', :js, :with_cuprite do
end
context 'with permissions to globally manage calendars' do
- it 'shows no create button' do
- calendars_page.expect_no_create_button
+ it 'shows the create new button' do
+ calendars_page.expect_create_button
end
end