Sha256: d34026389bacd2e0ee18654fa5339be31f80f067d012bcfbbf9a6458cb46bc5d

Contents?: true

Size: 1.34 KB

Versions: 39

Compression:

Stored size: 1.34 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.extract!(&:nil?)
        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
            values.map! do |v|
              predicate_builder.build_bind_attribute(attribute.name, v)
            end
            values.empty? ? NullPredicate : attribute.in(values)
          end

        unless nils.empty?
          values_predicate = values_predicate.or(predicate_builder.build(attribute, nil))
        end

        array_predicates = ranges.map { |range| predicate_builder.build(attribute, range) }
        array_predicates.unshift(values_predicate)
        array_predicates.inject(&:or)
      end

      private
        attr_reader :predicate_builder

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

Version data entries

39 entries across 39 versions & 4 rubygems

Version Path
activerecord-6.0.6.1 lib/active_record/relation/predicate_builder/array_handler.rb
activerecord-6.0.6 lib/active_record/relation/predicate_builder/array_handler.rb
activerecord-6.0.5.1 lib/active_record/relation/predicate_builder/array_handler.rb
activerecord-6.0.5 lib/active_record/relation/predicate_builder/array_handler.rb
activerecord-6.0.4.8 lib/active_record/relation/predicate_builder/array_handler.rb
activerecord-6.0.4.7 lib/active_record/relation/predicate_builder/array_handler.rb
activerecord-6.0.4.6 lib/active_record/relation/predicate_builder/array_handler.rb
activerecord-6.0.4.5 lib/active_record/relation/predicate_builder/array_handler.rb
activerecord-6.0.4.4 lib/active_record/relation/predicate_builder/array_handler.rb
activerecord-6.0.4.3 lib/active_record/relation/predicate_builder/array_handler.rb
activerecord-6.0.4.2 lib/active_record/relation/predicate_builder/array_handler.rb
activerecord-6.0.4.1 lib/active_record/relation/predicate_builder/array_handler.rb
activerecord-6.0.4 lib/active_record/relation/predicate_builder/array_handler.rb
activerecord-6.0.3.7 lib/active_record/relation/predicate_builder/array_handler.rb
activerecord-6.0.3.6 lib/active_record/relation/predicate_builder/array_handler.rb
activerecord-6.0.3.5 lib/active_record/relation/predicate_builder/array_handler.rb
activerecord-6.0.3.4 lib/active_record/relation/predicate_builder/array_handler.rb
activerecord-6.0.3.3 lib/active_record/relation/predicate_builder/array_handler.rb
activerecord-6.0.3.2 lib/active_record/relation/predicate_builder/array_handler.rb
activerecord-6.0.3.1 lib/active_record/relation/predicate_builder/array_handler.rb