Skip to content

Instantly share code, notes, and snippets.

$redis.publish 'realtime_msg', {
msg: 'hello world - ' + SecureRandom.hex,
recipient_user_ids: [41, 42]
}.to_json
$redis.publish 'realtime_msg', {
channel: "/chats/#{@chat.id}",
event: 'chat',
message: {
html: @message,
sender: @member.username
}
}.to_json
socket.on('subscribe_public', function (m) {
socket.join(m.channel);
});
socket.on('subscribe_private', function (m) {
if (m.jwt && is_valid_jwt(m.jwt)) {
socket.join(m.channel);
}
});
// From client side, they just need to subscribe to what they need.
window.realtime.socketIo.emit('subscribe_public', {
channel: ch
});
// or the private one,
window.realtime.socketIo.emit('subscribe_private', {
channel: ch,
jwt: localStorage.jwt
});
DatabaseCleaner.strategy = :truncation, {:only => %w[widgets dogs some_other_collection]}
# or
DatabaseCleaner.strategy = :truncation, {:except => %w[widgets]}
Before do
# ...
# list of all collection
Chat.delete_all
end
class OmniauthCallbacksController < Devise::OmniauthCallbacksController
def self.provides_callback_for(provider)
class_eval %Q{
def #{provider}
# NOTE:
# Extract Oauth logic to a new ruby class.
@user = FactoryMethod::User.create("#{provider}", env["omniauth.auth"], current_user)
if @user.persisted?
# factory_method/user.rb
module FactoryMethod
# [Role] Factory
class User
def self.create(type, data=nil, current_user=nil)
if type == 'normal'
# login normally
else
module FactoryMethod
# [Role] Product
class FacebookUser
def initialize(identity, data)
@identity = identity
@data = data
end
def create
@user = User.where(email: @data.info.email)
def paid!
# ...
if self.status == Order::UNPAID
# sent email, change status, ...
elsif self.status == Order::PAID
# do nothing
end
# ...