Sha256: eb1b70f095eac0604d5406c8b243c8b9524c9021a844005d50b99a878803e5b4

Contents?: true

Size: 1.23 KB

Versions: 23

Compression:

Stored size: 1.23 KB

Contents

# frozen_string_literal: true

require "active_support/core_ext/array/extract"

module ActiveRecord
  class PredicateBuilder
    class ArrayHandler # :nodoc:
      def initialize(predicate_builder)
        @predicate_builder = predicate_builder
      end

      def call(attribute, value)
        return attribute.in([]) if value.empty?

        values = value.map { |x| x.is_a?(Base) ? x.id : x }
        nils = values.compact!
        ranges = values.extract! { |v| v.is_a?(Range) }

        values_predicate =
          case values.length
          when 0 then NullPredicate
          when 1 then predicate_builder.build(attribute, values.first)
          else Arel::Nodes::HomogeneousIn.new(values, attribute, :in)
          end

        if nils
          values_predicate = values_predicate.or(attribute.eq(nil))
        end

        if ranges.empty?
          values_predicate
        else
          array_predicates = ranges.map! { |range| predicate_builder.build(attribute, range) }
          array_predicates.inject(values_predicate, &:or)
        end
      end

      private
        attr_reader :predicate_builder

        module NullPredicate # :nodoc:
          def self.or(other)
            other
          end
        end
    end
  end
end

Version data entries

23 entries across 23 versions & 2 rubygems

Version Path
activerecord-8.0.1 lib/active_record/relation/predicate_builder/array_handler.rb
activerecord-8.0.0.1 lib/active_record/relation/predicate_builder/array_handler.rb
activerecord-7.2.2.1 lib/active_record/relation/predicate_builder/array_handler.rb
activerecord-8.0.0 lib/active_record/relation/predicate_builder/array_handler.rb
activerecord-7.2.2 lib/active_record/relation/predicate_builder/array_handler.rb
activerecord-8.0.0.rc2 lib/active_record/relation/predicate_builder/array_handler.rb
activerecord-7.2.1.2 lib/active_record/relation/predicate_builder/array_handler.rb
activerecord-8.0.0.rc1 lib/active_record/relation/predicate_builder/array_handler.rb
activerecord-7.2.1.1 lib/active_record/relation/predicate_builder/array_handler.rb
activerecord-8.0.0.beta1 lib/active_record/relation/predicate_builder/array_handler.rb
omg-activerecord-8.0.0.alpha9 lib/active_record/relation/predicate_builder/array_handler.rb
omg-activerecord-8.0.0.alpha8 lib/active_record/relation/predicate_builder/array_handler.rb
omg-activerecord-8.0.0.alpha7 lib/active_record/relation/predicate_builder/array_handler.rb
omg-activerecord-8.0.0.alpha4 lib/active_record/relation/predicate_builder/array_handler.rb
omg-activerecord-8.0.0.alpha3 lib/active_record/relation/predicate_builder/array_handler.rb
omg-activerecord-8.0.0.alpha2 lib/active_record/relation/predicate_builder/array_handler.rb
omg-activerecord-8.0.0.alpha1 lib/active_record/relation/predicate_builder/array_handler.rb
activerecord-7.2.1 lib/active_record/relation/predicate_builder/array_handler.rb
activerecord-7.2.0 lib/active_record/relation/predicate_builder/array_handler.rb
activerecord-7.2.0.rc1 lib/active_record/relation/predicate_builder/array_handler.rb