Sha256: 3e40bf6934bb45a3d1f00e7c2d4c341359571e7083f679fbb468e481049c72cf

Contents?: true

Size: 1.25 KB

Versions: 98

Compression:

Stored size: 1.25 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 Arel::Nodes::HomogeneousIn.new(values, attribute, :in)
          end

        unless nils.empty?
          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

98 entries across 93 versions & 10 rubygems

Version Path
trusty-cms-7.0.9.1 vendor/bundle/ruby/3.3.0/gems/activerecord-7.0.8.7/lib/active_record/relation/predicate_builder/array_handler.rb
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/activerecord-7.1.3.4/lib/active_record/relation/predicate_builder/array_handler.rb
activerecord-7.1.5.1 lib/active_record/relation/predicate_builder/array_handler.rb
activerecord-7.0.8.7 lib/active_record/relation/predicate_builder/array_handler.rb
activerecord-7.1.5 lib/active_record/relation/predicate_builder/array_handler.rb
activerecord-7.1.4.2 lib/active_record/relation/predicate_builder/array_handler.rb
activerecord-7.0.8.6 lib/active_record/relation/predicate_builder/array_handler.rb
activerecord-6.1.7.10 lib/active_record/relation/predicate_builder/array_handler.rb
activerecord-6.1.7.9 lib/active_record/relation/predicate_builder/array_handler.rb
activerecord-7.1.4.1 lib/active_record/relation/predicate_builder/array_handler.rb
activerecord-7.0.8.5 lib/active_record/relation/predicate_builder/array_handler.rb
activerecord-7.1.4 lib/active_record/relation/predicate_builder/array_handler.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/activerecord-7.0.8.4/lib/active_record/relation/predicate_builder/array_handler.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/activerecord-7.1.3.4/lib/active_record/relation/predicate_builder/array_handler.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/activerecord-7.0.5.1/lib/active_record/relation/predicate_builder/array_handler.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/activerecord-7.0.5.1/lib/active_record/relation/predicate_builder/array_handler.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/activerecord-7.0.5.1/lib/active_record/relation/predicate_builder/array_handler.rb
tinymce-rails-7.1.2 vendor/bundle/ruby/3.3.0/gems/activerecord-7.1.3.4/lib/active_record/relation/predicate_builder/array_handler.rb
activerecord-7.1.3.4 lib/active_record/relation/predicate_builder/array_handler.rb
activerecord-7.0.8.4 lib/active_record/relation/predicate_builder/array_handler.rb