Skip to content

Instantly share code, notes, and snippets.

View tobiashm's full-sized avatar

Tobias H. Michaelsen tobiashm

View GitHub Profile
@tobiashm
tobiashm / missing-routes.sh
Created January 2, 2014 10:07
List missing Rails routes from production log
grep -e "(No route matches" log/production.log | awk '{print $(NF-1)," ",$NF}' | sort | uniq
@tobiashm
tobiashm / appcache.rake
Created January 2, 2014 08:53
Rake task to generate appcache manifest for Rails assets
namespace :appcache do
desc "Create manifest"
task :create do
sprockets_manifest = Rails.root.join("public/assets/manifest.yml")
abort "#{sprockets_manifest} not found" unless sprockets_manifest.exist?
prefix = Rails.configuration.assets.prefix
files = YAML.load(sprockets_manifest.open).map { |_, file| "#{prefix}/#{file}" }
Rails.root.join("public/manifest.appcache").open("w") do |out|
out << "CACHE MANIFEST\n\n"
out << "# " << `git rev-parse HEAD` << "\n"
@tobiashm
tobiashm / establish_connection.rb
Created September 20, 2013 11:06
Example of how to determine if a class method is defined on the current class or an ancestor. Notice that Method#owner returns the metaclass, so we can't just compare to the class itself.
base_classes_with_own_connection = ActiveRecord::Base.subclasses.find_all { |klass|
klass.method(:establish_connection).owner == (class << klass; self; end)
}
([ActiveRecord::Base] + base_classes_with_own_connection).each do |base|
base.establish_connection
end
@tobiashm
tobiashm / cphjs.svg
Created September 18, 2013 11:04
CPH JS logo
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@tobiashm
tobiashm / dsl.rb
Created September 17, 2013 17:11
A Ruby DSL builder helper. By using a delegator object we can isolate the DSL methods, making them only available inside the `build` block.
require "delegate"
module DSL
def self.build(klass, &block)
delegator_klass = Class.new(SimpleDelegator, &block)
klass.define_singleton_method(:build) do |*args, &block|
klass.new(*args).tap { |base| delegator_klass.new(base).instance_eval(&block) }
end
end
end
@tobiashm
tobiashm / dabblet.css
Created September 4, 2013 12:09
Wifi radar
@keyframes radar {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
#wifi {
width: 100px; height: 100px;
border: double 100px;
border-radius: 200px;
border-color: black transparent transparent;
animation: radar 3s linear infinite;
@tobiashm
tobiashm / Function.prototype.inherits_from.js
Created August 2, 2013 09:49
Helper function for prototypical inheritance
Function.prototype.inherits_from = function(parent) {
var Eigen = function() {};
Eigen.prototype = parent.prototype;
this.prototype = new Eigen();
this.prototype.constructor = this;
this.prototype.super = function() {
parent.apply(this, Array.prototype.slice.apply(arguments));
};
};
@tobiashm
tobiashm / Function.prototype.make.js
Created August 2, 2013 09:20
Helper function to ensure correct context in JavaScript constructor. Alternative to `function Foo(...) { if (!(this instanceof Foo)) return new Foo(...); ... }`
Function.prototype.make = function(self, args) {
if (self instanceof args.callee) return self;
var F = function() {};
F.prototype = args.callee.prototype;
var o = new F();
F.prototype.constructor.apply(o, Array.prototype.slice.apply(args));
return o;
};
function Foo(name) {
Gem::Specification.new do |spec|
spec.name = 'ping_response'
spec.version = '0.1.0'
spec.platform = Gem::Platform::RUBY
spec.author = 'Tobias H. Michaelsen'
spec.email = '[email protected]'
spec.summary = 'Simple wrapper for Rack apps to add /ping response handler'
spec.description = 'Will wrap a regular Rack app and add a handler for /ping which responds with `pong` in text/plain'
spec.license = 'MIT'
spec.files = ['ping_response.rb']
@tobiashm
tobiashm / dabblet.css
Created May 21, 2013 13:57
scroll shadow with images
/**
* scroll shadow with images
*/
.wrapper {
position: relative;
width: 200px;
height: 300px;
overflow-y: auto;
background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAAAUCAYAAAB7wJiVAAAEJGlDQ1BJQ0MgUHJvZmlsZQAAOBGFVd9v21QUPolvUqQWPyBYR4eKxa9VU1u5GxqtxgZJk6XtShal6dgqJOQ6N4mpGwfb6baqT3uBNwb8AUDZAw9IPCENBmJ72fbAtElThyqqSUh76MQPISbtBVXhu3ZiJ1PEXPX6yznfOec7517bRD1fabWaGVWIlquunc8klZOnFpSeTYrSs9RLA9Sr6U4tkcvNEi7BFffO6+EdigjL7ZHu/k72I796i9zRiSJPwG4VHX0Z+AxRzNRrtksUvwf7+Gm3BtzzHPDTNgQCqwKXfZwSeNHHJz1OIT8JjtAq6xWtCLwGPLzYZi+3YV8DGMiT4VVuG7oiZpGzrZJhcs/hL49xtzH/Dy6bdfTsXYNY+5yluWO4D4neK/ZUvok/17X0HPBLsF+vuUlhfwX4j/rSfAJ4H1H0qZJ9dN7nR19frRTeBt4Fe9FwpwtN+2p1MXscGLHR9SXrmMgjONd1ZxKzpBeA71b4tNhj6JGoyFNp4GHgwUp9qplfmnFW5oTdy7NamcwCI49kv6fN5IAHgD+0rbyoBc3SOjczohbyS1drbq6pQdqumllRC/0ymTtej8gpbbuVwpQfyw66dqEZyxZKxtHpJn+tZnpnEdrYBbueF9qQn93S7HQGGHnYP7w6L+YGHNtd1FJitqPAR+hERCNOFi1i1alKO6RQnjKUxL1GNjwlMsiEhcPLYTEiT9ISbN15OY/jx4SMshe9LaJRpTvHr3C/ybFYP1PZAfwfYrPsMBtnE6SwN9ib7AhLwTrBDgUKcm06FSrTfSj187x