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

impl/65232 Adapt the existing comments model to be agnostic of News commentable (#19361)

Kabiru Mwenja762ee611834f
diff --git a/app/models/comment.rb b/app/models/comment.rb
index 76f44982481..d97116a03e8 100644
--- a/app/models/comment.rb
+++ b/app/models/comment.rb
@@ -1,3 +1,5 @@
+# frozen_string_literal: true
+
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) the OpenProject GmbH
@@ -32,7 +34,7 @@ class Comment < ApplicationRecord
validates :commented, :author, :comments, presence: true
- after_create :send_news_comment_added_mail
+ after_create :send_comment_added_event
def text
comments
@@ -44,6 +46,10 @@ class Comment < ApplicationRecord
private
+ def send_comment_added_event
+ send_news_comment_added_mail if commented_type == News.name
+ end
+
def send_news_comment_added_mail
OpenProject::Notifications.send(OpenProject::Events::NEWS_COMMENT_CREATED,
comment: self,
diff --git a/spec/models/comment_spec.rb b/spec/models/comment_spec.rb
index fb1a9b01570..b21b5390566 100644
--- a/spec/models/comment_spec.rb
+++ b/spec/models/comment_spec.rb
@@ -32,12 +32,46 @@ require "spec_helper"
RSpec.describe Comment do
shared_let(:user) { create(:user) }
shared_let(:news) { create(:news) }
- let(:comment) { described_class.new(author: user, comments: "some important words", commented: news) }
+
+ let(:commented) { news }
+
+ subject(:comment) { build(:comment, author: user, comments: "some important words", commented:) }
+
+ describe "associations" do
+ it { is_expected.to belong_to(:author).class_name("User") }
+ it { is_expected.to belong_to(:commented) }
+ end
+
+ describe "validations" do
+ it { is_expected.to validate_presence_of(:author) }
+ it { is_expected.to validate_presence_of(:commented) }
+ it { is_expected.to validate_presence_of(:comments) }
+ end
describe "#create" do
- it "creates the comment" do
- expect(described_class.create(commented: news, author: user, comments: "some important words"))
- .to be_truthy
+ before do
+ allow(OpenProject::Notifications).to receive(:send)
+ end
+
+ context "for news" do
+ it "creates the comment" do
+ expect { comment.save! }
+ .to change(described_class, :count).by(1).and change { commented.reload.comments_count }.by(1)
+
+ aggregate_failures "sends a news comment added event" do
+ expect(OpenProject::Notifications).to have_received(:send)
+ .with(OpenProject::Events::NEWS_COMMENT_CREATED,
+ comment: comment, send_notification: true)
+ end
+ end
+ end
+ end
+
+ describe "#destroy" do
+ before { comment.save! }
+
+ it "decrements the comments_count on the commented object" do
+ expect { comment.destroy! }.to change { commented.reload.comments_count }.by(-1)
end
end