Class: Chats::MessagesController

Inherits:
ApplicationController show all
Defined in:
app/controllers/chats/messages_controller.rb

Overview

Sending, editing, soft-deleting, and re-rendering message bubbles.

Instance Method Summary collapse

Instance Method Details

#createObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/controllers/chats/messages_controller.rb', line 31

def create
  @message = @conversation.messages.new(message_params.merge(sender: chats_current_messager))

  if @message.save
    respond_to do |format|
      # The sender's OWN bubble appends instantly from this response (no
      # waiting for the Action Cable round-trip); the broadcast then
      # delivers the same element to everyone else — and to the sender
      # again, where Turbo's append dedup (same DOM id) makes it a no-op.
      format.turbo_stream
      format.html { redirect_to conversation_path(@conversation) }
    end
  else
    respond_to do |format|
      format.turbo_stream { render :errors, status: :unprocessable_entity }
      format.html do
        redirect_to conversation_path(@conversation),
                    alert: @message.errors.full_messages.to_sentence
      end
    end
  end
end

#destroyObject

Soft delete by default: tombstone the bubble (see Message#soft_delete!).



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/controllers/chats/messages_controller.rb', line 67

def destroy
  @message.soft_delete!

  respond_to do |format|
    format.turbo_stream do
      if @message.destroyed?
        render turbo_stream: turbo_stream.remove(@message)
      else
        render_bubble_replacement
      end
    end
    format.html { redirect_to conversation_path(@conversation) }
  end
end

#showObject

A single bubble, re-rendered. Exists for one delightful reason: it’s the “cancel edit” target — replacing the inline edit form back with the plain bubble via one turbo_stream GET, no full page reload.



27
28
29
# File 'app/controllers/chats/messages_controller.rb', line 27

def show
  render_bubble_replacement
end

#updateObject

Swap the bubble for an inline edit form (turbo_stream), with a plain page as the no-JS fallback. Edits arrive from the COMPOSER (the long-press → Editar flow re-targets the composer form at this URL with _method=patch), so failures render into the composer’s error slot — same surface as failed sends.



59
60
61
62
63
64
# File 'app/controllers/chats/messages_controller.rb', line 59

def update
  @message.edit!(message_params[:body])
  render_bubble_replacement
rescue ActiveRecord::RecordInvalid, Chats::NotAllowedError
  render :errors, status: :unprocessable_entity
end