Sha256: f5adb4c94c2ededf1c3b3102211aef38f7538503e2c51ee5e27ec8a399c4da63

Contents?: true

Size: 1.42 KB

Versions: 2

Compression:

Stored size: 1.42 KB

Contents

module ActiveRecord
  class PredicateBuilder

    def initialize(engine)
      @engine = engine
    end

    def build_from_hash(attributes, default_table, allow_table_name = true)
      predicates = attributes.map do |column, value|
        table = default_table

        if allow_table_name && value.is_a?(Hash)
          table = Arel::Table.new(column, :engine => @engine)

          if value.empty?
            '1 = 2'
          else
            build_from_hash(value, table, false)
          end
        else
          column = column.to_s

          if allow_table_name && column.include?('.')
            table_name, column = column.split('.', 2)
            table = Arel::Table.new(table_name, :engine => @engine)
          end

          attribute = table[column] || Arel::Attribute.new(table, column)

          case value
          when Array, ActiveRecord::Associations::AssociationCollection, ActiveRecord::Relation
            values = value.to_a.map { |x|
              x.is_a?(ActiveRecord::Base) ? x.id : x
            }
            attribute.in(values)
          when Range, Arel::Relation
            attribute.in(value)
          when ActiveRecord::Base
            attribute.eq(value.id)
          when Class
            # FIXME: I think we need to deprecate this behavior
            attribute.eq(value.name)
          else
            attribute.eq(value)
          end
        end
      end

      predicates.flatten
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
activerecord-3.0.20 lib/active_record/relation/predicate_builder.rb
activerecord-3.0.19 lib/active_record/relation/predicate_builder.rb