Skip to content

Instantly share code, notes, and snippets.

View johanhalse's full-sized avatar

Johan Halse johanhalse

View GitHub Profile
@johanhalse
johanhalse / rubocop_autofix.py
Created August 31, 2023 07:59
Rubocop autofix command for Sublime Text
import sublime
import sublime_plugin
import subprocess
import shlex
class RubocopAutofixCommand(sublime_plugin.TextCommand):
def run(self, edit):
full_text = self.view.substr(sublime.Region(0, self.view.size()))
try:
@johanhalse
johanhalse / component_switcher.py
Created August 31, 2023 06:20
Sublime text command for quickly switching between a Rails view_component view and component
import sublime
import sublime_plugin
import os
class ComponentSwitcherCommand(sublime_plugin.TextCommand):
def open_ext(self, filename, ext):
file_to_open = f"{filename}{ext}"
if os.path.exists(file_to_open):
self.view.window().open_file(file_to_open)
else:
@johanhalse
johanhalse / gist:36e5a13e1ac3ad56e195
Last active August 29, 2015 14:22
Validate a Swedish personnummer with Luhn's Algorithm
// Expects a Swedish personnummer formatted as YYYYMMDDXXXX
var isLuhn = function(pnr) {
var multiplier = 1;
var nums = pnr.substr(2).split('').map(function(numStr) {
multiplier = 1 + (multiplier % 2);
var num = parseInt(numStr) * multiplier;
return (num < 10) ? num : num - 9;
});
return nums.reduce(function(prev, curr) { return curr + prev; }) % 10 === 0;
### Keybase proof
I hereby claim:
* I am johanhalse on github.
* I am johanhalse (https://keybase.io/johanhalse) on keybase.
* I have a public key whose fingerprint is 2B2C EC34 E155 51FF A54F F3FB 6BB5 CC80 F804 D530
To claim this, I am signing this object:
@johanhalse
johanhalse / moment_shim.js
Created May 4, 2015 14:18
Small shim for formatting Pikaday dates as YYYY-MM-DD without moment.js
// Would be ridiculous to pull in the entire moment.js library for this
window.moment = function(dateString) {
var date = new Date(dateString);
var format = function() {
return date.getFullYear() + '-' +
('0' + (date.getMonth() + 1)).slice(-2) + '-' +
('0' + date.getDate()).slice(-2);
};
@johanhalse
johanhalse / gist:9049840
Created February 17, 2014 12:39
JavaScript concatenation and uglification with Harp server
// Wrapper for Harp web server, to include JS concat/compile step. Put your stuff
// in /harp subdirectory, npm install your dependencies, run, enjoy.
// Live updating of the concatenated JS file left as an exercise for the reader :)
var fs = require('fs');
var path = require('path');
var harp = require('harp');
var UglifyJS = require('uglify-js');
var files = [];
files.push(__dirname + '/harp/js/file0.js');
@johanhalse
johanhalse / gist:6351304
Created August 27, 2013 09:01
Working searchkick query from rails console
2.0.0 :001 > Article.search 'om'
=> #<Searchkick::Results:0x00000006cf3a68 @response={"took"=>68, "timed_out"=>false,
"_shards"=>{"total"=>5, "successful"=>5, "failed"=>0}, "hits"=>{"total"=>4,
"max_score"=>0.035879318, "hits"=>[{"_index"=>"articles_development_20130827084418630",
"_type"=>"article", "_id"=>"1", "_score"=>0.035879318},
{"_index"=>"articles_development_20130827084418630", "_type"=>"article", "_id"=>"2",
"_score"=>0.032162987}, {"_index"=>"articles_development_20130827084418630",
"_type"=>"article", "_id"=>"3", "_score"=>0.026802491},
{"_index"=>"articles_development_20130827084418630", "_type"=>"article", "_id"=>"4",
"_score"=>0.020341659}]}}, @options={:load=>true, :payload=>{:query=>{:dis_max=>{
@johanhalse
johanhalse / gist:6351288
Created August 27, 2013 08:59
Searchkick error message
Tire::Search::SearchRequestFailed (400 : {
"error":"SearchPhaseExecutionException[Failed to execute phase [query],
all shards failed; shardFailures {
[8LEbz2d7RS6EGrhSp8sNuA][articles_production][4]:
SearchParseException[[articles_production][4]:
from[-1],size[-1]: Parse Failure [Failed to parse source [{
"query":{
"dis_max":{
"queries":[{
"multi_match":{