Skip to content

Instantly share code, notes, and snippets.

Josh Cheek JoshCheek

Block or report user

Report or block JoshCheek

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
@JoshCheek
JoshCheek / config.ru
Created Sep 13, 2019
Example of making a Rails inspired minimalist web framework for Rack
View config.ru
# ru means "rackup"
class Object
private def erb(text)
ERB.new(text).result(binding)
end
end
class MyRails
attr_accessor :routes
@JoshCheek
JoshCheek / setting_a_hidden_field_with_capybara.rb
Created Nov 1, 2012
Setting a hidden field with Capybara
View setting_a_hidden_field_with_capybara.rb
require 'sinatra/base'
class ExampleApp < Sinatra::Base
get '/get_form' do
'<html>
<body>
<form action="/post_form" method="post" accept-charset="utf-8">
<input type="hidden" name="hidden_face" value=":(" id="hidden_face">
<p><input type="submit" value="Submit!"></p>
</form>
@JoshCheek
JoshCheek / rack_test_example.rb
Created Jul 21, 2014
example of how to use rack test with minitest
View rack_test_example.rb
require 'sinatra/base'
require 'minitest/autorun'
require 'minitest/spec'
require "rack/test"
require 'nokogiri'
class MyApp < Sinatra::Base
enable :inline_templates
get '/users/:id' do
@JoshCheek
JoshCheek / hooks.rb
Created Aug 24, 2012
Creates before/after hooks for methods
View hooks.rb
# a response to www.ruby-forum.com/topic/4405076
module Hook
def before(meth_name, &callback)
add_hook :before, meth_name, &callback
end
def after(meth_name, &callback)
add_hook :after, meth_name, &callback
end
@JoshCheek
JoshCheek / recentness.rb
Created Aug 16, 2019
List db tables by the recentness of their `created_at` column.
View recentness.rb
# example: ruby recentness.rb myapp_development | column -t
dbname = ARGV.fetch(0)
require 'pg'
puts "schema\ttable\tlast-insert"
PG.connect(dbname: dbname).exec(<<~SQL).each { |row| puts row.values.join "\t" }
create function pg_temp.recentness(table_schema text, table_name text, out last_insert timestamp)
as $fn$ begin
execute format('select max(created_at) from %I.%I', table_schema, table_name)
into last_insert;
end $fn$ language plpgsql;
@JoshCheek
JoshCheek / gist:7cc84715dc7ab6deef40adf9982bf755
Created Aug 12, 2019
Using a refinement to scope a monkey patch
View gist:7cc84715dc7ab6deef40adf9982bf755
# code we don't own
class User
def initialize(first_name, last_name)
@first_name, @last_name = first_name, last_name
end
def full_name
@first_name + " " + @last_name
end
end
@JoshCheek
JoshCheek / option_parser_example.rb
Created Feb 8, 2015
Why I will not use Ruby's OptionParser
View option_parser_example.rb
# Have to hijack the global environment, b/c that's how invasive it is!
def self.wtf
to_return = {exception: nil, stdout: nil, stderr: nil}
require 'stringio'
$stdout = StringIO.new
$stderr = StringIO.new
to_return[:returned] = yield
rescue Exception
to_return[:exception] = $!
ensure
@JoshCheek
JoshCheek / feature_testing.md
Last active Aug 1, 2019
Thoughts on creating an acceptance/feature test suite for a website with a React frontend and a few services on the backend
View feature_testing.md

Make fewer tests that are larger, because the tests are going to be extremely expensive and fragile. Make assertions that are very permissive, eg assert that you see a success message, but not what the message says. Because there's a million ways for these tests to fail. Someone changes some wording in a message and tests fail. Someone changes the DOM structure and the tests fail. Someone changes a classname and the tests fail. Some async operation takes longer than expected or resolves in some unexpected order, and the tests fail. But in all those situations, the thing you are testing is still correct, it's just that the way the test checks it is fragile, and the thing you are testing is volatile. So you want the tests to only assert the most important things, and the art of it is figuring out how permissive you want to be. Eg you want to be assertive enough that they don't accidentally pass when they actually should fail, but still permissive enough that they can tolerate irrelevant volatility.

For tooling

@JoshCheek
JoshCheek / map_ar_enum_to_pg_enum.rb
Created Jul 31, 2019
ActiveRecord enum / PostgreSQL enum
View map_ar_enum_to_pg_enum.rb
# require 'pg'; PG.connect(dbname: 'postgres').exec('create database josh_testing') # <-- uncomment to create the db (only needs to run once)
# ===== Setup ActiveRecord =====
require 'active_record'
require 'logger'
ActiveRecord::Base.establish_connection adapter: 'postgresql', database: 'josh_testing'
ActiveRecord::Base.logger = Logger.new $stdout
ActiveSupport::LogSubscriber.colorize_logging = false
# ===== Migration =====
@JoshCheek
JoshCheek / pg_cursor_example.rb
Created Oct 6, 2017
PostgreSQL cursor example
View pg_cursor_example.rb
require 'pg'
db = PG.connect dbname: 'postgres'
db.exec("DROP DATABASE IF EXISTS just_fkn_around;")
db.exec("CREATE DATABASE just_fkn_around;")
db = PG.connect dbname: 'just_fkn_around'
define_method(:sql) { |sql| db.exec(sql).to_a }
sql <<-SQL
-- some data to query
You can’t perform that action at this time.