diff --git a/app/controllers/bubbles_controller.rb b/app/controllers/bubbles_controller.rb index 226980d23..d8a642d3c 100644 --- a/app/controllers/bubbles_controller.rb +++ b/app/controllers/bubbles_controller.rb @@ -40,7 +40,7 @@ class BubblesController < ApplicationController end def bubble_params - params.expect(bubble: [ :status, :title, :color, :due_on, :image, tag_ids: [] ]) + params.expect(bubble: [ :status, :title, :color, :due_on, :image, :draft_comment, tag_ids: [] ]) end def deleted_notice diff --git a/app/javascript/controllers/remote_auto_save_controller.js b/app/javascript/controllers/remote_auto_save_controller.js new file mode 100644 index 000000000..7ba4a6d5e --- /dev/null +++ b/app/javascript/controllers/remote_auto_save_controller.js @@ -0,0 +1,13 @@ +import { Controller } from "@hotwired/stimulus" + +export default class extends Controller { + static outlets = [ "auto-save" ] + + change(event) { + this.autoSaveOutlet.change(event) + } + + submit() { + this.autoSaveOutlet.submit() + } +} 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/app/views/bubbles/_bubble.html.erb b/app/views/bubbles/_bubble.html.erb index db59e48f1..ea8833e50 100644 --- a/app/views/bubbles/_bubble.html.erb +++ b/app/views/bubbles/_bubble.html.erb @@ -8,14 +8,14 @@