Skip to content

Instantly share code, notes, and snippets.

iainbeeston / after.rb
Created January 20, 2023 11:45
Eager loading polymorphic associations
View after.rb
class Picture < ApplicationRecord
belongs_to :employee , -> { left_joins(:picture).merge(Picture.of_employee) }, foreign_key: :imageable_id, inverse_of: :pictures
belongs_to :product , -> { left_joins(:product).merge(Picture.of_product) }, foreign_key: :imageable_id, inverse_of: :pictures
scope :of_employee, -> { where(imageable_type: "Employee") }
scope :of_product, -> { where(imegeable_type: "Product") }
class Employee < ApplicationRecord
has_many :pictures, -> { of_employee }, dependent: nil, foreign_key: :imageable_id, inverse_of: :employee
View find_duplicate_methods.rb
#!/usr/bin/env ruby
require 'tempfile'
def get_methods(file_contents)
code_for_all_methods = file_contents.scan(/((\s+?)def .*?^\2end)/m)
code_for_all_methods.each_with_object({}) do |(code), hsh|
method_name = code[/def (?:self\.)?(\w+)/, 1]
hsh[method_name.to_s] = code
iainbeeston / after-renaming-to-pug.log
Created August 12, 2019 10:12
Code combat webpack logs
View after-renaming-to-pug.log
npm WARN lifecycle The node binary used for scripts is /Users/iainbeeston/.nodenv/shims/node but npm is using /Users/iainbeeston/.nvm/versions/node/v8.15.0/bin/node itself. Use the `--scripts-prepend-node-path` option to include the path for the node binary npm was executed with.
> codecombat@1.0.0 webpack /Users/iainbeeston/dev/codecombat
> webpack "--watch"
Automatically using Development webpack config
Starting Webpack...
Webpack is watching the files…
iainbeeston / arrow_function_event_handlers.rb
Created July 21, 2017 11:48
Re-define react event handler methods using arrow functions
View arrow_function_event_handlers.rb
#!/usr/bin/env ruby
# Given some react components written as ES6 classes, this converts any event handler methods from "normal"
# class methods to be arrow functions, assigned to static variables in the class. That way the method is
# always bound to "this".
file_paths = ARGV
iainbeeston / recursive_find_subclasses.rb
Last active June 28, 2017 14:14
Recursively find and print out all subclasses
View recursive_find_subclasses.rb
#!/usr/bin/env ruby
# recursively find classes
starting_file, working_directory = *ARGV
throw 'Please specify the file that contains the base class and the source directory' unless starting_file && working_directory
def files_that_subclass(file_path, dir)
contents =
View gist:3bc2379ff4b34aea37d6
ruby-install -p -p -p -p -p ruby 2.0.0-p353
View gist:a200090a767d7d381311
### Keybase proof
I hereby claim:
* I am iainbeeston on github.
* I am iainbeeston ( on keybase.
* I have a public key whose fingerprint is BD22 1F09 7190 335D 4E95 8A76 EC60 2EFF 2326 F692
To claim this, I am signing this object:
iainbeeston / mapit.mkd
Last active December 27, 2015 18:29 — forked from ismasan/mapit.mkd
View mapit.mkd
iainbeeston / gist:3194914
Created July 28, 2012 21:41
brew install postgresql on mountain lion
View gist:3194914
==> Downloading
Already downloaded: /Library/Caches/Homebrew/postgresql-9.1.4.tar.bz2
==> Patching
patching file src/pl/plpython/Makefile
==> ./configure --disable-debug --prefix=/Users/iain/.homebrew/Cellar/postgresql/9.1.4 --data
checking build system type... x86_64-apple-darwin12.0.0
checking host system type... x86_64-apple-darwin12.0.0
checking which template to use... darwin
checking whether to build with 64-bit integer date/time support... yes
checking whether NLS is wanted... no
iainbeeston / application_helper.rb
Created March 6, 2012 23:43
Facebook real-time updates Rails Metal controller.
View application_helper.rb
module ApplicationHelper
def signature(str, key = nil)
key ||= CONFIG['facebook']['secret'] if defined?(CONFIG)
OpenSSL::HMAC.hexdigest('sha1'), key, str)