Class: BatchLoaderActiveRecord::AssociationManager
- Inherits:
-
Object
- Object
- BatchLoaderActiveRecord::AssociationManager
- Defined in:
- lib/batch_loader_active_record/association_manager.rb
Instance Attribute Summary collapse
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#reflection ⇒ Object
readonly
Returns the value of attribute reflection.
Instance Method Summary collapse
- #accessor_name ⇒ Object
- #belongs_to_batch_loader(instance) ⇒ Object
- #has_and_belongs_to_many_to_batch_loader(instance, instance_scope) ⇒ Object
- #has_many_to_batch_loader(instance, instance_scope) ⇒ Object
- #has_one_to_batch_loader(instance) ⇒ Object
-
#initialize(model:, reflection:) ⇒ AssociationManager
constructor
A new instance of AssociationManager.
- #polymorphic_belongs_to_batch_loader(instance) ⇒ Object
Constructor Details
#initialize(model:, reflection:) ⇒ AssociationManager
Returns a new instance of AssociationManager.
5 6 7 8 |
# File 'lib/batch_loader_active_record/association_manager.rb', line 5 def initialize(model:, reflection:) @model = model @reflection = reflection end |
Instance Attribute Details
#model ⇒ Object (readonly)
Returns the value of attribute model.
3 4 5 |
# File 'lib/batch_loader_active_record/association_manager.rb', line 3 def model @model end |
#reflection ⇒ Object (readonly)
Returns the value of attribute reflection.
3 4 5 |
# File 'lib/batch_loader_active_record/association_manager.rb', line 3 def reflection @reflection end |
Instance Method Details
#accessor_name ⇒ Object
10 11 12 |
# File 'lib/batch_loader_active_record/association_manager.rb', line 10 def accessor_name :"#{reflection.name}_lazy" end |
#belongs_to_batch_loader(instance) ⇒ Object
14 15 16 17 18 19 |
# File 'lib/batch_loader_active_record/association_manager.rb', line 14 def belongs_to_batch_loader(instance) foreign_key_value = instance.send(reflection.foreign_key) or return nil BatchLoader.for(foreign_key_value).batch(key: batch_key) do |foreign_key_values, loader| target_scope.where(id: foreign_key_values).each { |instance| loader.call(instance.id, instance) } end end |
#has_and_belongs_to_many_to_batch_loader(instance, instance_scope) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/batch_loader_active_record/association_manager.rb', line 65 def has_and_belongs_to_many_to_batch_loader(instance, instance_scope) custom_key = batch_key custom_key += [instance_scope.to_sql.hash] unless instance_scope.nil? BatchLoader.for(instance.id).batch(default_value: [], key: custom_key) do |model_ids, loader| instance_id_path = "#{reflection.join_table}.#{reflection.foreign_key}" relation_with_scope(instance_scope) .joins(habtm_join(reflection)) .where("#{reflection.join_table}.#{reflection.foreign_key} IN (?)", model_ids) .select("#{target_scope.table_name}.*, #{instance_id_path} AS _instance_id") .each do |instance| loader.call(instance.public_send(:_instance_id)) { |value| value << instance } end end end |
#has_many_to_batch_loader(instance, instance_scope) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/batch_loader_active_record/association_manager.rb', line 46 def has_many_to_batch_loader(instance, instance_scope) custom_key = batch_key custom_key += [instance_scope.to_sql.hash] unless instance_scope.nil? BatchLoader.for(instance.id).batch(default_value: [], key: custom_key) do |model_ids, loader| relation = relation_with_scope(instance_scope) relation = relation.where(reflection.type => model.to_s) if reflection.type if reflection.through_reflection instances = fetch_for_model_ids(model_ids, relation: relation) instances.each do |instance| loader.call(instance.public_send(:_instance_id)) { |value| value << instance } end else relation.where(reflection.foreign_key => model_ids).each do |instance| loader.call(instance.public_send(reflection.foreign_key)) { |value| value << instance } end end end end |
#has_one_to_batch_loader(instance) ⇒ Object
36 37 38 39 40 41 42 43 44 |
# File 'lib/batch_loader_active_record/association_manager.rb', line 36 def has_one_to_batch_loader(instance) BatchLoader.for(instance.id).batch(key: batch_key) do |model_ids, loader| relation = target_scope.where(reflection.foreign_key => model_ids) relation = relation.where(reflection.type => model.to_s) if reflection.type relation.each do |instance| loader.call(instance.public_send(reflection.foreign_key), instance) end end end |
#polymorphic_belongs_to_batch_loader(instance) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/batch_loader_active_record/association_manager.rb', line 21 def polymorphic_belongs_to_batch_loader(instance) foreign_id = instance.send(reflection.foreign_key) or return nil foreign_type = instance.send(reflection.foreign_type)&.constantize or return nil BatchLoader.for([foreign_type, foreign_id]).batch(key: batch_key) do |foreign_ids_types, loader| foreign_ids_types .group_by(&:first) .each do |type, type_ids| ids = type_ids.map(&:second) klass_scope(type).where(id: ids).each do |instance| loader.call([type, instance.id], instance) end end end end |