Skip to content

Instantly share code, notes, and snippets.

View ernie's full-sized avatar

Ernie Miller ernie

View GitHub Profile
From f2bd8bd136b206c6ff48b8c2633e8ab8ca224eb1 Mon Sep 17 00:00:00 2001
From: Ernie Miller <ernie@metautonomo.us>
Date: Tue, 13 Apr 2010 10:45:44 -0400
Subject: [PATCH] New predicates, including Not (Unary), Any/All (Polyadic), and NotMatch/NotIn (Binary)
This patch adds support for some new types of predicates and
cleans up a few small things. Tests are included.
It adds a unary predicate, Not. Predicate#not will negate the predicate
on which it's called.
Autojoin sample results:
a = Article.includes(:comments).where(:comments => {:moderations => {:value => 1},:body => 'hey'}).autojoin
Article Load (0.4ms) SELECT "articles".* FROM "articles" INNER JOIN "comments" ON "comments"."article_id" = "articles"."id" INNER JOIN "moderations" ON "moderations"."comment_id" = "comments"."id" WHERE (("moderations"."value" = 1 AND "comments"."body" = 'hey'))
Comment Load (0.4ms) SELECT "comments".* FROM "comments" WHERE ("comments".article_id = 1)
a = Article.eager_load(:moderations).where(:comments => {:moderations => {:value => 1},:body => 'hey'}).autojoin.first
Article Load (0.3ms) SELECT DISTINCT "articles".id FROM "articles" LEFT OUTER JOIN "comments" ON "articles"."id" = "comments"."article_id" LEFT OUTER JOIN "moderations" ON "moderations"."comment_id" = "comments"."id" INNER JOIN "comments" "comments_articles" ON "comments_articles"."article_id" = "articles"."id" INNER JOIN "moderations" "moderations_comments" ON "moderations_comments"."comment_id"
ruby-1.9.2-head > params = {:search => {:created_at_gteq => 2.weeks.ago}}
=> {:search=>{:created_at_gteq=>Mon, 28 Jun 2010 11:46:34 EDT -04:00}}
ruby-1.9.2-head > @search = Article.search(params[:search])
=> #<MetaSearch::Builder:0x00000100ac2820 @relation=[], @base=Article(id: integer, title: string, body: text, created_at: datetime, updated_at: datetime, lookup_id: integer), @opts={}, @join_dependency=#<ActiveRecord::Associations::ClassMethods::JoinDependency:0x00000100ac25c8 @joins=[#<ActiveRecord::Associations::ClassMethods::JoinDependency::JoinBase:0x00000100ac25a0 @active_record=Article(id: integer, title: string, body: text, created_at: datetime, updated_at: datetime, lookup_id: integer), @cached_record={}, @table_joins=nil>], @associations=[], @reflections=[], @base_records_hash={}, @base_records_in_order=[], @table_aliases={"articles"=>1}>, @search_attributes={"created_at_greater_than_or_equal_to"=>Mon, 28 Jun 2010 11:46:34 EDT -04:00}>
ruby-1.9.2-head > @search.created_at_lteq ||= @search.creat
module ActionView
module Helpers
class FormBuilder
Check = Struct.new(:box, :label)
# Behaves almost exactly like the select method, but instead of generating a select tag,
# generates Checks. These consist of two attributes, +box+ and +label+,
# which are (unsurprisingly) the HTML for the check box and the label. Called without a block,
# this method will return an array of check boxes. Called with a block, it will yield each
# check box to your template.
ruby-1.9.2-head > a = Arel::Predicates::Inequality.new(1,1)
=> #<Arel::Predicates::Inequality:0x00000101a0b480 @operand=1, @operand2=1>
ruby-1.9.2-head > a.is_a?(Arel::Predicates::Equality)
=> true
ruby-1.9.2-head > Arel::Predicates::Equality === a
=> true
ruby-1.9.2-head > [a].grep(Arel::Predicates::Equality)
=> [#<Arel::Predicates::Inequality:0x00000101a0b480 @operand=1, @operand2=1>]
From 6cb5f1f85c8af1a37a6a27e6f69c0f41a84bbf2f Mon Sep 17 00:00:00 2001
From: Ernie Miller <ernie@metautonomo.us>
Date: Mon, 4 Oct 2010 13:35:38 -0400
Subject: [PATCH 1/2] Convert to model before calling model_name on a record in ActiveModel::Naming
---
activemodel/lib/active_model/naming.rb | 6 +++++-
1 files changed, 5 insertions(+), 1 deletions(-)
diff --git a/activemodel/lib/active_model/naming.rb b/activemodel/lib/active_model/naming.rb
From 124de9bdb3109236af68d06052ce21a4522059e6 Mon Sep 17 00:00:00 2001
From: Ernie Miller <ernie@metautonomo.us>
Date: Wed, 27 Oct 2010 09:43:20 -0400
Subject: [PATCH 1/2] Refactor predication methods to be available to SqlLiterals as well.
---
lib/arel.rb | 1 +
lib/arel/attributes/attribute.rb | 175 +-------------------------------------
lib/arel/nodes/sql_literal.rb | 1 +
lib/arel/predications.rb | 177 ++++++++++++++++++++++++++++++++++++++
@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 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(-)