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
-
#name ⇒ String
readonly
Association name.
Instance Method Summary collapse
-
#initialize(ref, scope = nil, use: nil, as: nil, optimizer: 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.
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) { ... } ⇒ 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.
19 20 21 22 23 24 25 26 |
# File 'lib/occams-record/eager_loaders/polymorphic_belongs_to.rb', line 19 def initialize(ref, scope = nil, use: nil, as: nil, optimizer: nil, &builder) @ref, @scope, @use = ref, scope, use @name = (as || ref.name).to_s @foreign_type = @ref.foreign_type.to_sym @foreign_key = @ref.foreign_key.to_sym @eager_loaders = EagerLoaders::Context.new(nil, polymorphic: true) instance_exec(&builder) if builder end |
Instance Attribute Details
#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 |
Instance Method Details
#run(rows, query_logger: nil, measurements: nil) ⇒ Object
Run the query and merge the results into the given rows.
34 35 36 37 38 39 40 41 42 |
# File 'lib/occams-record/eager_loaders/polymorphic_belongs_to.rb', line 34 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).run merge! assoc_rows, rows } nil end |