Sha256: 41952d206f6b429eaab5176fcaa74ab98bc17c1a5fe794ce232950adc7f2f1dd

Contents?: true

Size: 928 Bytes

Versions: 1

Compression:

Stored size: 928 Bytes

Contents

require 'encore/serializer/utils'

module Encore
  module Serializer
    module LinkedResourceManager
      extend ActiveSupport::Concern

      def self.add(linked_ids, object)
        included_models = linked_ids.keys.map { |key| key.downcase }
        included_models << object.klass.name.downcase
        included_models << object.klass.name.downcase.pluralize

        linked_ids.reduce({}) do |memo, (model, ids)|
          klass = model.constantize
          serializer = Utils.fetch_serializer(klass)

          collection = klass.where(id: ids.to_a)
          available_includes = klass.reflections.map do |key, _|
            next unless included_models.include?(key.to_s)
            key
          end.compact

          collection = collection.includes(available_includes) unless available_includes.empty?
          memo.merge! MainResourceManager.add(collection, serializer)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
encore-0.2 lib/encore/serializer/linked_resource_manager.rb