Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

alias name based on column name #76

Closed
wants to merge 1 commit into from

6 participants

@skorfmann

build the alias name based on column name, instead of hard-coded name ("avg/min/max/sum_id)

@vipulnsward

This seems like a good idea. No movement for the last 2 years though.

@a3gis

How would the alias name behave in presence of a JOIN? (two colums sharing the same name)? I'll add a test asap.

@matthewd
Collaborator

As is, this will break on columns whose names need to be quoted

@tamird

This PR is now obsolete; the alias is no longer passed in this initialization. also, i think i'm going to introduce another PR to remove the aliaz parameter from this constructor as I did for extract.

Good to close!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 14, 2011
  1. @skorfmann
This page is out of date. Refresh to see the latest.
Showing with 46 additions and 4 deletions.
  1. +10 −4 lib/arel/expressions.rb
  2. +36 −0 test/attributes/test_attribute.rb
View
14 lib/arel/expressions.rb
@@ -5,19 +5,25 @@ def count distinct = false
end
def sum
- Nodes::Sum.new [self], Nodes::SqlLiteral.new('sum_id')
+ Nodes::Sum.new [self], Nodes::SqlLiteral.new(build_alias_name('sum'))
end
def maximum
- Nodes::Max.new [self], Nodes::SqlLiteral.new('max_id')
+ Nodes::Max.new [self], Nodes::SqlLiteral.new(build_alias_name('max'))
end
def minimum
- Nodes::Min.new [self], Nodes::SqlLiteral.new('min_id')
+ Nodes::Min.new [self], Nodes::SqlLiteral.new(build_alias_name('min'))
end
def average
- Nodes::Avg.new [self], Nodes::SqlLiteral.new('avg_id')
+ Nodes::Avg.new [self], Nodes::SqlLiteral.new(build_alias_name('avg'))
end
+
+ private
+
+ def build_alias_name(kind)
+ [kind, self.name] * '_'
+ end
end
end
View
36 test/attributes/test_attribute.rb
@@ -267,6 +267,15 @@ module Attributes
FROM "users"
}
end
+
+ it 'should set the alias to "avg_price"' do
+ relation = Table.new(:products)
+ mgr = relation.project relation[:price].average
+ mgr.to_sql.must_be_like %{
+ SELECT AVG("products"."price") AS avg_price
+ FROM "products"
+ }
+ end
end
describe '#maximum' do
@@ -284,6 +293,15 @@ module Attributes
FROM "users"
}
end
+
+ it 'should set the alias to "max_price"' do
+ relation = Table.new(:products)
+ mgr = relation.project relation[:price].maximum
+ mgr.to_sql.must_be_like %{
+ SELECT MAX("products"."price") AS max_price
+ FROM "products"
+ }
+ end
end
describe '#minimum' do
@@ -291,6 +309,15 @@ module Attributes
relation = Table.new(:users)
relation[:id].minimum.must_be_kind_of Nodes::Min
end
+
+ it 'should set the alias to "min_price"' do
+ relation = Table.new(:products)
+ mgr = relation.project relation[:price].minimum
+ mgr.to_sql.must_be_like %{
+ SELECT MIN("products"."price") AS min_price
+ FROM "products"
+ }
+ end
end
describe '#sum' do
@@ -308,6 +335,15 @@ module Attributes
FROM "users"
}
end
+
+ it 'should set the alias to "sum_price"' do
+ relation = Table.new(:products)
+ mgr = relation.project relation[:price].sum
+ mgr.to_sql.must_be_like %{
+ SELECT SUM("products"."price") AS sum_price
+ FROM "products"
+ }
+ end
end
describe '#count' do
Something went wrong with that request. Please try again.