diff options
Diffstat (limited to 'lib/active_relation/relations/rename.rb')
-rw-r--r-- | lib/active_relation/relations/rename.rb | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/lib/active_relation/relations/rename.rb b/lib/active_relation/relations/rename.rb new file mode 100644 index 0000000000..7a1693df57 --- /dev/null +++ b/lib/active_relation/relations/rename.rb @@ -0,0 +1,38 @@ +module ActiveRelation + module Relations + class Rename < Compound + attr_reader :relation, :schmattribute, :alias + + def initialize(relation, renames) + @schmattribute, @alias = renames.shift + @relation = renames.empty?? relation : Rename.new(relation, renames) + end + + def ==(other) + relation == other.relation and schmattribute.eql?(other.schmattribute) and self.alias == other.alias + end + + def attributes + relation.attributes.collect { |a| substitute(a) } + end + + def qualify + Rename.new(relation.qualify, schmattribute.qualify => self.alias) + end + + protected + def attribute(name) + case + when name == self.alias then schmattribute.alias(self.alias) + when relation[name].eql?(schmattribute) then nil + else relation[name] + end + end + + private + def substitute(a) + a.eql?(schmattribute) ? a.alias(self.alias) : a + end + end + end +end
\ No newline at end of file |