Skip to content

Instantly share code, notes, and snippets.

@iainbeeston
iainbeeston / after.rb
Created January 20, 2023 11:45
Eager loading polymorphic associations
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") }
end
class Employee < ApplicationRecord
has_many :pictures, -> { of_employee }, dependent: nil, foreign_key: :imageable_id, inverse_of: :employee
end
#!/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
end
@iainbeeston
iainbeeston / after-renaming-to-pug.log
Created August 12, 2019 10:12
Code combat webpack logs
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
iainbeeston / arrow_function_event_handlers.rb
Created July 21, 2017 11:48
Re-define react event handler methods using arrow functions
#!/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
iainbeeston / recursive_find_subclasses.rb
Last active June 28, 2017 14:14
Recursively find and print out all subclasses
#!/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 = File.read(file_path)
ruby-install -p https://raw.github.com/skaes/rvm-patchsets/master/patches/ruby/2.0.0/p353/railsexpress/01-zero-broken-tests.patch -p https://raw.github.com/skaes/rvm-patchsets/master/patches/ruby/2.0.0/p353/railsexpress/02-railsexpress-gc.patch -p https://raw.github.com/skaes/rvm-patchsets/master/patches/ruby/2.0.0/p353/railsexpress/03-display-more-detailed-stack-trace.patch -p https://raw.github.com/skaes/rvm-patchsets/master/patches/ruby/2.0.0/p353/railsexpress/04-show-full-backtrace-on-stack-overflow.patch -p https://raw.github.com/skaes/rvm-patchsets/master/patches/ruby/2.0.0/p353/railsexpress/05-fix-missing-c-return-event.patch ruby 2.0.0-p353
### Keybase proof
I hereby claim:
* I am iainbeeston on github.
* I am iainbeeston (https://keybase.io/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
iainbeeston / mapit.mkd
Last active December 27, 2015 18:29 — forked from ismasan/mapit.mkd
@iainbeeston
iainbeeston / gist:3194914
Created July 28, 2012 21:41
brew install postgresql on mountain lion
==> Downloading http://ftp.postgresql.org/pub/source/v9.1.4/postgresql-9.1.4.tar.bz2
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
iainbeeston / application_helper.rb
Created March 6, 2012 23:43
Facebook real-time updates Rails Metal controller.
module ApplicationHelper
def signature(str, key = nil)
key ||= CONFIG['facebook']['secret'] if defined?(CONFIG)
OpenSSL::HMAC.hexdigest(OpenSSL::Digest::Digest.new('sha1'), key, str)
end
end