Skip to content

Commit

Permalink
some convenience methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick Kallen committed May 20, 2008
1 parent e8966bf commit 78b5b57
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions doc/TODO
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions lib/arel.rb
Expand Up @@ -4,6 +4,7 @@
require 'activesupport'
require 'activerecord'

require 'arel/arel'
require 'arel/extensions'
require 'arel/sql'
require 'arel/predicates'
Expand Down
3 changes: 3 additions & 0 deletions lib/arel/arel.rb
@@ -0,0 +1,3 @@
def Arel(name, engine = Arel::Table.engine)
Arel::Table.new(name, engine)
end
20 changes: 10 additions & 10 deletions lib/arel/relations/relation.rb
Expand Up @@ -57,34 +57,34 @@ 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

def alias
Alias.new(self)
end

def order(*attributes)
def order(*attributes, &block)
attributes.all?(&:blank?) ? self : Order.new(self, *attributes)
end

Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion spec/arel/integration/joins/with_adjacency_spec.rb
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions spec/arel/integration/joins/with_aggregations_spec.rb
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions spec/arel/integration/joins/with_compounds_spec.rb
Expand Up @@ -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

Expand Down

0 comments on commit 78b5b57

Please sign in to comment.