Skip to content

Commit

Permalink
kinda turboey
Browse files Browse the repository at this point in the history
  • Loading branch information
nickthecook committed Mar 10, 2024
1 parent 903b124 commit 8b7a999
Show file tree
Hide file tree
Showing 30 changed files with 427 additions and 31 deletions.
4 changes: 3 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ gem "tzinfo-data", platforms: %i[ windows jruby ]
gem "bootsnap", require: false

# Use Active Storage variants [https://guides.rubyonrails.org/active_storage_overview.html#transforming-images]
# gem "image_processing", "~> 1.2"
gem "image_processing", "~> 1.2"

group :development, :test do
# See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
Expand All @@ -72,3 +72,5 @@ end
gem "devise", "~> 4.9"

gem "pry-rails", "~> 0.3.9"

gem "actiontext", "~> 7.1"
9 changes: 9 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,14 @@ GEM
warden (~> 1.2.3)
drb (2.2.1)
erubi (1.12.0)
ffi (1.16.3)
globalid (1.2.1)
activesupport (>= 6.1)
i18n (1.14.4)
concurrent-ruby (~> 1.0)
image_processing (1.12.2)
mini_magick (>= 4.9.5, < 5)
ruby-vips (>= 2.0.17, < 3)
importmap-rails (2.0.1)
actionpack (>= 6.0.0)
activesupport (>= 6.0.0)
Expand All @@ -135,6 +139,7 @@ GEM
marcel (1.0.4)
matrix (0.4.2)
method_source (1.0.0)
mini_magick (4.12.0)
mini_mime (1.1.5)
minitest (5.22.2)
msgpack (1.7.2)
Expand Down Expand Up @@ -220,6 +225,8 @@ GEM
actionpack (>= 5.2)
railties (>= 5.2)
rexml (3.2.6)
ruby-vips (2.2.1)
ffi (~> 1.12)
rubyzip (2.3.2)
selenium-webdriver (4.18.1)
base64 (~> 0.2)
Expand Down Expand Up @@ -287,10 +294,12 @@ PLATFORMS
x86_64-linux

DEPENDENCIES
actiontext (~> 7.1)
bootsnap
capybara
debug
devise (~> 4.9)
image_processing (~> 1.2)
importmap-rails
jbuilder
pry-rails (~> 0.3.9)
Expand Down
31 changes: 31 additions & 0 deletions app/assets/stylesheets/actiontext.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Provides a drop-in pointer for the default Trix stylesheet that will format the toolbar and
* the trix-editor content (whether displayed or under editing). Feel free to incorporate this
* inclusion directly in any other asset bundle and remove this file.
*
*= require trix
*/

/*
* We need to override trix.css’s image gallery styles to accommodate the
* <action-text-attachment> element we wrap around attachments. Otherwise,
* images in galleries will be squished by the max-width: 33%; rule.
*/
.trix-content .attachment-gallery > action-text-attachment,
.trix-content .attachment-gallery > .attachment {
flex: 1 0 33%;
padding: 0 0.5em;
max-width: 33%;
}

.trix-content .attachment-gallery.attachment-gallery--2 > action-text-attachment,
.trix-content .attachment-gallery.attachment-gallery--2 > .attachment, .trix-content .attachment-gallery.attachment-gallery--4 > action-text-attachment,
.trix-content .attachment-gallery.attachment-gallery--4 > .attachment {
flex-basis: 50%;
max-width: 50%;
}

.trix-content action-text-attachment .attachment {
padding: 0 !important;
max-width: 100% !important;
}
1 change: 1 addition & 0 deletions app/assets/stylesheets/application.tailwind.css
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@
}
*/
@import 'actiontext.css';
7 changes: 7 additions & 0 deletions app/controllers/conversations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ def index

# GET /conversations/1 or /conversations/1.json
def show
respond_to do |format|
format.turbo_stream { render turbo_stream: turbo_stream.replace("conversation", partial: "conversation") }
format.html do
@conversations = Conversation.all
render :index
end
end
end

# GET /conversations/new
Expand Down
29 changes: 29 additions & 0 deletions app/controllers/messages_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class MessagesController < ApplicationController
before_action :set_message
before_action :set_conversation

def create
@message = Message.new(message_params)
@message.user = current_user
@message.save!
end

def destroy
@message.destroy!
redirect_to conversation_path(@conversation)
end

private

def message_params
params.require(:message).permit(:content)
end

def set_message
@message = Message.find(params[:id])
end

def set_conversation
@conversation = Conversation.find(@message.conversation_id)
end
end
2 changes: 2 additions & 0 deletions app/helpers/messages_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module MessagesHelper
end
3 changes: 3 additions & 0 deletions app/javascript/application.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Configure your import map in config/importmap.rb. Read more: https://github.com/rails/importmap-rails
import "@hotwired/turbo-rails"
import "controllers"

import "trix"
import "@rails/actiontext"
2 changes: 1 addition & 1 deletion app/models/conversation.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class Conversation < ApplicationRecord
belongs_to :user
belongs_to :model_config
has_many :messages
has_many :messages, dependent: :destroy
end
3 changes: 3 additions & 0 deletions app/models/message.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
class Message < ApplicationRecord
belongs_to :user
belongs_to :conversation

# has_rich_text :content
end

14 changes: 14 additions & 0 deletions app/views/active_storage/blobs/_blob.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<figure class="attachment attachment--<%= blob.representable? ? "preview" : "file" %> attachment--<%= blob.filename.extension %>">
<% if blob.representable? %>
<%= image_tag blob.representation(resize_to_limit: local_assigns[:in_gallery] ? [ 800, 600 ] : [ 1024, 768 ]) %>
<% end %>

<figcaption class="attachment__caption">
<% if caption = blob.try(:caption) %>
<%= caption %>
<% else %>
<span class="attachment__name"><%= blob.filename %></span>
<span class="attachment__size"><%= number_to_human_size blob.byte_size %></span>
<% end %>
</figcaption>
</figure>
26 changes: 10 additions & 16 deletions app/views/conversations/_conversation.html.erb
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
<div id="<%= dom_id conversation %>">
<p class="my-5">
<strong class="block font-medium mb-1">User:</strong>
<%= conversation.user_id %>
</p>
<div id="<%=dom_id(@conversation) %>">
<div class="pb-2">
<div class="text-xl"><%= @conversation.title %></div>
<div class="text-m">Model: <%= @conversation.model_config.name %></div>
</div>

<p class="my-5">
<strong class="block font-medium mb-1">Title:</strong>
<%= conversation.model_config_id %>
</p>

<p class="my-5">
<strong class="block font-medium mb-1">Title:</strong>
<%= conversation.title %>
</p>
<div class="container flex flex-col flex-start border-2 border-gray-400 rounded-lg">
<%= render @conversation.messages %>
</div>

<% if action_name != "show" %>
<%= link_to "Show this conversation", conversation, class: "rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
<%= link_to "Edit this conversation", edit_conversation_path(conversation), class: "rounded-lg py-3 ml-2 px-5 bg-gray-100 inline-block font-medium" %>
<%= link_to "Show this conversation", @conversation, class: "rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
<%= link_to "Edit this conversation", edit_conversation_path(@conversation), class: "rounded-lg py-3 ml-2 px-5 bg-gray-100 inline-block font-medium" %>
<hr class="mt-6">
<% end %>
</div>
2 changes: 1 addition & 1 deletion app/views/conversations/_conversation_list.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div class="text-xl font-bold pb-5">Conversations</div>
<div class="text-xl font-bold pb-5"><%= link_to "Conan", root_path %></div>
<% conversations.each do |conversation| %>
<%= link_to "New conversation", new_conversation_path, class: "rounded-lg py-1 px-3 my-2 text-center bg-blue-600 text-white block font-medium" %>
<%= render "conversation_list_item", conversation: %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/conversations/_conversation_list_item.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<div class="container flex">
<%= link_to conversation.title, conversation %>
<%= link_to conversation.title, conversation, data: { turbo_stream: true } %>
</div>
10 changes: 4 additions & 6 deletions app/views/conversations/_no_conversation.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<div class="container flex justify-center align-center items-center w-full h-full">
<div class="container flex flex-col w-1/2 justify-center items-center">
<%= image_tag "conan_ready.svg" %>
<div class="text-4xl font-bold">Conan</div>
<div class="text-xl font-bold">the Librarian</div>
</div>
<div class="container flex flex-col w-1/2 justify-center items-center">
<%= image_tag "conan_ready.svg" %>
<div class="text-4xl font-bold">Conan</div>
<div class="text-xl font-bold">the Librarian</div>
</div>
13 changes: 11 additions & 2 deletions app/views/conversations/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,17 @@
<%= render "conversation_list", conversations: @conversations %>
</div>

<div class="flex-1 w-100 h-screen bg-gray-100 text-gray-700 overflow-y-scroll p-5">
<%= render "no_conversation" %>
<div id="conversation_area" class="flex-1 w-100 h-screen bg-gray-100 text-gray-700 overflow-y-scroll p-5">
<%= turbo_stream_from "conversations" %>
<div id="conversation" class="h-full overflow-y-scroll">
<% if @conversation %>
<%= render @conversation %>
<% else %>
<div class="container flex justify-center align-center h-full">
<%= render "no_conversation" %>
</div>
<% end %>
</div>
</div>
</div>

Expand Down
3 changes: 3 additions & 0 deletions app/views/layouts/action_text/contents/_content.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div class="trix-content">
<%= yield -%>
</div>
2 changes: 1 addition & 1 deletion app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@

<div class="app relative">
<%= yield %>
</main>
</div>
</body>
</html>
15 changes: 15 additions & 0 deletions app/views/messages/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<%= form_with url: messages_path, html: { id: "message_form", class: "mt-3" } do |f| %>
<div class="container flex">
<div class="flex-1">
<%= f.text_area :message,
class: "form-control w-100",
placeholder: "your message",
autofocus: true,
data: { controller: "submit", "submit-target": "input" }
%>
</div>
<div>
<%= f.submit "Send"%>
</div>
</div>
<% end %>
6 changes: 6 additions & 0 deletions app/views/messages/_message.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<div class="flex flex-col items-center justify-center px-20 py-5 bg-gray-100 rounded-md shadow-lg">
<%= content_tag :div, id: dom_id(message) do %>
<p><%= message.content %></p>
<p class="text-sm text-muted"><%= message.user.email %></p>
<% end %>
</div>
2 changes: 2 additions & 0 deletions config/importmap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@
pin "@hotwired/stimulus", to: "stimulus.min.js"
pin "@hotwired/stimulus-loading", to: "stimulus-loading.js"
pin_all_from "app/javascript/controllers", under: "controllers"
pin "trix"
pin "@rails/actiontext", to: "actiontext.esm.js"
5 changes: 4 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
Rails.application.routes.draw do
resources :conversations
resources :conversations do
resources :messages, only: [:create, :destroy]
end

devise_for :users
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# This migration comes from active_storage (originally 20170806125915)
class CreateActiveStorageTables < ActiveRecord::Migration[7.0]
def change
# Use Active Record's configured type for primary and foreign keys
primary_key_type, foreign_key_type = primary_and_foreign_key_types

create_table :active_storage_blobs, id: primary_key_type do |t|
t.string :key, null: false
t.string :filename, null: false
t.string :content_type
t.text :metadata
t.string :service_name, null: false
t.bigint :byte_size, null: false
t.string :checksum

if connection.supports_datetime_with_precision?
t.datetime :created_at, precision: 6, null: false
else
t.datetime :created_at, null: false
end

t.index [ :key ], unique: true
end

create_table :active_storage_attachments, id: primary_key_type do |t|
t.string :name, null: false
t.references :record, null: false, polymorphic: true, index: false, type: foreign_key_type
t.references :blob, null: false, type: foreign_key_type

if connection.supports_datetime_with_precision?
t.datetime :created_at, precision: 6, null: false
else
t.datetime :created_at, null: false
end

t.index [ :record_type, :record_id, :name, :blob_id ], name: :index_active_storage_attachments_uniqueness, unique: true
t.foreign_key :active_storage_blobs, column: :blob_id
end

create_table :active_storage_variant_records, id: primary_key_type do |t|
t.belongs_to :blob, null: false, index: false, type: foreign_key_type
t.string :variation_digest, null: false

t.index [ :blob_id, :variation_digest ], name: :index_active_storage_variant_records_uniqueness, unique: true
t.foreign_key :active_storage_blobs, column: :blob_id
end
end

private
def primary_and_foreign_key_types
config = Rails.configuration.generators
setting = config.options[config.orm][:primary_key_type]
primary_key_type = setting || :primary_key
foreign_key_type = setting || :bigint
[primary_key_type, foreign_key_type]
end
end
26 changes: 26 additions & 0 deletions db/migrate/20240310210312_create_action_text_tables.action_text.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# This migration comes from action_text (originally 20180528164100)
class CreateActionTextTables < ActiveRecord::Migration[6.0]
def change
# Use Active Record's configured type for primary and foreign keys
primary_key_type, foreign_key_type = primary_and_foreign_key_types

create_table :action_text_rich_texts, id: primary_key_type do |t|
t.string :name, null: false
t.text :body, size: :long
t.references :record, null: false, polymorphic: true, index: false, type: foreign_key_type

t.timestamps

t.index [ :record_type, :record_id, :name ], name: "index_action_text_rich_texts_uniqueness", unique: true
end
end

private
def primary_and_foreign_key_types
config = Rails.configuration.generators
setting = config.options[config.orm][:primary_key_type]
primary_key_type = setting || :primary_key
foreign_key_type = setting || :bigint
[primary_key_type, foreign_key_type]
end
end
Loading

0 comments on commit 8b7a999

Please sign in to comment.