Sha256: 6adabddb96ece12fecb6f6ceb5aeed8f2933cca702c2cda456dfb335015c5960

Contents?: true

Size: 844 Bytes

Versions: 43

Compression:

Stored size: 844 Bytes

Contents

module ActiveRecord
  class PredicateBuilder
    class ArrayHandler # :nodoc:
      def call(attribute, value)
        values = value.map { |x| x.is_a?(Base) ? x.id : x }
        ranges, values = values.partition { |v| v.is_a?(Range) }

        values_predicate = if values.include?(nil)
          values = values.compact

          case values.length
          when 0
            attribute.eq(nil)
          when 1
            attribute.eq(values.first).or(attribute.eq(nil))
          else
            attribute.in(values).or(attribute.eq(nil))
          end
        else
          attribute.in(values)
        end

        array_predicates = ranges.map { |range| attribute.in(range) }
        array_predicates << values_predicate
        array_predicates.inject { |composite, predicate| composite.or(predicate) }
      end
    end
  end
end

Version data entries

43 entries across 43 versions & 2 rubygems

Version Path
activerecord-4.1.16 lib/active_record/relation/predicate_builder/array_handler.rb
activerecord-4.1.16.rc1 lib/active_record/relation/predicate_builder/array_handler.rb
activerecord-4.1.15 lib/active_record/relation/predicate_builder/array_handler.rb
activerecord-4.1.15.rc1 lib/active_record/relation/predicate_builder/array_handler.rb
activerecord-4.1.14.2 lib/active_record/relation/predicate_builder/array_handler.rb
activerecord-4.1.14.1 lib/active_record/relation/predicate_builder/array_handler.rb
angular-rails4-templates-0.4.1 vendor/ruby/2.1.0/gems/activerecord-4.1.13/lib/active_record/relation/predicate_builder/array_handler.rb
angular-rails4-templates-0.4.0 vendor/ruby/2.1.0/gems/activerecord-4.1.13/lib/active_record/relation/predicate_builder/array_handler.rb
angular-rails4-templates-0.3.0 vendor/ruby/2.1.0/gems/activerecord-4.1.13/lib/active_record/relation/predicate_builder/array_handler.rb
activerecord-4.1.14 lib/active_record/relation/predicate_builder/array_handler.rb
activerecord-4.1.14.rc2 lib/active_record/relation/predicate_builder/array_handler.rb
activerecord-4.1.14.rc1 lib/active_record/relation/predicate_builder/array_handler.rb
activerecord-4.1.13 lib/active_record/relation/predicate_builder/array_handler.rb
activerecord-4.1.13.rc1 lib/active_record/relation/predicate_builder/array_handler.rb
activerecord-4.1.12 lib/active_record/relation/predicate_builder/array_handler.rb
activerecord-4.1.12.rc1 lib/active_record/relation/predicate_builder/array_handler.rb
activerecord-4.1.11 lib/active_record/relation/predicate_builder/array_handler.rb
activerecord-4.1.10 lib/active_record/relation/predicate_builder/array_handler.rb
activerecord-4.1.10.rc4 lib/active_record/relation/predicate_builder/array_handler.rb
activerecord-4.1.10.rc3 lib/active_record/relation/predicate_builder/array_handler.rb