aboutsummaryrefslogblamecommitdiffstats
path: root/activerecord/lib/active_record/relation/where_clause_factory.rb
blob: 13747853540dd3e09926398b9a9e2dc3494281e6 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11

                             

                   
                                      





                                              





                                                                                     
                                    
 
                                                               
                              
                        

                                                                                   

           
                              



               
                                              


       
# frozen_string_literal: true

module ActiveRecord
  class Relation
    class WhereClauseFactory # :nodoc:
      def initialize(klass, predicate_builder)
        @klass = klass
        @predicate_builder = predicate_builder
      end

      def build(opts, other)
        case opts
        when String, Array
          parts = [klass.send(:sanitize_sql, other.empty? ? opts : ([opts] + other))]
        when Hash
          attributes = predicate_builder.resolve_column_aliases(opts)
          attributes = klass.send(:expand_hash_conditions_for_aggregates, attributes)
          attributes.stringify_keys!

          parts = predicate_builder.build_from_hash(attributes)
        when Arel::Nodes::Node
          parts = [opts]
        else
          raise ArgumentError, "Unsupported argument type: #{opts} (#{opts.class})"
        end

        WhereClause.new(parts)
      end

      protected

        attr_reader :klass, :predicate_builder
    end
  end
end