class AdequateSerialization::Rails::RelationSerializer
Attributes
relation[R]
Public Class Methods
new(relation)
click to toggle source
# File lib/adequate_serialization/rails/relation_serializer.rb, line 8 def initialize(relation) @relation = relation end
Public Instance Methods
as_json(*options)
click to toggle source
# File lib/adequate_serialization/rails/relation_serializer.rb, line 12 def as_json(*options) # Very purposefully using #length here. If you use #empty? or #count and # the relation isn't yet loaded, it's going to trigger another query. # Since we're going to need to map over each of the results in order to # serialize it anyway, it's better just to load it now and avoid the # extra query. return [] if relation.length.zero? opts = Options.from(*options, multi_caching: true) decorator = Decorator.from(opts.attachments) response_for(opts, decorator) end
Private Instance Methods
cache_keys_for(opts)
click to toggle source
# File lib/adequate_serialization/rails/relation_serializer.rb, line 38 def cache_keys_for(opts) relation.map do |record| return nil unless CacheKey.cacheable?(record) CacheKey.for(record, opts.includes) end end
cacheable_response_for(opts, decorator, cache_keys)
click to toggle source
# File lib/adequate_serialization/rails/relation_serializer.rb, line 46 def cacheable_response_for(opts, decorator, cache_keys) results = ::Rails.cache.fetch_multi(*cache_keys) do |(record, *)| serialize(record, opts) end cache_keys.map do |cache_key| decorator.decorate(results[cache_key]) end end
response_for(opts, decorator)
click to toggle source
# File lib/adequate_serialization/rails/relation_serializer.rb, line 28 def response_for(opts, decorator) cache_keys = cache_keys_for(opts) if cache_keys cacheable_response_for(opts, decorator, cache_keys) else uncacheable_response_for(opts, decorator) end end
serialize(record, opts)
click to toggle source
# File lib/adequate_serialization/rails/relation_serializer.rb, line 63 def serialize(record, opts) record.class.serializer.serialize(record, opts) end
uncacheable_response_for(opts, decorator)
click to toggle source
# File lib/adequate_serialization/rails/relation_serializer.rb, line 57 def uncacheable_response_for(opts, decorator) relation.map do |record| decorator.decorate(serialize(record, opts)) end end