From d3eae22f1322a740fdddba553a9aa5ca69c68eaf Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Thu, 17 Nov 2022 21:55:01 +0100 Subject: [PATCH] Generate inbox entries using Turbo Streams --- app/controllers/inbox_controller.rb | 17 +++++++++++++++++ app/views/inbox/_actions.html.haml | 2 +- config/routes.rb | 1 + 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/app/controllers/inbox_controller.rb b/app/controllers/inbox_controller.rb index af510e2b..d91afa77 100644 --- a/app/controllers/inbox_controller.rb +++ b/app/controllers/inbox_controller.rb @@ -52,4 +52,21 @@ class InboxController < ApplicationController format.turbo_stream { render "show", layout: false, status: :see_other } end end + + def create + question = Question.create!(content: QuestionGenerator.generate, + author_is_anonymous: true, + author_identifier: "justask", + user: current_user) + + inbox = Inbox.create!(user: current_user, question_id: question.id, new: true) + + respond_to do |format| + format.turbo_stream do + render turbo_stream: turbo_stream.prepend("entries", partial: "inbox/entry", locals: { i: inbox }) + end + + format.html { redirect_to inbox_path } + end + end end diff --git a/app/views/inbox/_actions.html.haml b/app/views/inbox/_actions.html.haml index 34927346..bbb7f3c1 100644 --- a/app/views/inbox/_actions.html.haml +++ b/app/views/inbox/_actions.html.haml @@ -5,7 +5,7 @@ = f.text_field :author, value: params[:author], placeholder: t(".author.placeholder"), prepend: "@", hide_label: true, append: f.button(t(".author.button"), name: nil, class: "btn btn-light", id: "ib-author") .d-flex.ml-auto.mt-2.mt-md-0 - %button.btn.btn-info.mr-auto{ type: :button, id: "ib-generate-question" } + = button_to inbox_create_path, class: "btn btn-info mr-auto" do = t(".questions.button") .button-group.ml-1 %button.btn.btn-default{ title: t(".share.heading"), data: { toggle: :dropdown }, aria: { expanded: false } } diff --git a/config/routes.rb b/config/routes.rb index b42e0cdf..a733f6fe 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -143,6 +143,7 @@ Rails.application.routes.draw do match "/notifications(/:type)", to: "notifications#index", via: [:get, :post], as: :notifications, defaults: { type: "new" } + post "/inbox/create", to: "inbox#create", as: :inbox_create match "/inbox(/:author)", via: [:get, :post], to: "inbox#show", as: :inbox match "/user/:username(/p/:page)", to: "user#show", via: [:get, :post], defaults: { page: 1 }