From 88f295eee24fa2644774fa98b1dbe1d12eecd947 Mon Sep 17 00:00:00 2001 From: Kevin McConnell Date: Thu, 13 Feb 2025 10:53:17 +0000 Subject: [PATCH] Provide accessor for the draft comment --- app/models/bubble/messages.rb | 26 ++++++++++++++++ test/models/bubble/messages_test.rb | 46 +++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 test/models/bubble/messages_test.rb diff --git a/app/models/bubble/messages.rb b/app/models/bubble/messages.rb index 552f2f483..e30f3dc62 100644 --- a/app/models/bubble/messages.rb +++ b/app/models/bubble/messages.rb @@ -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 diff --git a/test/models/bubble/messages_test.rb b/test/models/bubble/messages_test.rb new file mode 100644 index 000000000..d507ebd69 --- /dev/null +++ b/test/models/bubble/messages_test.rb @@ -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