Skip to content

Instantly share code, notes, and snippets.

@seanlinsley
seanlinsley / chat_controller.rb
Last active August 26, 2020 01:13
Websockets with Postgres listen/notify and the Tubesock gem
require 'pg_notify'
class ChatController < ApplicationController
include Tubesock::Hijack
@@notifier = PGNotify.new 'chat'
def chat
hijack do |websocket|
websocket.onopen do
@@notifier.subscribe websocket do |payload|
<!DOCTYPE html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.0.8/d3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/1.3.1/lodash.min.js"></script>
<meta charset=utf-8 />
<title>Flame Graph of Page</title>
@seanlinsley
seanlinsley / precompile.rake
Created November 15, 2015 16:21
Support undigested assets in Rails 4.2
task 'assets:precompile' => 'environment' do
# See https://github.com/rails/sprockets-rails/issues/49
#
# Sprockets no longer creates undigested assets, so we have to
# emulate the old behavior to keep vendored JS libraries happy
#
# For the record,
# Digested: 4-star-de4f1f10cb13e222111c8afe0b516131.jpg
# Undigested: 4-star.jpg
# When you have really complex scenarios you have to test, your tests can become very long
# and duplicative. Here's a simple way to prevent that:
describe SomeController do
def scenario_setup
post :foo
expect(controller.current_user).to be_a User
# some more complex stuff that takes a lot of space
@seanlinsley
seanlinsley / at.rb
Last active September 2, 2015 19:20
def at(time)
original = Time.now
allow(Time).to receive(:now) { time }
yield
ensure
allow(Time).to receive(:now) { original }
end
at 12.hours.from_now do
# your test
@seanlinsley
seanlinsley / child.rb
Last active August 29, 2015 14:28
Inheritable settings between two models, in Rails
class Child < ActiveRecord::Base
belongs_to :parent
INHERITABLE_SETTINGS = %i[foo bar baz]
INHERITABLE_SETTINGS.each do |attr|
define_method attr do
value = read_attribute(attr)
default = default_setting(attr)
inherited = inherited_setting(attr)
@seanlinsley
seanlinsley / gist:193b93d5854c433a2b18
Last active August 29, 2015 14:20
why no implicit arguments for super inside define_method?
# setup
class User
def name; 'Bob'; end
end
class Object
def wrap(method, &block)
prepend Module.new { define_method method, &block }
end
end
@seanlinsley
seanlinsley / database.yml
Last active August 29, 2015 14:19
Rails: retry connecting to Postgres
development: &default
adapter: postgresql
database: app_development
connect_retry_timeout: 1
@seanlinsley
seanlinsley / seeyouspacecowboy.rb
Last active August 30, 2020 23:46
A Ruby verison, because the ANSI colors weren't showing up on logout in the Shell version this is forked from
#!/usr/bin/env ruby
# SEE YOU SPACE COWBOY by DANIEL REHN (danielrehn.com)
# Displays a timeless message in your terminal with cosmic color effects
# Usage: add `ruby ~/seeyouspacecowboy.rb; sleep 2` to .bash_logout (or similar) in your home directory
# (adjust the sleep variable to display the message for more seconds)
class String
def colorize(color_code)
@seanlinsley
seanlinsley / def!.rb
Last active January 12, 2016 23:56
For slightly more future-proof monkey patches, raise an error if a method name starts being used upstream
# class SomethingYouDontControl
# def! :something_you_expect_not_to_exist do |arg|
# arg + 1
# end
# end
#
# I wanted to provide built-in memoization, but it's not currenlty possible
# while preserving strict argument checking. https://bugs.ruby-lang.org/issues/9777
#
module Kernel