Class: OccamsRecord::EagerLoaders::PolymorphicBelongsTo

Inherits:
Object
  • Object
show all
Defined in:
lib/occams-record/eager_loaders/polymorphic_belongs_to.rb

Overview

Eager loader for polymorphic belongs tos

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ref, scope = nil, use: nil, as: nil) { ... } ⇒ PolymorphicBelongsTo

ActiveRecord::Relation on which you may call all the normal query hethods (select, where, etc) as well as any scopes you’ve defined on the model.

Parameters:

  • ref (ActiveRecord::Association)

    the ActiveRecord association

  • scope (Proc) (defaults to: nil)

    a scope to apply to the query (optional). It will be passed an

  • use (Array<Module>) (defaults to: nil)

    optional Module to include in the result class (single or array)

  • as (Symbol) (defaults to: nil)

    Load the association usign a different attribute name

Yields:

  • perform eager loading on this association (optional)



20
21
22
23
24
25
# File 'lib/occams-record/eager_loaders/polymorphic_belongs_to.rb', line 20

def initialize(ref, scope = nil, use: nil, as: nil, &eval_block)
  @ref, @scope, @use, @eval_block = ref, scope, use, eval_block
  @name = (as || ref.name).to_s
  @foreign_type = @ref.foreign_type.to_sym
  @foreign_key = @ref.foreign_key.to_sym
end

Instance Attribute Details

#eval_blockProc (readonly)

Returns optional Proc for eager loading things on this association.

Returns:

  • (Proc)

    optional Proc for eager loading things on this association



10
11
12
# File 'lib/occams-record/eager_loaders/polymorphic_belongs_to.rb', line 10

def eval_block
  @eval_block
end

#nameString (readonly)

Returns association name.

Returns:

  • (String)

    association name



6
7
8
# File 'lib/occams-record/eager_loaders/polymorphic_belongs_to.rb', line 6

def name
  @name
end

#useArray<Module> (readonly)

Returns optional Module to include in the result class (single or array).

Returns:

  • (Array<Module>)

    optional Module to include in the result class (single or array)



8
9
10
# File 'lib/occams-record/eager_loaders/polymorphic_belongs_to.rb', line 8

def use
  @use
end

Instance Method Details

#merge!(assoc_rows_of_type, rows) ⇒ Object

Merge associations of type N into rows of model N.



46
47
48
49
50
51
52
53
# File 'lib/occams-record/eager_loaders/polymorphic_belongs_to.rb', line 46

def merge!(assoc_rows_of_type, rows)
  return if assoc_rows_of_type.empty?
  type = assoc_rows_of_type[0].class.model_name
  rows_of_type = rows.select { |r| r.send(@foreign_type) == type }
  model = type.constantize
  Merge.new(rows_of_type, name).
    single!(assoc_rows_of_type, @ref.foreign_key.to_s, model.primary_key.to_s)
end

#query(rows) { ... } ⇒ Object

Yield ActiveRecord::Relations to the given block, one for every “type” represented in the given rows.

Parameters:

Yields:



33
34
35
36
37
38
39
40
41
# File 'lib/occams-record/eager_loaders/polymorphic_belongs_to.rb', line 33

def query(rows)
  rows_by_type = rows.group_by(&@foreign_type)
  rows_by_type.each do |type, rows_of_type|
    model = type.constantize
    ids = rows_of_type.map(&@foreign_key).uniq
    q = base_scope(model).where(model.primary_key => ids)
    yield q
  end
end