aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/relation/predicate_builder/association_query_handler.rb
blob: 7e20cb2c63b417d00f98ab4b2010b6ed75451f22 (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
module ActiveRecord
  class PredicateBuilder
    class AssociationQueryHandler # :nodoc:
      def self.value_for(table, column, value)
        associated_table = table.associated_table(column)
        klass = if associated_table.polymorphic_association? && ::Array === value && value.first.is_a?(Base)
          PolymorphicArrayValue
        else
          AssociationQueryValue
        end

        klass.new(associated_table, value)
      end

      def initialize(predicate_builder)
        @predicate_builder = predicate_builder
      end

      def call(attribute, value)
        queries = {}

        table = value.associated_table
        if value.base_class
          queries[table.association_foreign_type.to_s] = value.base_class.name
        end

        queries[table.association_foreign_key.to_s] = value.ids
        predicate_builder.build_from_hash(queries)
      end

      protected

        attr_reader :predicate_builder
    end

    class AssociationQueryValue # :nodoc:
      attr_reader :associated_table, :value

      def initialize(associated_table, value)
        @associated_table = associated_table
        @value = value
      end

      def ids
        case value
        when Relation
          value.select(primary_key)
        when Array
          value.map { |v| convert_to_id(v) }
        else
          convert_to_id(value)
        end
      end

      def base_class
        if associated_table.polymorphic_association?
          @base_class ||= polymorphic_base_class_from_value
        end
      end

      private

        def primary_key
          associated_table.association_primary_key(base_class)
        end

        def polymorphic_base_class_from_value
          case value
          when Relation
            value.klass.base_class
          when Array
            val = value.compact.first
            val.class.base_class if val.is_a?(Base)
          when Base
            value.class.base_class
          end
        end

        def convert_to_id(value)
          case value
          when Base
            value._read_attribute(primary_key)
          else
            value
          end
        end
    end
  end
end