Skip to content

Instantly share code, notes, and snippets.

View eliotsykes's full-sized avatar

Eliot Sykes eliotsykes

View GitHub Profile
@eliotsykes
eliotsykes / thinRestart.sh
Created June 17, 2012 11:00
Robust Thin restart|start|stop script for HTTP and SSL in development
#! /bin/bash
# restart will start thin without error even when it is not already running.
echo "Usage: ./script/thin start|stop|restart"
# Log and pid file names will include port number before the .log/.pid extension.
bundle exec thin --pid tmp/pids/thin.pid --servers 1 --daemonize --log log/thin.log --port 3000 $1
bundle exec thin --pid tmp/pids/thin.pid --servers 1 --daemonize --log log/thin.log --port 3001 --ssl $1
@eliotsykes
eliotsykes / HOMEDIR-.ssh-config
Created June 27, 2012 15:26
SSH connection sharing in multiple terminals
# Put this in your local ~/.ssh/config to be prompted for password only once for multiple terminals
ControlMaster auto
ControlPath /tmp/ssh_mux_%h_%p_%r
@eliotsykes
eliotsykes / actions.js
Created July 13, 2012 16:24
selenium selenese helper functions
// Perform HTTP POST to the given URL using selenium. Put this in actions.js.
// Works with selenese: | post | http://url-to-http-post-to.com/ |
Selenium.prototype.doPost = function(url) {
var form = this.getCurrentWindow().document.createElement('form');
form.id = 'formAddedBySeleniumDoPost';
form.method = 'POST';
form.action = url;
var body = this.browserbot.findElement("css=body");
if (typeof body == "undefined") {
throw new SeleniumError("No body element to append form element to for performing POST to url '" + url + "'");
@eliotsykes
eliotsykes / code_example.md
Last active October 24, 2015 14:52
Markdown code block example using backticks

Triple backticks are used to open and close code blocks. The triple backticks need to be on their own line:

puts "hello world"

And here is another, separate code block. Triple backticks open and close it:

puts "line 1"
@eliotsykes
eliotsykes / api_controller.rb
Last active October 30, 2015 18:40
Avoid cookies in Rails 4.2 API Controller
class Api::ApiController < ActionController::Base
protect_from_forgery with: :null_session
def self.disable_turbolinks_cookies
skip_before_action :set_request_method_cookie
end
disable_turbolinks_cookies
end
@eliotsykes
eliotsykes / base_scopes_sample.diff
Last active December 15, 2015 10:24
Rails BaseScopes and subclasses for tidying up excessive SQL scopes in models
+++ b/app/scopes/base_scopes.rb
+class BaseScopes < SimpleDelegator
+
+ def initialize
+ super self.class.relation
+ end
+
+ def self.method_missing(method_name, *args)
+ method = self.new.method(method_name)
+ method.to_proc
@eliotsykes
eliotsykes / gmaps4rails_example_1.html.erb
Last active December 19, 2015 12:38
Getting started with gmaps4rails - a simple map with a single point centred on London, UK
<%# Shows a map centred on London, using the default marker/pin image. %>
<%= gmaps4rails([{:lat => 51.51, :lng => -0.12}].to_json) %>
<%= yield :scripts %><%# Usually yield :scripts goes just before the closing body tag %>
@eliotsykes
eliotsykes / asset_server_middleware.rb
Last active December 20, 2015 01:29
***** Checkout the rack-zippy gem, I recommend it over this gist, find it at https://github.com/eliotsykes/rack-zippy ***** An asset server for Rails 3.2.x that serves asset pipeline precompiled assets to clients, including those elusive gzipped (.gz) assets. Sadly Rails' own ActionDispatch::Static middleware does not take care of serving gzippe…
# 1. Add rack-rewrite to your Gemfile and run 'bundle install':
# gem 'rack-rewrite'
#
# 2. Create a file with the contents below in config/initializers/asset_server_middleware.rb
#
# 3. Rename 'YourApp' below
#
# 4. In config/environments/production.rb and config/environments/test.rb, set:
# config.serve_static_assets = true
# config.assets.compile = false
@eliotsykes
eliotsykes / audit.rb
Created December 30, 2015 07:00
Add Custom Scopes to Audited Gem
# app/models/audit.rb
Audit = Audited.audit_class
class Audit
scope :foo, -> { where("created_at > 0") }
scope :bar, -> { where("updated_at > 0") }
end
# Using scopes example:
# Audit.foo.bar
@eliotsykes
eliotsykes / deploy
Last active January 4, 2016 20:50
bin/deploy for rails+heroku app
#!/bin/sh
# Create this file in the bin/ directory of your
# Rails app.
# Run this script to deploy the app to Heroku.
set -e
# Push changes to Heroku