Skip to content

Instantly share code, notes, and snippets.

View peter's full-sized avatar

Peter Marklund peter

View GitHub Profile
# This patch gives us an easy way to globally disable/enable the cache
Rails.cache.class.class_eval do
attr_accessor :enabled
def read_with_enabled(*args, &block)
enabled ? read_without_enabled(*args, &block) : nil
end
alias_method_chain :read, :enabled
end
if File.basename($0) == "rake" && ARGV.include?("db:migrate")
# => 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
# 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
@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)
@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: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 / 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 / java_vs_clojure_immutable.md
Last active March 23, 2016 08:59
Java vs Clojure - Dealing with JSON and Immutable Value Objects

Java

We are using the Builder pattern here. Notice how each field is duplicated about five times and how we need to implement equals and hashCode manually (left as an exercise for the reader):

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

@JsonDeserialize(builder = User.Builder.class)
(ns versioned.crud-api-attributes
(:require [versioned.model-support :as model-support]
[versioned.model-spec :as model-spec :refer [Model]]
[versioned.json-api :as json-api]
[versioned.model-attributes :refer [api-writable-attributes api-readable-attributes]]
[versioned.crud-api-audit :refer [updated-by created-by save-changelog]]
[versioned.crud-api-types :refer [coerce-attribute-types]]
[clojure.spec :as s]
[schema.core :as schema]))