Skip to content

Instantly share code, notes, and snippets.

View peter's full-sized avatar

Peter Marklund peter

View GitHub Profile
@peter
peter / gist:663900
Created November 5, 2010 09:52
debug_log example
require 'rubygems'
require 'debug_log'
my_list = [:a, :b, :c]
my_number = 5
debug.log("after variable initialization", "my_number", "my_list")
# => DebugLog | after variable initialization | my_number="5" (Fixnum), my_list="[:a, :b, :c]" (Array) | /Users/peter/tmp/debug-example.rb:6:in `foo'
@peter
peter / gist:700194
Created November 15, 2010 09:21
Rails Migration With Tests
class ImportLegacyDevices < ActiveRecord::Migration
def self.up
return unless Rails.env.production?
legacy_devices.each do |device_id, issue|
if device = Device.find_by_hardware_id(device_id)
unless InclusiveIssue.exists?(:issue_id => issue.id, :device_id => device.id)
InclusiveIssue.create!(
:issue => issue,
:device => device,
@peter
peter / gist:715518
Created November 25, 2010 15:15
Make sure ActiveRecord saves NULL in your database instead of empty strings
module NullifyTextAttributes
def self.included(base)
base.class_eval do
before_validation :nullify_text_attributes
private
def nullify_text_attributes
self.class.columns.select { |c| [:text, :string].include?(c.type) }.each do |column|
send("#{column.name}=", nil) if read_attribute(column.name).blank?
end
@peter
peter / ruby_inject_readable.rb
Created September 7, 2011 14:09
Ruby inject example, concise and yet readable?
# Traditional idiom
total = 1
[2, 3, 5].each do |value|
total = total * value
end
puts total
# Ruby idiom
puts [2, 3, 5].inject { |mult, value| mult * value }
@peter
peter / about_scoring_project.rb
Created September 8, 2011 14:23
Solution to Ruby Koans Scoring Project (about_scoring_project.rb)
def counts_from_dice(dice)
dice.inject({}) do |hash, value|
hash[value] ||= 0
hash[value] += 1
hash
end
end
def score(dice)
counts_from_dice(dice).inject(0) do |sum, (number, count)|
@peter
peter / creating-edgerails-app.sh
Created June 30, 2012 21:03
Creating and Deploying an EdgeRails (Rails 4) Application to Heroku
# 0. Make sure you have Ruby 1.9.3 installed, and optionally RVM and PostgreSQL
# 0.2 If you are on the Mac, make sure you have a c compiler by installing XCode Command Line Tools or gcc4.2 with homebrew
# https://github.com/mxcl/homebrew/wiki/Custom-GCC-and-cross-compilers
# 0.5 Make sure you have bundler version ~> 1.2 as Rails depends on it
gem install bundler
# 1. Get edge Rails source (master branch)
git clone https://github.com/rails/rails.git
@peter
peter / identity_map.rb
Created January 25, 2013 13:33
Poor man's ActiveRecord identity map
# Poor man's identity map for reusing ActiveRecord objects and avoid fetching the same
# record multiple times from the database within a request or background job. The check method can
# be used to locate duplicate queries in development. The goal is to instantiate
# each ActiveRecord object only once. The Rails identity map is known to have issues:
# http://stackoverflow.com/questions/6905891/rails-3-1-identity-map-issues
class IdentityMap
# Set one or more ActiveRecord objects in the map
def set(*objects)
objects.each do |object|
map[key(object)] = object
@peter
peter / rack-rest-api-example.rb
Created January 31, 2013 19:06
An example of how to write a simple JSON REST API with a simplistic router directly on top of Rack (i.e. without the use of a framework like Sinatra).
#################################################################
#
# File: lib/api.rb
#
#################################################################
require 'user' # ActiveRecord model
# Connect to the db
ActiveRecord::Base.establish_connection(ENV['DATABASE_URL'])
@peter
peter / gist:9121297
Created February 20, 2014 19:24
Force SSL for Express app on Heroku
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Hooking up the middleware with the express app
// In app.js
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
var forceSSL = require('../middleware/ssl').force(config.hostname);
if ('production' == app.get('env')) {
app.use(forceSSL);
}
@peter
peter / carrierwave-file-size-limit-example.rb
Created March 7, 2014 09:17
Checking and Limiting File Size of Uploaded Files with Carrierwave
# NOTE: There must be a better way to do this - help appreciated!
###############################################################
# uploaded_file.rb - utility class
###############################################################
class UploadedFile
def self.size(file)
uploaded?(file) ? file.size : nil
end