Provide accessor for the draft comment

This commit is contained in:
Kevin McConnell
2025-02-13 10:53:17 +00:00
parent 946f387e7c
commit 88f295eee2
2 changed files with 72 additions and 0 deletions
+26
View File
@@ -3,9 +3,35 @@ module Bubble::Messages
included do
has_many :messages, -> { chronologically }, dependent: :destroy
after_save :capture_draft_comment
end
def capture(messageable)
messages.create! messageable: messageable
end
def draft_comment
find_or_build_initial_comment.body_plain_text
end
def draft_comment=(body)
if body.present?
@draft_comment = body
else
messages.comments.destroy_all
end
end
private
def find_or_build_initial_comment
message = messages.comments.first || messages.new(messageable: Comment.new)
message.comment
end
def capture_draft_comment
if @draft_comment.present?
find_or_build_initial_comment.update! body: @draft_comment, creator: creator
end
@draft_comment = nil
end
end
+46
View File
@@ -0,0 +1,46 @@
require "test_helper"
class Bubble::MessagesTest < ActiveSupport::TestCase
test "creating a bubble does not create a message by default" do
bubble = buckets(:writebook).bubbles.create! creator: users(:kevin), title: "New"
assert_empty bubble.messages
end
test "creating a bubble with an initial draft comment" do
bubble = buckets(:writebook).bubbles.create! creator: users(:kevin), title: "New",
draft_comment: "This is a comment"
assert_equal 1, bubble.messages.count
assert_equal "This is a comment", bubble.draft_comment.strip
end
test "updating the draft comment" do
bubble = buckets(:writebook).bubbles.create! creator: users(:kevin), title: "New",
draft_comment: "This is a comment"
bubble.update! draft_comment: "This is an updated comment"
assert_equal 1, bubble.messages.count
assert_equal "This is an updated comment", bubble.draft_comment.strip
end
test "setting the draft comment to be blank removes it" do
bubble = buckets(:writebook).bubbles.create! creator: users(:kevin), title: "New",
draft_comment: "This is a comment"
bubble.update! draft_comment: " "
assert bubble.messages.first.nil?
end
test "omitting the draft comment does not remove it" do
bubble = buckets(:writebook).bubbles.create! creator: users(:kevin), title: "New",
draft_comment: "This is a comment"
bubble.update! title: "Newer"
assert_equal 1, bubble.messages.count
assert_equal "This is a comment", bubble.draft_comment.strip
end
end