Module: Sequel::Plugins::PgEagerAnyTypedArray::ClassMethods

Defined in:
lib/sequel/plugins/pg_eager_any_typed_array.rb

Constant Summary collapse

TRANSFORM =
proc do |values, ref|
  type = ref.send(:cached_fetch, :_pg_eager_any_typed_array_type) do
    key = ref.predicate_key
    next if key.is_a?(Array)

    while key.is_a?(SQL::QualifiedIdentifier)
      key = key.column
    end

    # :nocov:
    # many_to_pg_array association type does not need changes, as it
    # already converts the values to a typed postgres array, it does
    # not call the code that uses :eager_loading_predicate_transform.
    #
    # No association type that ships with Sequel can reach this code
    # unless it is one of these types, but external association types
    # could potentially reach it.
    sch = case ref[:type]
    # :nocov:
    when :many_to_one, :one_to_one, :one_to_many, :pg_array_to_many
      ref.associated_class.db_schema
    when :many_to_many, :one_through_one
      # Not compatible with the :join_table_db option, but that option
      # does not call into this code.
      Hash[ref.associated_class.db.schema(ref.join_table_source)]
    when :many_through_many, :one_through_many
      # Not compatible with the :separate_query_per_table option, but
      # that option does not call into this code.
      Hash[ref.associated_class.db.schema(ref[:through][0][:table])]
    end

    if sch && (sch = sch[key])
      sch[:db_type]
    end
  end

  if type
    Sequel.function(:ANY, Sequel.pg_array(values, type))
  else
    values
  end
end

Instance Method Summary collapse

Instance Method Details

#associate(type, name, opts = OPTS, &block) ⇒ Object

Set the :eager_loading_predicate_transform option if not already set

[View source]

83
84
85
86
87
88
89
90
91
# File 'lib/sequel/plugins/pg_eager_any_typed_array.rb', line 83

def associate(type, name, opts = OPTS, &block)
  res = super

  unless res.has_key?(:eager_loading_predicate_transform)
    res[:eager_loading_predicate_transform] = TRANSFORM
  end

  res
end