Skip to content

Instantly share code, notes, and snippets.

@bbugh
bbugh / tinymce_spec_helper.rb
Created October 20, 2017 20:34
Improved handling of TinyMCE with error logging and guard checks
module TinyMCESpecHelper
# Fill in a TinyMCE editor with the specified value.
# Pass in the same id you give to TinyMCE, (such as "editorContent")
# *not* TinyMCE's generated one.
#
# NOTE: The first argument is not a css selector, just an element id.
#
# May require selenium chromedriver!
# Adapted from https://gist.github.com/eoinkelly/69be6c27beb0106aa555
#
@bbugh
bbugh / indifferent_hash_serializer.rb
Created October 6, 2017 14:24
IndifferentHashSerializer for Rails json/jsonb columns
# app/serializers/indifferent_hash_serializer.rb
# Used for converting a json or jsonb column into a useable Rails hash instead
# of the psuedo-json hash that is normally returned by Rails.
class IndifferentHashSerializer
def self.dump(hash)
hash.to_json
end
def self.load(hash)
@bbugh
bbugh / spec_helper.rb
Last active September 14, 2017 20:25 — forked from anolson/spec_helper.rb
Profile ActiveRecord queries, output Markdown table of counts
# Originally referenced from https://medium.com/treehouse-engineering/continous-improvements-4741fc3c7daa
RSpec.configure do |config|
config.before(:suite) do
Thread.current[:query_counter] = Hash.new(0)
end
config.around(:example) do |procsy|
callback = lambda do |*args|
event = ActiveSupport::Notifications::Event.new(*args)
@bbugh
bbugh / .overcommit.yml
Created June 25, 2017 18:59
Overcommit setup for Rails apps
# Use this file to configure the Overcommit hooks you wish to use. This will
# extend the default configuration defined in:
# https://github.com/brigade/overcommit/blob/master/config/default.yml
#
# At the topmost level of this YAML file is a key representing type of hook
# being run (e.g. pre-commit, commit-msg, etc.). Within each type you can
# customize each hook, such as whether to only run it on certain files (via
# `include`), whether to only display output if it fails (via `quiet`), etc.
#
# For a complete list of hooks, see:
@bbugh
bbugh / ipython_magic_function_inspector.py
Last active December 17, 2016 00:46
iPython Django extensions
"""
An IPython magic function to pretty-print objects with syntax highlighting.
Updated to also pretty print the object's __dict__ if it's available.
See, "Defining your own magics":
http://ipython.org/ipython-doc/stable/interactive/reference.html#defining-your-own-magics
For more on Pygments:
http://pygments.org/docs/quickstart/
Usage
# Elixir solution for http://www.codewars.com/kata/dont-drink-the-water
defmodule Liquids do
@density %{ ?H => 1.36, ?W => 1.00, ?A => 0.87, ?O => 0.8 }
def separate_liquids([]), do: []
def separate_liquids([h|_] = glass) when is_list(h) and length(h) > 0 do
List.flatten(glass) |> Enum.sort_by(&(@density[&1])) |> Enum.chunk(length(h))
end
end
@bbugh
bbugh / introrx.md
Last active March 12, 2016 18:43 — forked from staltz/introrx.md
The introduction to Reactive Programming you've been missing

The introduction to Reactive Programming you've been missing

(by @andrestaltz)

So you're curious in learning this new thing called Reactive Programming, particularly its variant comprising of Rx, Bacon.js, RAC, and others.

Learning it is hard, even harder by the lack of good material. When I started, I tried looking for tutorials. I found only a handful of practical guides, but they just scratched the surface and never tackled the challenge of building the whole architecture around it. Library documentations often don't help when you're trying to understand some function. I mean, honestly, look at this:

Rx.Observable.prototype.flatMapLatest(selector, [thisArg])

Projects each element of an observable sequence into a new sequence of observable sequences by incorporating the element's index and then transforms an observable sequence of observable sequences into an observable sequence producing values only from the most recent observable sequence.

@bbugh
bbugh / jquery-practice
Created December 7, 2014 18:27
Practicing events with jQuery
<!DOCTYPE html>
<html lang="en">
<head>
<title>jQuery Practice</title>
</head>
<body>
<div id="content">
<div class="entry">
<h1>You Won't Believe What Happens When You Click This Link</h1>
<a href="#">Click me!</a>
@bbugh
bbugh / rvm-sublime-text-2.md
Last active August 29, 2015 13:57
Using RVM in Sublime Text 2

Start with:

subl ~/Library/Application\ Support/Sublime\ Text\ 2/Packages/Ruby/Ruby.sublime-build

Copy and paste this, replacing the contents of that file with this:

{
  "env":{
 "PATH":"${HOME}/.rvm/bin:${PATH}"
@bbugh
bbugh / gist:5207416
Created March 20, 2013 18:57
Creating a valid email address from a name and email, using the Ruby Mail library. Used to prevent invalid headers.

If you are taking user input for name and email, then unless you very carefully validate or escape the name and email, you can end up with an invalid From header by simply concatenating strings. Here is a safe way:

require 'mail'
address = Mail::Address.new email # ex: "john@example.com"
address.display_name = name # ex: "John Doe"
# Set the From or Reply-To header to the following:
address.format # returns "John Doe <john@example.com>"