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

Render project activity list with ViewComponent

Christophe Bliard7321c39d9e3f
diff --git a/Gemfile b/Gemfile
index dd5dc966b6a..d10b4db93f8 100644
--- a/Gemfile
+++ b/Gemfile
@@ -204,6 +204,8 @@ gem 'validate_url'
# Appsignal integration
gem "appsignal", "~> 3.0", require: false
+gem 'view_component'
+
group :test do
gem 'launchy', '~> 2.5.0'
gem 'rack-test', '~> 2.0.0'
diff --git a/Gemfile.lock b/Gemfile.lock
index 46747d6f608..ecfd8310b77 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -944,6 +944,10 @@ GEM
validate_url (1.0.15)
activemodel (>= 3.0.0)
public_suffix
+ view_component (2.82.0)
+ activesupport (>= 5.2.0, < 8.0)
+ concurrent-ruby (~> 1.0)
+ method_source (~> 1.0)
warden (1.2.9)
rack (>= 2.0.9)
warden-basic_auth (0.2.1)
@@ -1142,6 +1146,7 @@ DEPENDENCIES
typed_dag (~> 2.0.2)
tzinfo-data (~> 1.2022.1)
validate_url
+ view_component
warden (~> 1.2)
warden-basic_auth (~> 0.2.1)
webdrivers (~> 5.2.0)
diff --git a/app/components/activity_item_component.html.erb b/app/components/activity_item_component.html.erb
new file mode 100644
index 00000000000..fe153ce65d3
--- /dev/null
+++ b/app/components/activity_item_component.html.erb
@@ -0,0 +1,25 @@
+<li class="op-project-activity-list--item">
+ <div class="op-project-activity-list--item-title">
+ <%= link_to format_activity_title(@event.event_title), @event.event_path %>
+ <% if display_belonging_project? -%>
+ (Project: <%= content_tag('span', link_to(@event.project.name, @event.project), class: 'project') %>)
+ <% end -%>
+ </div>
+ <div class="op-project-activity-list--item-subtitle">
+ <%= @journal.initial? ? "created by" : "updated by" %>
+ <%= avatar(@event.event_author, size: 'mini') %> <span class="spot-caption spot-caption_bold"><%= link_to_user(@event.event_author) %></span>
+ on <%= format_time(@event.event_datetime.to_time, true) %>
+ </div>
+ <% if @event.event_description.present? -%>
+ <div class="op-project-activity-list--item-description">
+ <%= truncate_formatted_text(@event.event_description) %>
+ </div>
+ <% end -%>
+ <% if show_details? -%>
+ <ul class="op-project-activity-list--item-details">
+ <% rendered_details.each do |detail| -%>
+ <li class="op-project-activity-list--item-detail"><%= detail %></li>
+ <% end -%>
+ </ul>
+ <% end -%>
+</li>
diff --git a/app/components/activity_item_component.rb b/app/components/activity_item_component.rb
new file mode 100644
index 00000000000..704dbe47a32
--- /dev/null
+++ b/app/components/activity_item_component.rb
@@ -0,0 +1,60 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2022 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.
+#++
+
+class ActivityItemComponent < ViewComponent::Base
+ delegate :avatar,
+ :format_activity_title,
+ :format_time,
+ :link_to_user,
+ :truncate_formatted_text,
+ to: :helpers
+
+ def initialize(event:, journal:)
+ super()
+ @event = event
+ @journal = journal
+ end
+
+ def display_belonging_project?
+ @journal.journable_type != 'Project'
+ end
+
+ def show_details?
+ return false if @journal.initial?
+
+ @journal.details.present?
+ end
+
+ def rendered_details
+ @journal.details
+ .map { |detail| @journal.render_detail(detail) }
+ .compact
+ end
+end
diff --git a/app/components/activity_list_component.html.erb b/app/components/activity_list_component.html.erb
new file mode 100644
index 00000000000..54460e70275
--- /dev/null
+++ b/app/components/activity_list_component.html.erb
@@ -0,0 +1,6 @@
+<ul class="op-project-activity-list">
+ <% @events.each do |event| -%>
+ <% journal = @journals_by_id[event.event_id] -%>
+ <%= render(ActivityItemComponent.new(event:, journal:)) %>
+ <% end -%>
+</ul>
diff --git a/app/components/activity_list_component.rb b/app/components/activity_list_component.rb
new file mode 100644
index 00000000000..2fddd5508a1
--- /dev/null
+++ b/app/components/activity_list_component.rb
@@ -0,0 +1,37 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2022 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.
+#++
+
+class ActivityListComponent < ViewComponent::Base
+ def initialize(events:, journals_by_id:)
+ super()
+ @events = events.sort { |x, y| y.event_datetime <=> x.event_datetime }
+ @journals_by_id = journals_by_id
+ end
+end
diff --git a/app/helpers/activity_helper.rb b/app/helpers/activity_helper.rb
index c9a6820d41f..28ba29c7e59 100644
--- a/app/helpers/activity_helper.rb
+++ b/app/helpers/activity_helper.rb
@@ -10,10 +10,4 @@ module ActivityHelper
event.event_type
end
end
-
- def journal_rendered_details(journal)
- journal.details
- .map { |detail| journal.render_detail(detail) }
- .compact
- end
end
diff --git a/app/views/activities/index.html.erb b/app/views/activities/index.html.erb
index 6bcad413b5a..76633ec687f 100644
--- a/app/views/activities/index.html.erb
+++ b/app/views/activities/index.html.erb
@@ -34,42 +34,10 @@ See COPYRIGHT and LICENSE files for more details.
%>
<div class="op-project-activity-days">
- <% @events_by_day.keys.sort.reverse.each do |day| %>
+ <% @events_by_day.sort_by { |day, _events| day }.reverse_each do |day, events| %>
<h3 class="op-project-activity-days--header"><%= format_activity_day(day) %></h3>
- <ul class="op-project-activity-list">
- <% @events_by_day[day].sort { |x, y| y.event_datetime <=> x.event_datetime }.each do |e| -%>
- <% journal = @journals_by_id[e.event_id] %>
-
- <li class="op-project-activity-list--item">
- <div class="op-project-activity-list--item-title">
- <%= link_to format_activity_title(e.event_title), e.event_path %>
- <% if journal.journable_type != 'Project' -%>
- (Project: <%= content_tag('span', link_to(e.project.name, e.project), class: 'project') %>)
- <% end -%>
- </div>
- <div class="op-project-activity-list--item-subtitle">
- <%= journal.initial? ? "created by" : "updated by" %>
- <%= avatar(e.event_author, size: 'mini') %> <span class="spot-caption spot-caption_bold"><%= link_to_user(e.event_author) %></span>
- on <%= format_time(e.event_datetime.to_time, true) %>
- </div>
- <% if e.event_description.present? -%>
- <div class="op-project-activity-list--item-description">
- <%= truncate_formatted_text(e.event_description) %>
- </div>
- <% end -%>
- <% unless journal.initial? %>
- <% if journal.details.any? -%>
- <ul class="op-project-activity-list--item-details">
- <% journal_rendered_details(journal).each do |detail| -%>
- <li class="op-project-activity-list--item-detail"><%= detail %></li>
- <% end -%>
- </ul>
- <% end -%>
- <% end -%>
- </li>
- <% end -%>
- </ul>
+ <%= render(ActivityListComponent.new(events:, journals_by_id: @journals_by_id)) %>
<% end -%>
</div>
diff --git a/spec/components/activity_item_component_spec.rb b/spec/components/activity_item_component_spec.rb
new file mode 100644
index 00000000000..77747cfd31a
--- /dev/null
+++ b/spec/components/activity_item_component_spec.rb
@@ -0,0 +1,54 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2022 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 "rails_helper"
+
+RSpec.describe ActivityItemComponent, type: :component do
+ pending "add some examples to (or delete) #{__FILE__}"
+
+ let(:event) do
+ Activities::Event.new(
+ event_title: "Event Title",
+ event_description: "something",
+ event_datetime: journal.created_at,
+ project_id: project.id,
+ project:,
+ event_path: "/project/123"
+ )
+ end
+ let(:project) { build_stubbed(:project) }
+ let(:journal) { build_stubbed(:project_journal, journable: project) }
+
+ it "renders the title" do
+ render_inline(described_class.new(event:, journal:))
+
+ expect(page).to have_css('.op-project-activity-list--item-title', text: 'Event Title')
+ end
+end
diff --git a/spec/support/view_components.rb b/spec/support/view_components.rb
new file mode 100644
index 00000000000..31d2c69eadc
--- /dev/null
+++ b/spec/support/view_components.rb
@@ -0,0 +1,38 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2022 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 'view_component/test_helpers'
+require 'capybara/rspec'
+
+RSpec.configure do |config|
+ config.include ViewComponent::TestHelpers, type: :component
+ config.include ViewComponent::SystemTestHelpers, type: :component
+ config.include Capybara::RSpecMatchers, type: :component
+end