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

https://community.openproject.org/work_packages/65232
This commit is contained in:
Kabiru Mwenja
2025-07-07 13:02:47 +03:00
committed by GitHub
parent 98019caf7b
commit 762ee61183
2 changed files with 45 additions and 5 deletions
+7 -1
View File
@@ -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,
+38 -4
View File
@@ -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