From 78b5b5783776f57a47e480db4bd83b15db988909 Mon Sep 17 00:00:00 2001 From: Nick Kallen Date: Mon, 19 May 2008 17:55:49 -0700 Subject: [PATCH] some convenience methods --- README | 2 +- doc/TODO | 1 + lib/arel.rb | 1 + lib/arel/arel.rb | 3 +++ lib/arel/relations/relation.rb | 20 +++++++++---------- .../integration/joins/with_adjacency_spec.rb | 2 +- .../joins/with_aggregations_spec.rb | 4 ++-- .../integration/joins/with_compounds_spec.rb | 4 ++-- 8 files changed, 21 insertions(+), 16 deletions(-) create mode 100644 lib/arel/arel.rb diff --git a/README b/README index 0e8f31ce..d870f066 100644 --- a/README +++ b/README @@ -109,7 +109,7 @@ This will return the first comment's reply's body. Arel can actually perform the aliasing automatically, without the need for the programmer to explicitly call `alias`. However, this makes it difficult to specify the join condition: - comments.join(comments).on(comments[:parent_id].eq(comments[:id])) + comments.join(comments, comments[:parent_id].eq(comments[:id])) # => SELECT * FROM comments INNER JOIN comments AS comments_2 WHERE comments.parent_id = comments.id This does NOT have the same meaning as the previous query. As an alternative to aliasing, there is a convenient block form: diff --git a/doc/TODO b/doc/TODO index 8d805d9f..d7dd75d6 100644 --- a/doc/TODO +++ b/doc/TODO @@ -4,6 +4,7 @@ todo: - result sets to attr correlation too - cache expiry on write - rewrite of arecord querycache test in light of this +- transactions - scoped writes done: diff --git a/lib/arel.rb b/lib/arel.rb index b4fae65f..f56005c2 100644 --- a/lib/arel.rb +++ b/lib/arel.rb @@ -4,6 +4,7 @@ require 'activesupport' require 'activerecord' +require 'arel/arel' require 'arel/extensions' require 'arel/sql' require 'arel/predicates' diff --git a/lib/arel/arel.rb b/lib/arel/arel.rb new file mode 100644 index 00000000..892be854 --- /dev/null +++ b/lib/arel/arel.rb @@ -0,0 +1,3 @@ +def Arel(name, engine = Arel::Table.engine) + Arel::Table.new(name, engine) +end \ No newline at end of file diff --git a/lib/arel/relations/relation.rb b/lib/arel/relations/relation.rb index 4440fb1c..6e041323 100644 --- a/lib/arel/relations/relation.rb +++ b/lib/arel/relations/relation.rb @@ -57,26 +57,26 @@ def first include Enumerable module Operable - def join(other = nil, join_type = "INNER JOIN") - case other + def join(other_relation = nil, join_type = "INNER JOIN") + case other_relation when String - Join.new(other, self) + Join.new(other_relation, self) when Relation - JoinOperation.new(join_type, self, other) + JoinOperation.new(join_type, self, other_relation) else self end end - def outer_join(other = nil) - join(other, "LEFT OUTER JOIN") + def outer_join(other_relation = nil) + join(other_relation, "LEFT OUTER JOIN") end - def where(*predicates) + def where(*predicates, &block) predicates.all?(&:blank?) ? self : Where.new(self, *predicates) end - def project(*attributes) + def project(*attributes, &block) attributes.all?(&:blank?) ? self : Project.new(self, *attributes) end @@ -84,7 +84,7 @@ def alias Alias.new(self) end - def order(*attributes) + def order(*attributes, &block) attributes.all?(&:blank?) ? self : Order.new(self, *attributes) end @@ -96,7 +96,7 @@ def skip(skipped = nil) skipped.blank?? self : Skip.new(self, skipped) end - def group(*groupings) + def group(*groupings, &block) groupings.all?(&:blank?) ? self : Group.new(self, *groupings) end diff --git a/spec/arel/integration/joins/with_adjacency_spec.rb b/spec/arel/integration/joins/with_adjacency_spec.rb index 2fb5895a..32109cc3 100644 --- a/spec/arel/integration/joins/with_adjacency_spec.rb +++ b/spec/arel/integration/joins/with_adjacency_spec.rb @@ -3,7 +3,7 @@ module Arel describe Join do before do - @relation1 = Table.new(:users) + @relation1 = Arel(:users) @relation2 = @relation1.alias @predicate = @relation1[:id].eq(@relation2[:id]) end diff --git a/spec/arel/integration/joins/with_aggregations_spec.rb b/spec/arel/integration/joins/with_aggregations_spec.rb index b4861b0c..9e28237c 100644 --- a/spec/arel/integration/joins/with_aggregations_spec.rb +++ b/spec/arel/integration/joins/with_aggregations_spec.rb @@ -3,8 +3,8 @@ module Arel describe Join do before do - @relation1 = Table.new(:users) - @relation2 = Table.new(:photos) + @relation1 = Arel(:users) + @relation2 = Arel(:photos) @predicate = @relation1[:id].eq(@relation2[:user_id]) end diff --git a/spec/arel/integration/joins/with_compounds_spec.rb b/spec/arel/integration/joins/with_compounds_spec.rb index 3c17ab31..acfe35b2 100644 --- a/spec/arel/integration/joins/with_compounds_spec.rb +++ b/spec/arel/integration/joins/with_compounds_spec.rb @@ -3,8 +3,8 @@ module Arel describe Join do before do - @relation1 = Table.new(:users) - @relation2 = Table.new(:photos) + @relation1 = Arel(:users) + @relation2 = Arel(:photos) @predicate = @relation1[:id].eq(@relation2[:user_id]) end