Provide accessor for the draft comment
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user