Skip to content

Instantly share code, notes, and snippets.

View tobiashm's full-sized avatar

Tobias H. Michaelsen tobiashm

View GitHub Profile
@tobiashm
tobiashm / unindent.rb
Last active February 2, 2016 13:10
Un-indent heredoc
# From Ruby Tapas ep. 249
def unindent(s)
s.gsub(/^#{s.scan(/^[ \t]+(?=\S)/).min}/, "")
end
puts unindent(<<-'EOS')
This is an indented document in Markdown.
It contains blank lines and a code block:
@tobiashm
tobiashm / wrap-jquery-ajax-promise.js
Created November 4, 2014 11:51
Wrap jQuery Ajax in real Promise
(function() {
function AjaxError(jqXHR, textStatus, errorThrown) {
this.name = 'AjaxError';
this.message = textStatus;
this.jqXHR = jqXHR;
this.errorThrown = errorThrown;
}
AjaxError.prototype = new Error();
AjaxError.prototype.constructor = AjaxError;
@tobiashm
tobiashm / pre-commit
Created October 8, 2014 07:14
Run RuboCop and NPM check on `git commit`
#!/bin/bash
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"
if [ -f ".rubocop.yml" ]; then
changed_ruby_files=$(git diff --cached --name-only --diff-filter=ACM | grep -e '\.rb$' | grep -v 'db/schema')
if [ "$changed_ruby_files" != "" ]; then
cd . # Cause RVM to change Ruby version to the one specified in the Gemfile or .ruby-version
bundle exec rubocop -n $changed_ruby_files || exit 1
fi
fi
@tobiashm
tobiashm / node_modules.rb
Last active October 26, 2023 22:26
Add node modules to Rails assets paths
# Allow Rails to use assets from Node packages
package_json = Rails.root.join("package.json")
Rails.configuration.watchable_files << package_json
Rails.configuration.to_prepare do
node_config = JSON.parse(package_json.read)
paths = Rails.configuration.assets.paths
node_modules = Rails.root.join("node_modules")
paths.reject! { |p| p.to_s.start_with?(node_modules.to_s) }
node_config["dependencies"].keys.each do |node_module|
module_dir = node_modules.join(node_module)
@tobiashm
tobiashm / .editorconfig
Last active August 29, 2015 14:06
Rails project setup basics
# http://EditorConfig.org
root = true
[*]
charset = utf-8
indent_size = 2
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true
@tobiashm
tobiashm / git-amend-previous.sh
Created June 4, 2014 09:43
Amend previous git commit
git rev-parse HEAD > latest
git reset --hard HEAD^
# make changes
git add -p
git commit --amend
git cherry-pick $(cat latest)
rm latest
@tobiashm
tobiashm / jquery-ajax-promise.js
Last active October 23, 2018 12:03
Wrap jQuery AJAX in ES6 Promise
function AjaxError(jqXHR, textStatus, errorThrown) {
this.name = "AjaxError";
this.message = textStatus;
this.jqXHR = jqXHR;
this.errorThrown = errorThrown;
}
AjaxError.prototype = new Error();
AjaxError.prototype.constructor = AjaxError;
(function($) {
@tobiashm
tobiashm / plsql-threadsafe.rb
Created March 7, 2014 08:56
Thread safety for ruby-plsql
require "ruby-plsql"
require "thread"
module PLSQL
module SynchronizedProcedure
def ensure_tmp_tables_created(overload)
@@semaphore ||= Mutex.new
@@semaphore.synchronize { super }
end
end
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@tobiashm
tobiashm / sass_svg_gradients.rb
Created February 7, 2014 14:40
Sass extension to create inline SVG gradients. Useful as a fallback for CSS gradients in IE.
require "base64"
require "rack"
module Sass::Script::Functions
def radial_gradient_image_data_url(color = Sass::Script::Color.new([0, 0, 0]), height = Sass::Script::Number.new(5))
assert_type height, :Number
svg_data_url(<<-SVG)
<svg xmlns="http://www.w3.org/2000/svg">
<defs>
<radialGradient id="gradient">