Skip to content

Instantly share code, notes, and snippets.

Douglas Teoh dteoh

Block or report user

Report or block dteoh

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@dteoh
dteoh / arel_select_star_from_table.rb
Last active Dec 2, 2019
Select all columns of an Arel table
View arel_select_star_from_table.rb
users = User.arel_table
query = users.project(users[Arel.star])
@dteoh
dteoh / mysql2-gem-install.md
Created Nov 22, 2019
Installing mysql2 gem
View mysql2-gem-install.md

Installing mysql2 gem

This is always an annoying process, especially when you are setting up a new computer. I assume you are using macOS + homebrew. I also assume that you want to run an older version of MySQL (although the instructions should be adaptable).

Installing MySQL

$ brew install mysql@5.7 # change the version if needed
@dteoh
dteoh / custom_ar_relation_relay_connection.rb
Last active Oct 28, 2019
GrapQL Ruby: Custom Relay connection class for ActiveRecord::Relation objects
View custom_ar_relation_relay_connection.rb
# This custom relay connection class exists because the built-in connection
# class is broken when max_page_size is used.
#
# See: https://github.com/rmosolgo/graphql-ruby/issues/1109
class CustomArRelationRelayConnection < GraphQL::Relay::BaseConnection
def cursor_from_node(item)
cursor_col = item.class.implicit_order_column
encode(item.send(cursor_col).to_s)
end
@dteoh
dteoh / dictionary.md
Created Oct 23, 2019
Display dictionary definition on macOS
View dictionary.md
  1. Move mouse cursor to point at a word
  2. Press Command + Ctrl + D
@dteoh
dteoh / cors-curl.md
Created Sep 26, 2019
Check CORS headers with `curl`
View cors-curl.md
$ curl -X OPTIONS -H “Origin: REQUESTER-DOMAIN.com” -H “Access-Control-Request-Method: GET“ -s -v <TARGET URL HERE> 1> /dev/null
@dteoh
dteoh / macos_no_sleep.md
Created Feb 23, 2017
Preventing macOS from sleeping
View macos_no_sleep.md

There is a built-in command line utility caffeinate that can be used to prevent macOS from sleeping.

To prevent macOS from sleeping before a process with given PID exits:

$ caffeinate -w PID

To prevent macOS from sleeping for the next N seconds:

@dteoh
dteoh / put_post_json_rails_5_integration_test.md
Created Jul 12, 2016
PUT or POST JSON in a Rails 5 ActionDispatch::IntegrationTest
View put_post_json_rails_5_integration_test.md

In Rails 5, the preferred base class for testing controllers is ActionDispatch::IntegrationTest.

If you have an API that receives parameters as JSON request bodies, here are some helper methods to facilitate testing:

class ActionDispatch::IntegrationTest
  def put_json(path, obj)
    put path, params: obj.to_json, headers: { 'CONTENT_TYPE' => 'application/json' }
  end
@dteoh
dteoh / neovim_python.md
Created Jun 26, 2019
NeoVim + Python 3 provider
View neovim_python.md

Assuming that [Conda][1] is used, start by creating an environment for NeoVim:

$ conda create -n neovim36 python=3.6

Fish shell users:

$ source (conda info --root)/etc/fish/conf.d/conda.fish  
@dteoh
dteoh / unused-this.md
Last active Apr 17, 2019
Solving "unused variable this." warnings when using ReasonML objects
View unused-this.md

unused variable this

You can create [objects in ReasonML][1]. For example:

let document = {
  pub title = "My Treatise";
  pub contents = "<a lot of words>"
};
@dteoh
dteoh / records-and-bs-json.md
Last active Apr 2, 2019
A pattern for encoding records with bs-json
View records-and-bs-json.md

A pattern for encoding records with bs-json

If you have a record type like:

type post = {
  title: string,
  body: string,
};
You can’t perform that action at this time.