Skip to content

Instantly share code, notes, and snippets.

@Haguilar91
Created December 6, 2019 14:58
Show Gist options
  • Save Haguilar91/734b99227231656e5681171910c3d4c1 to your computer and use it in GitHub Desktop.
Save Haguilar91/734b99227231656e5681171910c3d4c1 to your computer and use it in GitHub Desktop.
Action Cable with Devise and Mailboxer for realtime chat
module Accessible
extend ActiveSupport::Concern
included do
before_action :check_user
end
protected
def check_user
if current_doctor
flash.clear
# if you have rails_admin. You can redirect anywhere really
redirect_to(rails_admin.dashboard_path) && return
elsif current_user
flash.clear
# The authenticated root path can be defined in your routes.rb in: devise_scope :user do...
redirect_to(authenticated_user_root_path) && return
end
end
end
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
prepend_before_action :set_locale
before_action :configure_permitted_parameters, if: :devise_controller?
before_action :masquerade_user!
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:name,:username])
devise_parameter_sanitizer.permit(:account_update, keys: [:name])
end
#def after_sign_in_path_for(resource)
# redirect_to "landing#index"
# end
def set_locale
I18n.locale = I18n.default_locale
end
devise_group :member, contains: [:user, :doctor]
private
def pundit_user
# Make Pundit to use whichever Devise model [Organizer, Sponsor, User] as the 'current_user'
# Just by using the offered helper method from Devise, 'devise_group' feature
current_member
end
end
module ApplicationCable
class Connection < ActionCable::Connection::Base
identified_by :current_member
def connect
self.current_member = find_verified_user
logger.add_tags "ActionCable", "User #{current_member.id}"
end
protected
def find_verified_user
if verified_user = User.find_by(id: cookies.signed['user.id'])
verified_user
elsif verified_user = Doctor.find_by(id: cookies.signed['doctor.id'])
verified_user
else
reject_unauthorized_connection
end
end
end
end
class ConversationsController < ApplicationController
def index
@conversations = current_member.mailbox.conversations
end
def show
@conversation = current_member.mailbox.conversations.find(params[:id])
end
def new
@recipients = Doctor.all
end
def create
recipient = Doctor.find(params[:user_id])
receipt = current_member.send_message(recipient, params[:body],params[:subject])
redirect_to conversation_path(receipt.conversation)
end
end
class MessagesController < ApplicationController
before_action :set_conversation
def create
receipt = current_member.reply_to_conversation(@conversation,params[:body])
if receipt.save
ActionCable.server.broadcast "room_channel",
{content: receipt.message.body, sender: receipt.message.sender.name}
else
end
redirect_to conversation_path(receipt.conversation)
end
private
def set_conversation
@conversation = current_member.mailbox.conversations.find(params[:conversation_id])
end
end
import consumer from "./consumer"
consumer.subscriptions.create("RoomChannel", {
connected() {
// Called when the subscription is ready for use on the server
console.log("Connected to the room!")
},
disconnected() {
// Called when the subscription has been terminated by the server
},
received(data) {
// Called when there's incoming data on the websocket for this channel
$('#msg').append('<div class="message"> ' + data.sender + '&nbsp; dijo: &nbsp;' + data.content + '&nbsp;' + '</div>')
console.log("Receiving:")
console.log(data.content)
}
});
let submit_messages;
$(document).on('turbolinks:load', function () {
submit_messages()
})
submit_messages = function () {
$('#message_content').on('keydown', function (event) {
if (event.keyCode == 13) {
$('input').click()
event.target.value = ''
event.preventDefault()
}
})
}
<h1><%=@conversation.subject %></h1>
<% @conversation.receipts_for(current_member).each do |receipt|%>
<p><%= receipt.message.sender.name%> dijo: &nbsp; <%= receipt.message.body%></p>
<%end%>
<div id="msg">
</div>
<%= form_tag conversation_messages_path(@conversation), method: :post do%>
<div>
<%= text_area_tag :body%>
</div>
<%= submit_tag %>
<%end%>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment