Class: OccamsRecord::EagerLoaders::PolymorphicBelongsTo
- Inherits:
-
Object
- Object
- OccamsRecord::EagerLoaders::PolymorphicBelongsTo
- Includes:
- Builder
- Defined in:
- lib/occams-record/eager_loaders/polymorphic_belongs_to.rb
Overview
Eager loader for polymorphic belongs tos
Instance Attribute Summary collapse
- #eager_loaders ⇒ OccamsRecord::EagerLoaders::Context readonly
-
#name ⇒ String
readonly
Association name.
-
#tracer ⇒ OccamsRecord::EagerLoaders::Tracer | nil
readonly
A reference to this eager loader and its parent (if any).
Instance Method Summary collapse
-
#initialize(ref, scope = nil, use: nil, as: nil, optimizer: nil, parent: nil, active_record_fallback: nil) { ... } ⇒ PolymorphicBelongsTo
constructor
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.
-
#run(rows, query_logger: nil, measurements: nil) ⇒ Object
Run the query and merge the results into the given rows.
-
#scope {|ActiveRecord::Relation| ... } ⇒ Object
An alternative to passing a “scope” lambda to the constructor.
Methods included from Builder
#eager_load, #eager_load_many, #eager_load_one, #nest
Constructor Details
#initialize(ref, scope = nil, use: nil, as: nil, optimizer: nil, parent: nil, active_record_fallback: 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.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/occams-record/eager_loaders/polymorphic_belongs_to.rb', line 27 def initialize(ref, scope = nil, use: nil, as: nil, optimizer: nil, parent: nil, active_record_fallback: nil, &builder) @ref, @scopes, @use = ref, Array(scope), use @name = (as || ref.name).to_s @foreign_type = @ref.foreign_type.to_sym @foreign_key = @ref.foreign_key.to_sym @tracer = Tracer.new(name, parent) @eager_loaders = EagerLoaders::Context.new(nil, tracer: @tracer, polymorphic: true) @active_record_fallback = active_record_fallback if builder if builder.arity > 0 builder.call(self) else instance_exec(&builder) end end end |
Instance Attribute Details
#eager_loaders ⇒ OccamsRecord::EagerLoaders::Context (readonly)
14 15 16 |
# File 'lib/occams-record/eager_loaders/polymorphic_belongs_to.rb', line 14 def eager_loaders @eager_loaders end |
#name ⇒ String (readonly)
Returns association name.
8 9 10 |
# File 'lib/occams-record/eager_loaders/polymorphic_belongs_to.rb', line 8 def name @name end |
#tracer ⇒ OccamsRecord::EagerLoaders::Tracer | nil (readonly)
Returns a reference to this eager loader and its parent (if any).
11 12 13 |
# File 'lib/occams-record/eager_loaders/polymorphic_belongs_to.rb', line 11 def tracer @tracer end |
Instance Method Details
#run(rows, query_logger: nil, measurements: nil) ⇒ Object
Run the query and merge the results into the given rows.
64 65 66 67 68 69 70 71 72 |
# File 'lib/occams-record/eager_loaders/polymorphic_belongs_to.rb', line 64 def run(rows, query_logger: nil, measurements: nil) query(rows) { |scope| eager_loaders = @eager_loaders.dup eager_loaders.model = scope.klass assoc_rows = Query.new(scope, use: @use, eager_loaders: eager_loaders, query_logger: query_logger, measurements: measurements, active_record_fallback: @active_record_fallback).run merge! assoc_rows, rows } nil end |
#scope {|ActiveRecord::Relation| ... } ⇒ Object
An alternative to passing a “scope” lambda to the constructor. Your block is passed the query so you can call select, where, order, etc on it.
If you call scope multiple times, the results will be additive.
53 54 55 56 |
# File 'lib/occams-record/eager_loaders/polymorphic_belongs_to.rb', line 53 def scope(&scope) @scopes << scope if scope self end |