Sha256: 57b6713dc9a619dc50a0d0fc16d4b8503a81945e197652382be71efa1863e679

Contents?: true

Size: 1.35 KB

Versions: 5

Compression:

Stored size: 1.35 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)
          build_from_hash(value, table, false)
        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

5 entries across 5 versions & 1 rubygems

Version Path
activerecord-3.0.18 lib/active_record/relation/predicate_builder.rb
activerecord-3.0.17 lib/active_record/relation/predicate_builder.rb
activerecord-3.0.16 lib/active_record/relation/predicate_builder.rb
activerecord-3.0.15 lib/active_record/relation/predicate_builder.rb
activerecord-3.0.14 lib/active_record/relation/predicate_builder.rb