Skip to content

Instantly share code, notes, and snippets.

View buren's full-sized avatar

Jacob Burenstam Linder buren

View GitHub Profile
@buren
buren / i18n_generator.rb
Created July 10, 2015 18:53
Generate translations for activerecord models and columns
class I18nGenerator
attr_reader :models, :lang_code
def initialize(lang_code: :en)
ignored_tables = %w(schema_migrations)
@models = ActiveRecord::Base.connection.tables - ignored_tables
@models.map! { |model| model.singularize.camelize.constantize }
@lang_code = lang_code.to_s
end
@buren
buren / active_table_view.rb
Created July 11, 2015 00:44
ActiveTableView: Render "tables structure" for ActiveRecord objects
class ActiveTableView
def initialize(opts)
@col_names = opts.fetch(:cols)
@records = opts.fetch(:records)
@model_name = rows.table_name.singularize
end
def headers
@col_names.map { |name| t_col(name) }
end
@buren
buren / repeater.js
Created August 23, 2015 01:12
Dead simple JS repeater
/*
* Repeater.js
*/
(function (window) {
'use strict';
function Repeater(callback, timeout) {
var self = this;
DEADLINE = Time.new(2015, 9, 21, 13, 0, 0).freeze
module Audience;end
module Audience::Talk;end
String.class_eval { define_method(:last?) { false } }
NilClass.class_eval { define_method(:last?) { true } }
define_method(:wait) { puts 'Still not ready..';sleep 3 }
define_method(:any_questions?) { puts 'Any questions?' }
define_method(:feedback) { puts 'Feedback?' }
class Audience::Presenter < Struct.new(:name)
define_method(:ready?) { Time.now >= DEADLINE }
@buren
buren / minispec.js
Last active October 19, 2015 04:21
Minimal JS test DSL
function describe(label, testFunction) {
return new test(label, testFunction);
}
function test(label, testFunction) {
var self = this;
self.testLabel = label;
self.itLabel;
var label = function() { return self.testLabel + ' ' + self.itLabel; };
var passed = function() { console.log(label(), true); };
@buren
buren / minispec.rb
Created October 19, 2015 04:24
rspec-like specs, using ruby procs
def describe(label)
fails = []
it = ->(it_label, &block) {
assert = ->(expected, result) {
passed = ->() { print '.' }
failed = ->() {
print 'F'
fails << "#{label} #{it_label}, expected '#{expected}', but was '#{result}'."
}
expected == result ? passed.() : failed.()

Keybase proof

I hereby claim:

  • I am buren on github.
  • I am buren (https://keybase.io/buren) on keybase.
  • I have a public key whose fingerprint is A7ED 5FE6 79AA 414B 405F E637 CCC5 6E36 265C 2FB3

To claim this, I am signing this object:

@buren
buren / rails_web_console_param.rb
Last active February 13, 2016 18:16
Attach a rails web console to any page by adding ?web_console=
# config/initializers/web_console.rb
WebConsoleBeforeAction = ->(controller) do
controller.console if controller.params[:web_console]
end
ApplicationController.before_action(WebConsoleBeforeAction) if defined?(WebConsole) && Rails.env.development?
# NOTE:
# For security reasons only do this in development.
@buren
buren / language_wikimedia.js
Created March 23, 2016 17:29
and convert languages from Wikimedia to CSV
// Extract and convert languages @ below URL to CSV
// https://meta.wikimedia.org/wiki/Template:List_of_language_names_ordered_by_code
var langCode = [];
$('.wikitable tbody tr td:nth-child(1)').each(function(){
langCode.push($(this).text());
});
var enName = [];
$('.wikitable tbody tr td:nth-child(2)').each(function(){
@buren
buren / a-jsonapi-datastore.js
Last active March 29, 2016 16:10
Example jsonapi-datastore usage
var fs = require('fs')
var JsonApiDataStore = require('jsonapi-datastore').JsonApiDataStore;
function readExampleToObject(fileName, callback) {
fs.readFile(fileName, 'utf8', function (err, data) {
if (err) {
return console.log(err);
}
return callback(JSON.parse(data));
});