aboutsummaryrefslogtreecommitdiffstats
path: root/lib/active_relation/relations/rename.rb
blob: c4ba6ded887fa806685d83f7c12dc8e3da6393fb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
module ActiveRelation
  module Relations
    class Rename < Compound
      attr_reader :schmattribute, :rename
  
      def initialize(relation, renames)
        @schmattribute, @rename = renames.shift
        @relation = renames.empty?? relation : Rename.new(relation, renames)
      end
  
      def ==(other)
        relation == other.relation and schmattribute == other.schmattribute and self.rename == other.rename
      end
  
      def attributes
        relation.attributes.collect(&method(:substitute))
      end
  
      def qualify
        Rename.new(relation.qualify, schmattribute.qualify => self.rename)
      end
  
      protected
      def attribute(name)
        case
        when name == self.rename then schmattribute.as(self.rename)
        when relation[name] == schmattribute then nil
        else relation[name]
        end
      end
  
      private
      def substitute(a)
        a == schmattribute ? a.as(self.rename) : a
      end
    end
  end
end