aboutsummaryrefslogtreecommitdiffstats
path: root/lib/active_relation/relations/rename.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/active_relation/relations/rename.rb')
-rw-r--r--lib/active_relation/relations/rename.rb40
1 files changed, 26 insertions, 14 deletions
diff --git a/lib/active_relation/relations/rename.rb b/lib/active_relation/relations/rename.rb
index 94e5edcd47..8942ffbe29 100644
--- a/lib/active_relation/relations/rename.rb
+++ b/lib/active_relation/relations/rename.rb
@@ -1,40 +1,52 @@
module ActiveRelation
class Rename < Compound
- attr_reader :autonym, :pseudonym
+ attr_reader :attribute, :pseudonym
def initialize(relation, pseudonyms)
- @autonym, @pseudonym = pseudonyms.shift
+ @attribute, @pseudonym = pseudonyms.shift
@relation = pseudonyms.empty?? relation : Rename.new(relation, pseudonyms)
end
def ==(other)
- relation == other.relation and autonym == other.autonym and pseudonym == other.pseudonym
+ self.class == other.class and relation == other.relation and attribute == other.attribute and pseudonym == other.pseudonym
end
def qualify
- Rename.new(relation.qualify, autonym.qualify => self.pseudonym)
+ Rename.new(relation.qualify, attribute.qualify => pseudonym)
end
def attributes
- projections.collect(&:to_attribute)
+ relation.attributes.collect(&method(:substitute))
end
protected
- def projections
- relation.send(:projections).collect(&method(:substitute))
- end
-
- def attribute(name)
+ def attribute_for_name(name)
case
- when name == pseudonym then autonym.as(pseudonym)
- when relation[name] == autonym then nil
- else relation[name]
+ when referring_by_autonym?(name) then nil
+ when referring_by_pseudonym?(name) then attribute.as(pseudonym).substitute(self)
+ else (a = relation[name]) && a.substitute(self)
end
end
+
+ def attribute_for_attribute(attribute)
+ attribute.relation == self ? attribute : substitute(relation[attribute])
+ end
+
+ def attribute_for_expression(expression)
+ expression.relation == self ? expression : substitute(relation[expression])
+ end
private
def substitute(attribute)
- attribute == autonym ? attribute.as(pseudonym) : attribute
+ (relation[attribute] == relation[self.attribute] ? attribute.as(pseudonym) : attribute).substitute(self) if attribute
+ end
+
+ def referring_by_autonym?(name)
+ relation[name] == relation[attribute]
+ end
+
+ def referring_by_pseudonym?(name)
+ name == pseudonym
end
end
end \ No newline at end of file