Skip to content

Instantly share code, notes, and snippets.

@travisofthenorth
travisofthenorth / intersection_authorize.rb
Created October 7, 2018 23:13
Intersection use case - authorize a user who has an allowed role/ability
class PaymentsController < AuthenticatedController
before_action { authorize!(roles: [:superuser], abilities: [:charge_user, :manage_payments]) }
end
class AuthenticatedController < ApplicationController
def authorize!(roles: [], abilities: [])
# current
(roles & current_user.roles).any? || (abilities & current_user.abilities).any?
# desired
@travisofthenorth
travisofthenorth / not_null_check
Last active October 18, 2017 23:11
PG not-null constraint as a check
[development] # CREATE TABLE test (id integer, name text);
[development] # ALTER TABLE test ADD CHECK (name is not null) NOT VALID;
[development] # \d test
Table "public.test"
Column | Type | Modifiers
--------+---------+-----------
id | integer |
name | text |
Check constraints:
"test_name_check" CHECK (name IS NOT NULL) NOT VALID
@travisofthenorth
travisofthenorth / pluck_in_batches.rb
Created September 29, 2017 18:33
Pluck in batches
module MorePluckable
extend ActiveSupport::Concern
class_methods do
def pluck_in_batches(*column_names, batch_size: 1000)
column_names = column_names.map(&:to_sym)
column_names.delete(:id)
column_names.unshift(:id)
data = []
@travisofthenorth
travisofthenorth / index_searchable.rb
Created June 22, 2017 14:22
A Chewy mixin to make searching a bit nicer
module IndexSearchable
extend ActiveSupport::Concern
class_methods do
def search(options = {})
queries = searchable_terms.map { |term| term_search(term, options[term.to_sym]) }
queries << query_search(options[:query])
queries.compact.reduce(:merge)
end
@travisofthenorth
travisofthenorth / s3.rb
Created June 15, 2017 15:58
Upload/download files to/from S3
# Upload local file at filepath to <fog_directory>/directory/filename
def upload_file(filepath, directory, filename: nil, acl: 'public-read')
filename ||= File.basename(filepath)
s3 = Aws::S3::Resource.new(region: region)
obj = s3.bucket(bucket).object("#{directory}/#{filename}")
obj.upload_file(filepath, acl: acl)
end
# Download file from <fog_directory>/remote_filepath to local filepath
@travisofthenorth
travisofthenorth / array_contains_sum.rb
Last active November 17, 2016 00:42
Determine if a contiguous set of positive integers in an array add up to n
class Array
def contains_sum?(n)
return false if n.nil?
low = 0
high = 0
sum = first
while sum != n && low < size
if sum < n || low == high
return false if high == size - 1
@travisofthenorth
travisofthenorth / matrix_binary_search.rb
Last active November 17, 2016 17:15
Matrix binary search
class Matrix
Coordinate = Struct.new(:x, :y)
def initialize(grid)
@grid = grid
end
def binary_search(n)
row = find_row(n, 0, grid.count - 1)
return nil unless row

Keybase proof

I hereby claim:

  • I am travisofthenorth on github.
  • I am travishunter (https://keybase.io/travishunter) on keybase.
  • I have a public key whose fingerprint is 3D8C 4969 767F 1A5F E4B9 714C B781 0CC4 B1C2 283F

To claim this, I am signing this object:

@travisofthenorth
travisofthenorth / dankify_specs.sh
Last active September 28, 2017 20:04
If you're using FactoryGirl, replace all occurrences of 'create' with 'build' and 'git add' the specs that still pass.
#!/bin/bash
find spec -iname "*spec.rb" -exec sed -i.bak 's/create/build/g' {} \;
find spec -iname "*spec.rb" -print0 | while IFS= read -r -d $'\0' spec; do
spring rspec $spec --fail-fast
if [[ $? -gt 0 ]]; then
echo "$spec did not pass"
else
echo "$spec passed!!!"
@travisofthenorth
travisofthenorth / jailed_console.rb
Last active June 3, 2016 23:44
Module to sandbox the rails console more
module JailedConsole
extend ActiveSupport::Concern
included do
require 'colorize'
def sandbox_console
require 'webmock'
require 'sidekiq/testing' if defined?(Sidekiq)
end