Skip to content

Instantly share code, notes, and snippets.

View MacksMind's full-sized avatar

Mack Earnhardt MacksMind

View GitHub Profile
@MacksMind
MacksMind / s3_static_deploy.yml
Last active July 22, 2020 11:16
CloudFormation template to deploy a static website to S3. Details at https://www.macksmind.io/aws/2020/07/20/deploy-static-website-to-amazon-s3.html
AWSTemplateFormatVersion: 2010-09-09
Parameters:
GitHubOwner:
Type: String
Description: GitHub repo owner
MinLength: 1
GitHubRepo:
Type: String
Description: GitHub repo name
MinLength: 1
@MacksMind
MacksMind / gist:2be0f9829de72dd9ca76
Created March 2, 2016 21:13
Run rspec 5 specs at a time. Helps to debug where things are hanging up.
find spec -name '*_spec.rb' | xargs -tn 5 bundle exec ruby -I.:spec -r 'rspec/autorun' -e 'ARGV.each{|f| require f}'
# Takes an array (enumerable?) as input
# Return an array containing elements occurring multiple times in input
def report_duplicate_elements(list)
occurs_multiple_times = []
already_seen = []
list.each do |element|
if already_seen.include?(element)
occurs_multiple_times << element
else
@MacksMind
MacksMind / attr_tree.rb
Created March 26, 2013 13:52
Sometimes I've needed a 'deep compare' for ActiveRecord objects. This method returns a Hash containing the objects attributes plus the attributes from the first layer of relationships. After using #except to exclude attributes that should change, I can assert_equal to compare.
class ActiveSupport::TestCase
def attr_tree(obj)
tree = obj.attributes.except("id")
obj.class.reflections.each do |refl_name,refl|
refl_key = refl_name.to_s
case refl.macro
when :belongs_to, :has_one
tree[refl_key] = (o = obj.send(refl_name)) && o.attributes
when :has_many, :has_and_belongs_to_many
@MacksMind
MacksMind / mvplugin.sh
Created March 26, 2013 13:43
Shell script to help move Rails 2.3 style plugins to the approved location.
#!/bin/bash
for i in "$@"
do
git mv vendor/plugins/$i/lib lib/$i
git mv vendor/plugins/$i/init.rb config/initializers/$i.rb
git rm -r vendor/plugins/$i || rmdir vendor/plugins/$i
done
@MacksMind
MacksMind / test.sh
Created March 26, 2013 10:03
Shell script to run multiple test files. This version runs 5 at a time. Change the `-tn 5` to `-t` to run everything at once.
find test/* -type d -maxdepth 2 | xargs -J {} find {} -name '*_test.rb' | xargs -tn 5 bundle exec ruby -I.:test -e "ARGV.each{|f| require f}"
@MacksMind
MacksMind / warn_on_escaped_html.rb
Created March 16, 2013 23:51
I'm in the middle of upgrading a Rails app from 2.3 to 3.2. There are many places where html strings are constructed and rendered. This monkey-patch has been a big help in finding all the places I need to use raw or html_safe.
module ActionView
class Template
def render_with_warn_on_escaped_html(*args,&block)
output = render_without_warn_on_escaped_html(*args,&block)
unless Rails.env.production? || args[0].request.instance_variables.include?(:@skip_escaped_html_check)
if output =~ /^.*&lt;.*$/
warn "WARNING: Escaped HTML in #{ Rails.backtrace_cleaner.clean(caller).first }: #{Regexp.last_match}"
args[0].request.instance_variable_set(:@skip_escaped_html_check,true)
end
end