Module: ActiveRecordLaxIncludes::Preloader
- Defined in:
- lib/activerecord_lax_includes.rb
Class Method Summary collapse
Instance Method Summary collapse
- #filtered_records_by_reflection(association) ⇒ Object
- #lax_includes_enabled? ⇒ Boolean
- #preload_hash_with_lax_include(association) ⇒ Object
- #records_by_reflection_with_lax_include(association) ⇒ Object
Class Method Details
.included(base) ⇒ Object
3 4 5 6 7 8 9 10 11 |
# File 'lib/activerecord_lax_includes.rb', line 3 def self.included(base) base.class_eval do alias_method :records_by_reflection_default, :records_by_reflection alias_method :records_by_reflection, :records_by_reflection_with_lax_include alias_method :preload_hash_default, :preload_hash alias_method :preload_hash, :preload_hash_with_lax_include end end |
Instance Method Details
#filtered_records_by_reflection(association) ⇒ Object
35 36 37 38 39 |
# File 'lib/activerecord_lax_includes.rb', line 35 def filtered_records_by_reflection(association) records.select do |record| record.class.reflections[association].present? end end |
#lax_includes_enabled? ⇒ Boolean
41 42 43 44 45 46 47 48 |
# File 'lib/activerecord_lax_includes.rb', line 41 def lax_includes_enabled? result = Thread.current[:active_record_lax_includes_enabled] if result.nil? result = Rails.configuration.respond_to?(:active_record_lax_includes_enabled) && Rails.configuration.active_record_lax_includes_enabled end result end |
#preload_hash_with_lax_include(association) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/activerecord_lax_includes.rb', line 13 def preload_hash_with_lax_include(association) if lax_includes_enabled? association.each do |parent, child| ActiveRecord::Associations::Preloader.new(records, parent, ).run associated_records = filtered_records_by_reflection(parent).map { |record| record.send(parent) }.flatten ActiveRecord::Associations::Preloader.new(associated_records, child).run end else preload_hash_default(association) end end |
#records_by_reflection_with_lax_include(association) ⇒ Object
25 26 27 28 29 30 31 32 33 |
# File 'lib/activerecord_lax_includes.rb', line 25 def records_by_reflection_with_lax_include(association) if lax_includes_enabled? filtered_records_by_reflection(association).group_by do |record| record.class.reflections[association] end else records_by_reflection_default(association) end end |