Skip to content

Instantly share code, notes, and snippets.

View tilo's full-sized avatar
👋

Tilo tilo

👋
View GitHub Profile
@tilo
tilo / schema_dumper_2_of_2.rb
Last active February 7, 2018 23:41
ActiveRecord Monkey Patch to make Rails 5 dump DB agnostic format (2 of 2)
# config/initializers/active_record/connection_adapters/abstract/schema_dumper.rb
# DANGER, Will Robinson!!!
#
# This is a monkey patch for Rails 5.0 ONLY
#
if Rails.version !~ /^5.0/ # code taken from 5.0.6
puts "\n
--------------------------------------------------------------------------------
@tilo
tilo / schema_dumper_1_of_2.rb
Last active February 7, 2018 23:40
ActiveRecord Monkey Patch to make Rails 5 dump DB agnostic format (1 of 2)
# config/initializers/active_record/schema_dumper.rb
#
# DANGER, Will Robinson!!!
#
# This is a monkey patch for Rails 5.0 ONLY
#
if Rails.version !~ /^5.0/ # code taken from 5.0.6
puts "\n
--------------------------------------------------------------------------------
@tilo
tilo / convert.rake
Last active May 16, 2018 15:14 — forked from tboyko/convert.rake
namespace :db do
desc "convert a latin1 database with utf8 data into proper utf8"
task :convert_to_utf8 => :environment do
puts Time.now
dryrun = ENV['DOIT'] != '1'
conn = ActiveRecord::Base.connection
if dryrun
def conn.run_sql(sql)
puts(sql)
@tilo
tilo / gist:85ce8bd382027d830ed1
Created May 12, 2014 14:24
Argument Error when using acts-as-taggable-on with Rails 4.1ruby 1.9.3p448 (2013-06-27 revision 41675) [x86_64-darwin13.0.2]
Rails 4.1.0
ruby 1.9.3p448 (2013-06-27 revision 41675) [x86_64-darwin13.0.2]
acts-as-taggable-on versions 3.1.1 and
DB: PostgreSQL / PostGIS
```
t = MyModel.first
@tilo
tilo / hash_extensions.rb
Created September 24, 2012 20:48
Hash mix-in to add IndifferentAccess behavior without duplicating a given hash instance
module Hash::Extensions
# Author: Tilo Sloboda , 2012-09-24
# Gist: https://gist.github.com/3778285
#
# Mix-In Module Hash::Extensions::IndifferentAccess
#
# Adds "Hash with Indifferent Access" behavior to an existing Hash
# without creating a copy of that Hash.
#
@tilo
tilo / prettify_json.rb
Created September 17, 2012 18:37
Ruby script to pretty print JSON on the command line (comes with Ruby's json Gem)
#!/usr/bin/env ruby
require 'json'
require 'fileutils'
include FileUtils
# Parses the argument array _args_, according to the pattern _s_, to
# retrieve the single character command line options from it. If _s_ is
# 'xy:' an option '-x' without an option argument is searched, and an
# option '-y foo' with an option argument ('foo').
@tilo
tilo / awesome_print.rb
Created September 12, 2012 23:03
awesome_print giving NameError: uninitialized constant BSON
# work-around a bug in awesome_print gem, after Mongoid is no longer using BSON, but Moped::BSON
# put this in your Rails project as: ./config/initializers/awesome_print.rb
BSON=Moped::BSON if defined?(AwesomePrint) # conditional, in case you don't add awesome_print in production
@tilo
tilo / process_csv_file.rb
Created July 13, 2012 00:05
Process CSV-like files as Arrays of Hashes; allow key-remapping, chunking, processing via optionally given block
# NOTE:
# This was an initial proof-of-concept!
#
# Please use the Gem "smarter_csv" instead of using this no longer maintained code below.
#
# See also:
# * https://github.com/tilo/smarter_csv
# * http://www.unixgods.org/~tilo/Ruby/process_csv_as_hashes.html
#
# ---------------------------------------------------------------------------
@tilo
tilo / daily_collection.rb
Created June 16, 2012 22:01
Daily Collections with Mongoid 2.4
# Daily Collections With Mongoid 2.4
#
# Author: Tilo Sloboda, 2012-06-16
# Twitter: https://twitter.com/#!/tilosloboda
# GitHub: https://gist.github.com/tilo
# Gist: https://gist.github.com/2942627
#
# This can be useful if you need to re-import certain data every day
# and need to make sure that the latest daily collection is used in production
#
@tilo
tilo / rails_issue_5057.rb
Created February 16, 2012 19:20
Script for Rails Issue #5057
gem 'rails', '3.2.1'
require 'active_record'
puts "Active Record #{ActiveRecord::VERSION::STRING}"
ActiveRecord::Base.establish_connection(
:adapter => 'sqlite3',
:database => ':memory:'
)