Skip to content

Instantly share code, notes, and snippets.

View ernie's full-sized avatar

Ernie Miller ernie

View GitHub Profile
@ernie
ernie / evil.rb
Created November 16, 2010 14:11
A really evil trick to get at the wrapped AR model in a class method
def initialize(base_or_relation, opts = {})
@relation = base_or_relation.scoped
@base = @relation.klass
@search_key = opts[:search_key] ? opts[:search_key].to_s : 'search'
@join_dependency = build_join_dependency
@search_attributes = {}
@errors = ActiveModel::Errors.new(self)
singleton_class.instance_eval "def base; #{@base} end"
end
@ernie
ernie / verb.rb
Created November 18, 2010 02:04
One of the reasons I love MetaWhere (I may be biased :))
# Can this be done with strings? Sure. This feels much nicer to me, though.
class Verb < ActiveRecord::Base
belongs_to :verbable, :polymorphic => true
belongs_to :objectifiable, :polymorphic => true
scope :of_interest_to_user, lambda {|u|
where(
(:objectifiable_type.eq % 'User' & :objectifiable_id.eq % u.id) |
(:objectifiable_type.eq % 'Book' & :objectifiable_id.in % u.book_ids)
)
From 1be13d1c88ca518996135e89454c17691afbcc51 Mon Sep 17 00:00:00 2001
From: Ernie Miller <ernie@metautonomo.us>
Date: Thu, 6 Jan 2011 20:06:29 -0500
Subject: [PATCH] Fix polymorphic belongs_to associationproxy raising errors when loading target.
---
.../belongs_to_polymorphic_association.rb | 5 +++++
.../associations/belongs_to_associations_test.rb | 9 +++++++++
activerecord/test/models/sponsor.rb | 2 ++
3 files changed, 16 insertions(+), 0 deletions(-)
From 61ad8e150d74f6c2bafeaac73f1b40d11d3092c1 Mon Sep 17 00:00:00 2001
From: Ernie Miller <ernie@metautonomo.us>
Date: Thu, 6 Jan 2011 17:13:01 -0500
Subject: [PATCH] Fix polymorphic belongs_to associationproxy raising errors when loading target.
---
.../belongs_to_polymorphic_association.rb | 4 ++++
.../associations/belongs_to_associations_test.rb | 9 +++++++++
activerecord/test/models/sponsor.rb | 2 ++
3 files changed, 15 insertions(+), 0 deletions(-)
@ernie
ernie / javascripts.rake
Created March 9, 2011 19:07
Rake task for minifying javascripts using YUI Compressor
namespace :javascripts do
desc 'minify the javascript files residing in app/javascripts to public/javascripts'
task :minify do
RakeFileUtils.verbose false do
Dir["app/javascripts/*.js"].each do |filename|
outfile = filename.sub(/^app/, 'public').sub(/\.js$/, '.min.js')
puts "#{filename} -> #{outfile}"
sh(
'java',
'-jar',
@ernie
ernie / MetaWhere 2.0.rb
Created March 13, 2011 19:31
Sample queries with MW 2.0
Article.joins{person.comments}.where{person.comments.body =~ '%hello%'}.to_sql
# => "SELECT \"articles\".* FROM \"articles\" INNER JOIN \"people\" ON \"people\".\"id\" = \"articles\".\"person_id\" INNER JOIN \"comments\" ON \"comments\".\"person_id\" = \"people\".\"id\" WHERE \"comments\".\"body\" LIKE '%hello%'"
Person.where{(id + 1) == 2}.first
# => #<Person id: 1, parent_id: nil, name: "Aric Smith", salary: 31000>
Person.where{(salary - 40000) < 0}.to_sql
# => "SELECT \"people\".* FROM \"people\" WHERE \"people\".\"salary\" - 40000 < 0"
p = Person.select{[id, name, salary, (salary + 1000).as('salary_after_increase')]}.first
@ernie
ernie / pow.sh
Created April 7, 2011 19:10
A couple of bash functions for use with pow
# These are nothing fancy. Stick them in your .profile for quick access.
# Tail the log of a pow application
function powl {
tail -f ~/.pow/"$1"/log/development.log
}
# Restart a pow application
function powr {
touch ~/.pow/"$1"/tmp/restart.txt
@ernie
ernie / grouping_benchmark.rb
Created April 18, 2011 16:10
Benchmark for grouping optimization
require 'benchmark'
$LOAD_PATH << './lib'
require 'arel'
def grouping_any(range)
literal = Arel.sql('1')
literal.eq_any(range.to_a)
end
def grouping_all(range)
@ernie
ernie / select_distinct.rb
Created June 9, 2011 21:05
Attempt #1 at cross-DB select distinct
# So, a SELECT DISTINCT on some DBs requires that all order values are in the select clause.
# I'm hoping there's a better cross-platform way of doing this without branching, and I
# kind of think I must be overlooking something that's already built-in.
# Open to suggestions!
def distinct_relation(relation)
offset, limit = relation.offset_value, relation.limit_value
klass.select('DISTINCT subquery.*').
from(Arel::Nodes::As.new(
Arel::Nodes::Grouping.new(relation.except(:offset, :limit).arel.ast),
Arel.sql('subquery')
@ernie
ernie / benchmark.rb
Created July 16, 2011 20:46
A benchmark of some enumerable methods
require 'benchmark'
haystack = (1..1_000_000).to_a
needles = 1.upto(100).map {|n| n * 10_000}
module EachDetector
def self.find(haystack, needle)
haystack.each do |v|
return true if v == needle
end