Class: OccamsRecord::EagerLoaders::PolymorphicBelongsTo
- Inherits:
-
Object
- Object
- OccamsRecord::EagerLoaders::PolymorphicBelongsTo
- Defined in:
- lib/occams-record/eager_loaders/polymorphic_belongs_to.rb
Overview
Eager loader for polymorphic belongs tos
Instance Attribute Summary collapse
-
#eval_block ⇒ Proc
readonly
Optional Proc for eager loading things on this association.
-
#name ⇒ String
readonly
Association name.
-
#use ⇒ Array<Module>
readonly
Optional Module to include in the result class (single or array).
Instance Method Summary collapse
-
#initialize(ref, scope = nil, use: nil, as: 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.
-
#merge!(assoc_rows_of_type, rows) ⇒ Object
Merge associations of type N into rows of model N.
-
#query(rows) { ... } ⇒ Object
Yield ActiveRecord::Relations to the given block, one for every “type” represented in the given rows.
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.
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_block ⇒ Proc (readonly)
Returns 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 |
#name ⇒ String (readonly)
Returns association name.
6 7 8 |
# File 'lib/occams-record/eager_loaders/polymorphic_belongs_to.rb', line 6 def name @name end |
#use ⇒ Array<Module> (readonly)
Returns 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.
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 |