Add Chat Rooms with Turbo
This commit is contained in:
parent
f366db0ee5
commit
7328ccf331
@ -352,6 +352,8 @@ GEM
|
|||||||
nokogiri (1.15.5)
|
nokogiri (1.15.5)
|
||||||
mini_portile2 (~> 2.8.2)
|
mini_portile2 (~> 2.8.2)
|
||||||
racc (~> 1.4)
|
racc (~> 1.4)
|
||||||
|
nokogiri (1.15.5-x86_64-linux)
|
||||||
|
racc (~> 1.4)
|
||||||
notiffany (0.1.3)
|
notiffany (0.1.3)
|
||||||
nenv (~> 0.1)
|
nenv (~> 0.1)
|
||||||
shellany (~> 0.0)
|
shellany (~> 0.0)
|
||||||
@ -566,6 +568,7 @@ GEM
|
|||||||
|
|
||||||
PLATFORMS
|
PLATFORMS
|
||||||
ruby
|
ruby
|
||||||
|
x86_64-linux
|
||||||
|
|
||||||
DEPENDENCIES
|
DEPENDENCIES
|
||||||
activejob-status (~> 1.0)
|
activejob-status (~> 1.0)
|
||||||
|
|||||||
3
app/assets/javascripts/rooms.coffee
Normal file
3
app/assets/javascripts/rooms.coffee
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
# Place all the behaviors and hooks related to the matching controller here.
|
||||||
|
# All this logic will automatically be available in application.js.
|
||||||
|
# You can use CoffeeScript in this file: http://coffeescript.org/
|
||||||
3
app/assets/stylesheets/rooms.scss
Normal file
3
app/assets/stylesheets/rooms.scss
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
// Place all the styles related to the Rooms controller here.
|
||||||
|
// They will automatically be included in application.css.
|
||||||
|
// You can use Sass (SCSS) here: https://sass-lang.com/
|
||||||
@ -19,8 +19,8 @@ class ApplicationController < ActionController::Base
|
|||||||
def user_not_authorized(exception)
|
def user_not_authorized(exception)
|
||||||
policy_name = exception.policy.class.to_s.underscore
|
policy_name = exception.policy.class.to_s.underscore
|
||||||
# flash[:alert] = "#{policy_name}.#{exception.query}"
|
# flash[:alert] = "#{policy_name}.#{exception.query}"
|
||||||
# flash[:alert] = "Unable to #{exception.query[0..-2]} #{policy_name.split("_")[0].capitalize}"
|
# flash[:error] = t "#{policy_name}.#{exception.query}", scope: "pundit", default: :default
|
||||||
flash[:error] = t "#{policy_name}.#{exception.query}", scope: "pundit", default: :default
|
flash[:alert] = "Unable to #{exception.query[0..-2]} #{policy_name.split("_")[0].capitalize}"
|
||||||
redirect_to (request.referrer || root_path)
|
redirect_to (request.referrer || root_path)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
28
app/controllers/rooms_controller.rb
Normal file
28
app/controllers/rooms_controller.rb
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
class RoomsController < ApplicationController
|
||||||
|
before_action :set_room, only: %i[ show edit update destroy ]
|
||||||
|
def index
|
||||||
|
@rooms = policy_scope Room.all
|
||||||
|
authorize @rooms
|
||||||
|
authorize @new_room = Room.new
|
||||||
|
end
|
||||||
|
|
||||||
|
def create
|
||||||
|
authorize @room = Room.build(name: params["room"]["name"])
|
||||||
|
@room.save
|
||||||
|
end
|
||||||
|
|
||||||
|
def show
|
||||||
|
authorize @room
|
||||||
|
@rooms = Room.public_rooms
|
||||||
|
|
||||||
|
render "index"
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
# Use callbacks to share common setup or constraints between actions.
|
||||||
|
def set_room
|
||||||
|
@room = Room.find(params[:id])
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
2
app/helpers/rooms_helper.rb
Normal file
2
app/helpers/rooms_helper.rb
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
module RoomsHelper
|
||||||
|
end
|
||||||
@ -1 +1,2 @@
|
|||||||
import 'controllers'
|
import 'controllers'
|
||||||
|
import "@hotwired/turbo-rails"
|
||||||
|
|||||||
5
app/models/room.rb
Normal file
5
app/models/room.rb
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
class Room < ActiveRecord::Base
|
||||||
|
validates_uniqueness_of :name
|
||||||
|
scope :public_rooms, -> {where(is_private: false)}
|
||||||
|
after_create_commit { broadcast_append_to "rooms" }
|
||||||
|
end
|
||||||
56
app/policies/room_policy.rb
Normal file
56
app/policies/room_policy.rb
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
class RoomPolicy
|
||||||
|
attr_reader :user, :model
|
||||||
|
|
||||||
|
def initialize(user, model)
|
||||||
|
@user = user || User.new
|
||||||
|
@room = model
|
||||||
|
end
|
||||||
|
|
||||||
|
class Scope
|
||||||
|
def initialize(user, scope)
|
||||||
|
@user = user
|
||||||
|
@scope = scope
|
||||||
|
end
|
||||||
|
|
||||||
|
def resolve
|
||||||
|
if user.admin?
|
||||||
|
scope.all
|
||||||
|
else
|
||||||
|
#TODO: private chats
|
||||||
|
scope.all
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
attr_reader :user, :scope
|
||||||
|
end
|
||||||
|
|
||||||
|
def index?
|
||||||
|
true
|
||||||
|
end
|
||||||
|
|
||||||
|
def show?
|
||||||
|
true
|
||||||
|
end
|
||||||
|
|
||||||
|
def new?
|
||||||
|
@user.admin?
|
||||||
|
end
|
||||||
|
|
||||||
|
def create?
|
||||||
|
@user.admin?
|
||||||
|
end
|
||||||
|
|
||||||
|
def edit?
|
||||||
|
@user.admin?
|
||||||
|
end
|
||||||
|
|
||||||
|
def update?
|
||||||
|
@user.admin?
|
||||||
|
end
|
||||||
|
|
||||||
|
def destroy?
|
||||||
|
@user.admin?
|
||||||
|
end
|
||||||
|
end
|
||||||
3
app/views/layouts/_new_room_form.html.haml
Normal file
3
app/views/layouts/_new_room_form.html.haml
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
= form_with(model: @new_room, remote: true) do |f|
|
||||||
|
= f.text_field :name, class: "form-control", autocomplete: 'off'
|
||||||
|
= f.submit data: { disable_with: false }
|
||||||
2
app/views/rooms/_room.html.haml
Normal file
2
app/views/rooms/_room.html.haml
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
%div
|
||||||
|
= link_to room.name, room
|
||||||
11
app/views/rooms/index.html.haml
Normal file
11
app/views/rooms/index.html.haml
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
- if @room
|
||||||
|
%h4= @room.name
|
||||||
|
|
||||||
|
%h1 Rooms index
|
||||||
|
|
||||||
|
= render partial: "layouts/new_room_form"
|
||||||
|
|
||||||
|
= turbo_stream_from "rooms"
|
||||||
|
#rooms
|
||||||
|
%div
|
||||||
|
= render @rooms
|
||||||
@ -1,5 +1,6 @@
|
|||||||
development:
|
development:
|
||||||
adapter: async
|
adapter: redis
|
||||||
|
url: redis://127.0.0.1:6379/1
|
||||||
|
|
||||||
test:
|
test:
|
||||||
adapter: test
|
adapter: test
|
||||||
|
|||||||
@ -6,3 +6,4 @@ pin '@hotwired/stimulus-loading', to: 'stimulus-loading.js', preload: true
|
|||||||
pin_all_from 'app/javascript/models', under: 'models'
|
pin_all_from 'app/javascript/models', under: 'models'
|
||||||
pin_all_from 'app/javascript/controllers', under: 'controllers'
|
pin_all_from 'app/javascript/controllers', under: 'controllers'
|
||||||
pin_all_from 'app/javascript/subscriptions', under: 'subscriptions'
|
pin_all_from 'app/javascript/subscriptions', under: 'subscriptions'
|
||||||
|
pin "@hotwired/turbo-rails", to: "turbo.min.js", preload: true
|
||||||
|
|||||||
@ -1,16 +1,16 @@
|
|||||||
module DevisePermittedParameters
|
# module DevisePermittedParameters
|
||||||
extend ActiveSupport::Concern
|
# extend ActiveSupport::Concern
|
||||||
|
|
||||||
included do
|
# included do
|
||||||
before_action :configure_permitted_parameters
|
# before_action :configure_permitted_parameters
|
||||||
end
|
# end
|
||||||
|
|
||||||
protected
|
# protected
|
||||||
|
|
||||||
def configure_permitted_parameters
|
# def configure_permitted_parameters
|
||||||
devise_parameter_sanitizer.permit(:sign_up, keys: [:name])
|
# devise_parameter_sanitizer.permit(:sign_up, keys: [:name])
|
||||||
devise_parameter_sanitizer.permit(:account_update, keys: [:name])
|
# devise_parameter_sanitizer.permit(:account_update, keys: [:name])
|
||||||
end
|
# end
|
||||||
end
|
# end
|
||||||
|
|
||||||
DeviseController.send :include, DevisePermittedParameters
|
# DeviseController.send :include, DevisePermittedParameters
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
Rails.application.routes.draw do
|
Rails.application.routes.draw do
|
||||||
|
resources :rooms
|
||||||
resources :commands do
|
resources :commands do
|
||||||
member do
|
member do
|
||||||
get 'run'
|
get 'run'
|
||||||
|
|||||||
10
db/migrate/20231203160243_create_rooms.rb
Normal file
10
db/migrate/20231203160243_create_rooms.rb
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
class CreateRooms < ActiveRecord::Migration[6.1]
|
||||||
|
def change
|
||||||
|
create_table :rooms do |t|
|
||||||
|
t.string :name
|
||||||
|
t.boolean :is_private, default: false
|
||||||
|
|
||||||
|
t.timestamps
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@ -10,7 +10,7 @@
|
|||||||
#
|
#
|
||||||
# It's strongly recommended that you check this file into your version control system.
|
# It's strongly recommended that you check this file into your version control system.
|
||||||
|
|
||||||
ActiveRecord::Schema.define(version: 2023_12_02_164132) do
|
ActiveRecord::Schema.define(version: 2023_12_03_160243) do
|
||||||
|
|
||||||
# These are extensions that must be enabled in order to support this database
|
# These are extensions that must be enabled in order to support this database
|
||||||
enable_extension "plpgsql"
|
enable_extension "plpgsql"
|
||||||
@ -179,6 +179,13 @@ ActiveRecord::Schema.define(version: 2023_12_02_164132) do
|
|||||||
t.index ["follower_id"], name: "index_relationships_on_follower_id"
|
t.index ["follower_id"], name: "index_relationships_on_follower_id"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
create_table "rooms", force: :cascade do |t|
|
||||||
|
t.string "name"
|
||||||
|
t.boolean "is_private", default: false
|
||||||
|
t.datetime "created_at", precision: 6, null: false
|
||||||
|
t.datetime "updated_at", precision: 6, null: false
|
||||||
|
end
|
||||||
|
|
||||||
create_table "seats", force: :cascade do |t|
|
create_table "seats", force: :cascade do |t|
|
||||||
t.integer "table_id"
|
t.integer "table_id"
|
||||||
t.integer "user_id"
|
t.integer "user_id"
|
||||||
|
|||||||
6
spec/factories/rooms.rb
Normal file
6
spec/factories/rooms.rb
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
FactoryGirl.define do
|
||||||
|
factory :room do
|
||||||
|
name "MyString"
|
||||||
|
is_private false
|
||||||
|
end
|
||||||
|
end
|
||||||
15
spec/helpers/rooms_helper_spec.rb
Normal file
15
spec/helpers/rooms_helper_spec.rb
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
# Specs in this file have access to a helper object that includes
|
||||||
|
# the RoomsHelper. For example:
|
||||||
|
#
|
||||||
|
# describe RoomsHelper do
|
||||||
|
# describe "string concat" do
|
||||||
|
# it "concats two strings with spaces" do
|
||||||
|
# expect(helper.concat_strings("this","that")).to eq("this that")
|
||||||
|
# end
|
||||||
|
# end
|
||||||
|
# end
|
||||||
|
RSpec.describe RoomsHelper, type: :helper do
|
||||||
|
pending "add some examples to (or delete) #{__FILE__}"
|
||||||
|
end
|
||||||
5
spec/models/room_spec.rb
Normal file
5
spec/models/room_spec.rb
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
RSpec.describe Room, type: :model do
|
||||||
|
pending "add some examples to (or delete) #{__FILE__}"
|
||||||
|
end
|
||||||
11
spec/requests/rooms_spec.rb
Normal file
11
spec/requests/rooms_spec.rb
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
RSpec.describe "Rooms", type: :request do
|
||||||
|
describe "GET /index" do
|
||||||
|
it "returns http success" do
|
||||||
|
get "/rooms/index"
|
||||||
|
expect(response).to have_http_status(:success)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
5
spec/views/rooms/index.html.haml_spec.rb
Normal file
5
spec/views/rooms/index.html.haml_spec.rb
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
RSpec.describe "rooms/index.html.haml", type: :view do
|
||||||
|
pending "add some examples to (or delete) #{__FILE__}"
|
||||||
|
end
|
||||||
Loading…
x
Reference in New Issue
Block a user