Skip to content

Instantly share code, notes, and snippets.

View bradgessler's full-sized avatar

Brad Gessler bradgessler

View GitHub Profile
@bradgessler
bradgessler / component_helper.rb
Last active April 10, 2023 15:01
Sane way to call a ViewComponent
# Copy this file to ./app/views/component_helper.rb if you want to unlock ViewComponent rendering super powers.
module ComponentHelper
# Instead of the awkward `render FooComponent.new(title: "foo")` calls in Rails templates,
# use a method like `foo_component title: "foo"`.
def method_missing(method_name, *args, **kwargs, &block)
if method_name.end_with? "_component"
component_class = method_name.to_s.classify.constantize
component = component_class.new(*args, **kwargs)
component.render_in(self, &block)
else
@bradgessler
bradgessler / code.rb
Created May 19, 2021 00:13
Redcarpet integration with Rails view helpers
module Redcarpet
module Rails
DEFAULT_BASE_RENDERER = Redcarpet::Render::HTML
DEFAULT_OPTIONS = Hash.new
class << self
def stack
@stack ||= Stack.new
end
end
@bradgessler
bradgessler / start.sh
Created February 23, 2021 07:12
Sitepress pre-release
gem install sitepress --pre
sitepress new ./site
cd ./site
sitepress server
@bradgessler
bradgessler / csv_definition.rb
Created February 13, 2020 08:57
CSVDefinition
require "csv"
class CSVDefinition
attr_reader :definition
def initialize
@definition ||= Hash.new
end
def row(name, &block)
@bradgessler
bradgessler / docker.sh
Created November 15, 2016 20:45
First CLI commands
$ docker run ubuntu /bin/echo 'Hello world'
Hello world
@bradgessler
bradgessler / initializer.rb
Last active March 10, 2017 14:17
MIddleware for making legacy versions of Internet Explorer (anything below 11) work with rails. I release this code under MIT license.
# ./config/initializers/rewrite_ie_header.rb
# Don't forget to include the middleware file! If you have a ./lib/middleware folder in rails
# a `require './lib/middleware/rewrite_ie_header.rb'` should do the trick
#
# IE has a really stupid accept header, so here we scope its domain of mime-types that it can negotiate, otherwise we'll
# end up with weird bugs where if you hit /mcp/:id, IE will download a .ppt file instead of the .html content.
#
# Issue documented in more detail at https://github.com/rails/rails/issues/9940.
Rails.application.config.middleware.use RewriteIEAcceptHeader do |ie_mime_types|
@bradgessler
bradgessler / bundle
Created March 18, 2015 04:23
Fast bundler
#!/usr/bin/env bash
export IMAGE_NAME=my_app
CONTAINER_ID=$(docker run -d $IMAGE_NAME bundle $@)
docker logs -f $CONTAINER_ID
docker commit $CONTAINER_ID $IMAGE_NAME
module PunditExampleGroup
extend ::RSpec::Matchers::DSL
matcher :permit do |action|
match do |policy|
policy.public_send("#{action}?")
end
failure_message do |policy|
"#{policy.class} does not permit #{action} on #{policy.record} for #{policy.user.inspect}."
@bradgessler
bradgessler / license.rb
Created July 2, 2014 06:07
Print a list of licenses for gems in a bundle.
require 'bundler'
require 'yaml'
def format(hash)
Hash[hash.keys.map(&:to_s).zip(hash.values)].to_yaml
end
Bundler.load.specs.each do |spec|
puts format({
name: spec.name,
@bradgessler
bradgessler / list.hon
Last active December 29, 2015 00:39
JSON is too verbose for hypermedia APIs. HON (Hypermedia Object Notation) preserves the readability and implicit data structure of JSON while with the addition of line attributes for URL and pagination metadata. From http://bradgessler.com/articles/hon/
{
name: "Todo List",
items(href: '/lists/1203/items', next_href: "/lists/1203/items?page=2"): [
item(href: '/items/1'): {
description: "Pick up pizza",
status: "completed"
},
item(href: '/items/12'): {
description: "Eat pizza",
status: "non_started"