aboutsummaryrefslogtreecommitdiffstats
path: root/lib/active_relation/primitives
diff options
context:
space:
mode:
Diffstat (limited to 'lib/active_relation/primitives')
-rw-r--r--lib/active_relation/primitives/aggregation.rb4
-rw-r--r--lib/active_relation/primitives/attribute.rb21
2 files changed, 18 insertions, 7 deletions
diff --git a/lib/active_relation/primitives/aggregation.rb b/lib/active_relation/primitives/aggregation.rb
index 48f8946835..26348fb35e 100644
--- a/lib/active_relation/primitives/aggregation.rb
+++ b/lib/active_relation/primitives/aggregation.rb
@@ -6,6 +6,10 @@ module ActiveRelation
@attribute, @function_sql = attribute, function_sql
end
+ def substitute(new_relation)
+ Aggregation.new(attribute.substitute(new_relation), function_sql)
+ end
+
def to_sql(strategy = nil)
"#{function_sql}(#{attribute.to_sql})"
end
diff --git a/lib/active_relation/primitives/attribute.rb b/lib/active_relation/primitives/attribute.rb
index 90bbe8012b..eb167b1a66 100644
--- a/lib/active_relation/primitives/attribute.rb
+++ b/lib/active_relation/primitives/attribute.rb
@@ -6,18 +6,25 @@ module ActiveRelation
@relation, @name, @alias = relation, name, aliaz
end
- def as(aliaz = nil)
- Attribute.new(relation, name, aliaz)
- end
+ module Transformations
+ def as(aliaz = nil)
+ Attribute.new(relation, name, aliaz)
+ end
+
+ def substitute(new_relation)
+ Attribute.new(new_relation, name, @alias)
+ end
+ def qualify
+ self.as(qualified_name)
+ end
+ end
+ include Transformations
+
def qualified_name
"#{relation.name}.#{name}"
end
- def qualify
- self.as(qualified_name)
- end
-
def ==(other)
relation == other.relation and name == other.name and @alias == other.alias
end