-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
53 changed files
with
380 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
module ApplicationCable | ||
class Channel < ActionCable::Channel::Base | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
# frozen_string_literal: true | ||
|
||
# Web socket channel for our example app | ||
class HelloQuotesChannel < ApplicationCable::Channel | ||
def subscribed | ||
stream_from "hello_quotes" | ||
stream_from 'hello_quotes' | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,29 @@ | ||
class Api::V1::QuotesController < ApplicationController | ||
include CableReady::Broadcaster | ||
def index | ||
respond_to do |format| | ||
format.json { render json: Quote.order(created_at: :desc).all } | ||
# frozen_string_literal: true | ||
|
||
module Api | ||
module V1 | ||
# Quotes API controller for our example app | ||
class QuotesController < ApplicationController | ||
include CableReady::Broadcaster | ||
def index | ||
respond_to do |format| | ||
format.json { render json: Quote.order(created_at: :desc).all } | ||
end | ||
end | ||
|
||
def create | ||
quote = Quote.create(content: params[:content], author_name: params[:author]) | ||
respond_to do |format| | ||
format.json { render json: quote } | ||
end | ||
end | ||
|
||
def destroy | ||
Quote.find(params[:id]).destroy | ||
respond_to do |format| | ||
format.json { render json: params[:id] } | ||
end | ||
end | ||
end | ||
end | ||
def create | ||
quote = Quote.create(content: params[:content], author_name: params[:author]) | ||
respond_to do |format| | ||
format.json { render json: quote } | ||
end | ||
end | ||
def destroy | ||
Quote.find(params[:id]).destroy | ||
respond_to do |format| | ||
format.json { render json: params[:id] } | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
# frozen_string_literal: true | ||
|
||
class ApplicationController < ActionController::Base | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
# For top level view helper methods | ||
module ApplicationHelper | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
# frozen_string_literal: true | ||
|
||
# Constants and methods for all models | ||
class ApplicationRecord < ActiveRecord::Base | ||
self.abstract_class = true | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,52 @@ | ||
class Quote < ApplicationRecord | ||
include CableReady::Broadcaster | ||
after_create_commit -> { | ||
# frozen_string_literal: true | ||
|
||
# == Schema Information | ||
# | ||
# Table name: quotes | ||
# | ||
# id :bigint not null, primary key | ||
# author_name :string | ||
# content :text | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# | ||
class Quote < ApplicationRecord | ||
include CableReady::Broadcaster | ||
after_create_commit lambda { | ||
cable_ready['hello_quotes'].insert_adjacent_html( | ||
selector: '#quotes_list', | ||
position: 'afterBegin', | ||
html: ApplicationController.new.render_to_string(partial: 'welcome/quote.html.erb', locals: {quote: self, prefix: nil}) | ||
html: ApplicationController.new.render_to_string(partial: 'welcome/quote.html.erb', | ||
locals: { quote: self, prefix: nil }) | ||
).broadcast | ||
|
||
ActionCable.server.broadcast("hello_quotes", { | ||
type: "RQA::CreateQuoteSuccess", | ||
response: { | ||
entities: { | ||
quotes: { | ||
"#{id}": self | ||
} | ||
}, | ||
result: id | ||
} | ||
} | ||
) | ||
|
||
broadcast_prepend_to 'hello_turbo', target: "quotes_list_turbo", partial: "welcome/quote", locals: { prefix: "turbo-" } | ||
ActionCable.server.broadcast('hello_quotes', { | ||
type: 'RQA::CreateQuoteSuccess', | ||
response: { | ||
entities: { | ||
quotes: { | ||
"#{id}": self | ||
} | ||
}, | ||
result: id | ||
} | ||
}) | ||
|
||
broadcast_prepend_to 'hello_turbo', target: 'quotes_list_turbo', partial: 'welcome/quote', | ||
locals: { prefix: 'turbo-' } | ||
} | ||
|
||
after_destroy_commit -> { | ||
after_destroy_commit lambda { | ||
cable_ready['hello_quotes'].remove( | ||
selector: "#quote-#{id}" | ||
).remove( | ||
selector: "#turbo-quote-#{id}" | ||
).broadcast | ||
ActionCable.server.broadcast("hello_quotes", { | ||
type: "RQA::DeleteQuoteSuccess", | ||
response: { | ||
data: id.to_i | ||
} | ||
} | ||
) | ||
).broadcast | ||
ActionCable.server.broadcast('hello_quotes', { | ||
type: 'RQA::DeleteQuoteSuccess', | ||
response: { | ||
data: id.to_i | ||
} | ||
}) | ||
} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
# frozen_string_literal: true | ||
|
||
# Reflex methods for our example app | ||
class QuotesReflex < StimulusReflex::Reflex | ||
def destroy | ||
def destroy | ||
id = element.dataset[:id] | ||
Quote.find(id).destroy | ||
morph :nothing | ||
Quote.find(id).destroy | ||
morph :nothing | ||
end | ||
end |
Oops, something went wrong.