Skip to content

Instantly share code, notes, and snippets.

View peter's full-sized avatar

Peter Marklund peter

View GitHub Profile
@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 / 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 / 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 / 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 / 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: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:658203
Created November 1, 2010 13:56
Avoid Stubbing Non-Existent Methods with Mocha
# In spec_helper.rb or test_helper.rb:
Mocha::Configuration.prevent(:stubbing_non_existent_method)
module MochaExtensions
module ObjectMethods
def expects_message(*args, &block)
Mocha::Configuration.allow(:stubbing_non_existent_method) { expects(*args, &block) }
end
def stubs_message(*args, &block)
# Notes on migrating from the RSpec builtin mocking framework to the Mocha framework
# 1. Comment in this line in spec_helper.rb:
config.mock_with :mocha
# 2. Specify your gem dependency (optional) in config/environments/test.rb:
config.gem "mocha", :version => "0.9.8"
# 3. Use query-replace to do most but not all of the DSL conversion
# should_receive -> expects
-- Order by one indexed column (FAST)
newsdesk_production=# explain analyze select * from pressreleases order by published_at DESC limit 100;
QUERY PLAN
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=0.00..249.91 rows=100 width=1207) (actual time=26.070..716.453 rows=100 loops=1)
-> Index Scan Backward using pressreleases_published_at_index on pressreleases (cost=0.00..964766.62 rows=386042 width=1207) (actual time=26.067..716.343 rows=100 loops=1)
Total runtime: 716.709 ms
(3 rows)
- Order by two separately indexed columns (SLOW)
# => SELECT "posts".* FROM "posts" ORDER BY published DESC, id DESC LIMIT 1
Post.send(:with_scope, {:find => {:order => "id DESC"}}) do
Post.first(:order => "published DESC")
end