Module: AmsLazyRelationships::Core::LazyDigMethod
- Defined in:
- lib/ams_lazy_relationships/core/lazy_dig_method.rb
Overview
Provides ‘lazy_dig` as an instance method for serializers, in order to make possible to dig relationships in depth just like `Hash#dig` do, keeping the laziness and N+1-free evaluation.
Instance Method Summary collapse
-
#lazy_dig(*relation_names) ⇒ ActiveRecord::Base, ...
ActiveRecord objects found by digging through the sequence of nested relationships.
Instance Method Details
#lazy_dig(*relation_names) ⇒ ActiveRecord::Base, ...
Returns ActiveRecord objects found by digging through the sequence of nested relationships. Singular or plural nature of returned value depends from the singular/plural nature of the chain of relation_names.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/ams_lazy_relationships/core/lazy_dig_method.rb', line 34 def lazy_dig(*relation_names) relationships = { multiple: false, data: [{ serializer: self.class, object: object }] } relation_names.each do |relation_name| lazy_dig_relationship!(relation_name, relationships) end objects = relationships[:data].map { |r| r[:object] } relationships[:multiple] ? objects : objects.first end |