Skip to content

Instantly share code, notes, and snippets.

Tyler Hunt tylerhunt

Block or report user

Report or block tylerhunt

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
@tylerhunt
tylerhunt / visualize_specs.rb
Last active Feb 12, 2018
Visualize the state of an RSpec suite using the cached example status.
View visualize_specs.rb
require 'rspec/core'
css = %(
<style>
html { font-size: 50%; }
.unknown, .passed, .pending, .failed { display: inline-block; height: 1rem; width: 1rem; }
.unknown { background-color: #999; }
.pending { background-color: #FC0; }
.passed { background-color: #3F3; }
.failed { background-color: #F33; }
@tylerhunt
tylerhunt / Rakefile
Last active Dec 7, 2017
A Rakefile for trying out Citus using Docker
View Rakefile
ENV['COMPOSE_PROJECT_NAME'] ||= 'citus'
ENV['MASTER_EXTERNAL_PORT'] ||= '5433'
file 'docker-compose.yml' do
`curl -L https://raw.githubusercontent.com/citusdata/docker/master/docker-compose.yml > docker-compose.yml`
end
namespace :citus do
desc 'Start the Citus cluster'
task :up, [:workers] => 'docker-compose.yml' do |_task, args|
@tylerhunt
tylerhunt / global_id.rb
Created Aug 7, 2015
RSpec shared context to allow testing of Active Job objects with GlobalID-compatible doubles.
View global_id.rb
RSpec.shared_context 'Global ID', :global_id do
def global_id_instance_double(doubled_class, stubs={})
instance_double(doubled_class, stubs)
.extend(GlobalID::Identification)
.tap { |double|
unless double.respond_to?(:id)
allow(double).to receive(:id).and_return(double.object_id)
end
}
end
@tylerhunt
tylerhunt / rendering_helper.rb
Created Mar 20, 2015
Override Rails' #render helper to fix an issue with rendering partials based on an object within a namespace.
View rendering_helper.rb
module RenderingHelper
# Override Rails' #render helper to fix an issue with it not honoring objects
# with #to_partial_path definitions that return absolute paths, which is
# problematic when rendering partials within a namespaced controller.
def render(options={}, locals={}, &block)
return super unless options.respond_to?(:to_partial_path)
object = options
path = object.to_partial_path
@tylerhunt
tylerhunt / active_record_decorator.rb
Created Mar 19, 2015
An abstract decorator that allows decorated Active Record records to be assigned to associations without raising an ActiveRecord::AssociationTypeMismatch error.
View active_record_decorator.rb
require 'delegate'
# An abstract decorator useful for decorating Active Record objects.
class ActiveRecordDecorator < SimpleDelegator
# A proxy for the decorator class to allow the delegation of certain class
# methods to the decorated object's class.
class ClassProxy < SimpleDelegator
def initialize(decorator_class, decorated_class)
super decorator_class
self.decorated_class = decorated_class
@tylerhunt
tylerhunt / dependencies.rb
Created Feb 28, 2015
A macro to allow object dependencies to be specified at the class level with a block defining a default value. Based on attr_defaultable.
View dependencies.rb
module Dependencies
# Ensure dependencies from the superclass are inherited by the subclass.
def inherited(subclass)
if superclass.respond_to?(:dependencies)
subclass.dependencies.concat dependencies
end
super
end
@tylerhunt
tylerhunt / my_rack_app.rb
Created Sep 29, 2014
Rack app object with middleware
View my_rack_app.rb
class MyRackApp
def initialize(app)
stack = Rack::Buidler
# stack.use …
stack.run app
@app = stack.to_app
end
def call(env)
@tylerhunt
tylerhunt / README.md
Created Sep 10, 2014
Static error pages from dynamically generated assets.
View README.md

The guide Asset Pipeline Error Pages now no longer works, since non-digest assets aren't generated in newer Rails apps. To fix this, I've replaced the config/initializers/exceptions.rb initializer that swaps out the middleware with the Rake task here, which will be run after Rails assets:precompile task.

@tylerhunt
tylerhunt / void.rb
Created Aug 15, 2014
Void macro for Ruby
View void.rb
module Void
def void(method_name)
method = instance_method(method_name)
remove_method method_name
define_method(method_name) do |*args, &block|
method.bind(self).call(*args, &block).tap do |return_value|
raise TypeError, 'unexpected return value' unless return_value.nil?
end
end
@tylerhunt
tylerhunt / validations.rb
Last active Aug 29, 2015
Using ActiveModel::Validations standalone.
View validations.rb
# What I should have to do:
require 'active_model/validations'
# What I actually have to do:
require 'active_support/callbacks'
require 'active_support/concern'
require 'active_support/core_ext/module/delegation'
require 'active_model/callbacks'
require 'active_model/errors'
require 'active_model/naming'
You can’t perform that action at this time.