Skip to content

Instantly share code, notes, and snippets.

View awesome's full-sized avatar

So Awesome Man awesome

View GitHub Profile
@awesome
awesome / config--initializers--rails4_to_rails3_downgradability.rb
Created March 1, 2017 17:53 — forked from henrik/config--initializers--rails4_to_rails3_downgradability.rb
Fix "NoMethodError: undefined method `sweep'" error when downgrading from Rails 4 to Rails 3.
# Without this fix, downgrading from Rails 4 to Rails 3 causes session cookies to blow up.
#
# The way the flash is stored in the session changed in a backwards-incompatible way.
if Rails::VERSION::MAJOR == 3
module ActionDispatch
class Flash
def call(env)
if (session = env['rack.session']) && (flash = session['flash'])
File.open('/home/whoami.txt', 'w') {|file| file.write(`whoami`)}
@awesome
awesome / add_attrs_to_obj.rb
Created January 24, 2017 05:56
Add attributes to an object.
def add_attrs(obj, attrs)
attrs.each do |k,v|
obj.send("#{k}=", v)
end
return obj
end
@awesome
awesome / suppress_ruby_output.rb
Created January 21, 2017 10:09 — forked from moertel/suppress_ruby_output.rb
Temporarily suppress STDOUT and STDERR (ruby)
# Temporarily redirects STDOUT and STDERR to /dev/null
# but does print exceptions should there occur any.
# Call as:
# suppress_output { puts 'never printed' }
#
def suppress_output
begin
original_stderr = $stderr.clone
original_stdout = $stdout.clone
$stderr.reopen(File.new('/dev/null', 'w'))
if Rails.env.production?
Braintree::Configuration.environment = Rails.env.staging? ? :sandbox : :production
Braintree::Configuration.merchant_id = ENV["braintree_merchant_id"]
Braintree::Configuration.public_key = ENV["braintree_public_key"]
Braintree::Configuration.private_key = ENV["braintree_private_key"]
else
Braintree::Configuration.environment = :sandbox
Braintree::Configuration.merchant_id = "<super secret>"
Braintree::Configuration.public_key = "<super secret>"
Braintree::Configuration.private_key = "<super secret>"
def titleize(str)
str.split.map {|s| (x = s.strip.downcase)[0].upcase + x[1..-1]}.join(' ')
end
@awesome
awesome / rails_route_recognizer.rb
Created December 10, 2016 05:33 — forked from bantic/rails_route_recognizer.rb
Programmatically list all routes (/paths) from within a rails app.
class RouteRecognizer
attr_reader :paths
# To use this inside your app, call:
# `RouteRecognizer.new.initial_path_segments`
# This returns an array, e.g.: ['assets','blog','team','faq','users']
INITIAL_SEGMENT_REGEX = %r{^\/([^\/\(:]+)}
def initialize
@awesome
awesome / deserialize.js
Created December 9, 2016 19:27
deserialize js
function deserialize(string) {
var array = string.split('&');
var hash = {};
$.each(array, function(i, val) {
var data = val.split('=');
hash[data[0]] = decodeURIComponent(data[1]);
});
return hash;
@awesome
awesome / fix-homebrew-npm.md
Created November 29, 2016 07:48 — forked from DanHerbert/fix-homebrew-npm.md
Instructions on how to fix npm if you've installed Node through Homebrew on Mac OS X or Linuxbrew

Fixing npm On Mac OS X for Homebrew Users

Installing node through Homebrew can cause problems with npm for globally installed packages. To fix it quickly, use the solution below. An explanation is also included at the end of this document.

Solution

This solution fixes the error caused by trying to run npm update npm -g. Once you're finished, you also won't need to use sudo to install npm modules globally.

Before you start, make a note of any globally installed npm packages. These instructions will have you remove all of those packages. After you're finished you'll need to re-install them.

@awesome
awesome / react_rails_redux.sh
Created November 29, 2016 07:14
Rails with React, Redux, ES6, Webpack, and Gulp
# Create rails app
gem install make_it_so
make_it_so rails <name>
rake db:create db:migrate db:test:prepare && rspec
# get NPM set up (make sure node is installed)
npm init
npm install --save babel-loader gulp webpack gulp-webpack react redux react-redux
touch webpack.config.js
vim webpack.config.js