Skip to content

Instantly share code, notes, and snippets.

View BrianSigafoos's full-sized avatar

Brian Sigafoos BrianSigafoos

View GitHub Profile
@BrianSigafoos
BrianSigafoos / postgres-cheatsheet.md
Created January 25, 2017 17:38 — forked from Kartones/postgres-cheatsheet.md
PostgreSQL command line cheatsheet

PSQL

Magic words:

psql -U postgres

If run with -E flag, it will describe the underlaying queries of the \ commands (cool for learning!).

Most \d commands support additional param of __schema__.name__ and accept wildcards like *.*

@BrianSigafoos
BrianSigafoos / Gemfile.md
Last active August 16, 2017 16:26
Rails favorites - for faster app setup

Gemfile favorites

group :test do
  # Minitest - Rails default, but updated more often via gem https://github.com/seattlerb/minitest
  gem 'minitest'

  # Better reporting for minitiest - ala Rspec https://github.com/kern/minitest-reporters
  gem 'minitest-reporters'
@BrianSigafoos
BrianSigafoos / rails_setup.md
Last active August 26, 2017 14:54
Rails setup notes

Rails + React + PostgreSQL on Heroku

$ rails new <app-name> --database=postgresql --webpack=react
$ cd <app-name>
$ echo '.idea' >> .gitignore      # remove RubyMine's config
$ echo "ruby '2.4.0'" >> Gemfile  # for Heroku Ruby version
$ git add .
$ git commit -m "Initial commit"

# Create new repo on GitHub with same name...
@BrianSigafoos
BrianSigafoos / testing.md
Last active August 17, 2017 20:14
Testing syntax, using Shoulda matchers

Fixtures

DEFAULTS: &DEFAULTS
  payment_trail:      $LABEL
  identifier:         $LABEL_stripe_key
  amount_cents:       <%= AMOUNT_CENTS[:one_h] %>
  paid:               false
  status:             <%= Charge::STATUSES[0] %>
  state:              <%= Charge::STATES[0] %>
  created_at:         now  # special `now` or can use <%= 1.week.from_now %>
@BrianSigafoos
BrianSigafoos / metaprogramming.md
Last active March 8, 2023 14:43
Metaprogramming in Ruby

Dynamic Method

# Decide how to define a method at runtime
class C
end

C.class_eval do
  define_method :my_method do
    'a dynamic method'
 end
@BrianSigafoos
BrianSigafoos / rails_generators.md
Last active September 26, 2017 21:50
Rails generators

Rails Generators

rails generate <type> --help # for examples

# Model (+ migration, test, fixtures)
rails generate model NAME [field[:type][:index] field[:type][:index]] [options]

rails generate model post title:string blog:references published:boolean position:integer
rails generate model product supplier:references{polymorphic}
@BrianSigafoos
BrianSigafoos / model_validations.md
Created August 22, 2017 17:56
Rails model layout and validations

Model layout & validations

class User < ActiveRecord::Base
  # keep the default scope first (if any)
  default_scope { where(active: true) }

  # constants come up next
  COLORS = %w(red green blue)

  # afterwards we put attr related macros
@BrianSigafoos
BrianSigafoos / curl_bash.md
Created August 24, 2017 16:55
curl + REST API

curl testing a REST API

# HEAD request: -I 
$ curl -I  http://0.0.0.0:3000/posts

# -i (--include) protocol headers and -X (--request) for GET, PUT, DELETE
$ curl -i -X HEAD http://0.0.0.0:3000/posts

# prettify JSON results with python -mjson.tool 
$ curl ... | python -mjson.tool
@BrianSigafoos
BrianSigafoos / README.md
Created August 24, 2017 18:54 — forked from joyrexus/README.md
curl tutorial

An introduction to curl using GitHub's API.

Basics

Makes a basic GET request to the specifed URI

curl https://api.github.com/users/caspyin

Includes HTTP-Header information in the output

@BrianSigafoos
BrianSigafoos / capybara.md
Created August 31, 2017 20:55 — forked from remi/capybara.md
RSpec Matchers

Capybara

save_and_open_page

Matchers

have_button(locator)