Sha256: bf1ea41c466cf6721f7480d39bf91d5ec1b70479cc47522a45fe183fce8e6a37

Contents?: true

Size: 1.27 KB

Versions: 1

Compression:

Stored size: 1.27 KB

Contents

require 'encore/serializer/links_reflection_includer'

module Encore
  module Serializer
    class Base < ::ActiveModel::Serializer
      attributes :links

      def id
        object.id.to_s
      end

      def links
        object.reflections.each_with_object({}) do |(_, reflection), memo|
          if object.association(reflection.name).loaded?
            fetcher = LinksReflectionIncluder::Loaded
          else
            next unless self.class.can_access.include?(reflection.name)
            fetcher = LinksReflectionIncluder::NotLoaded
          end

          memo.merge!(reflection.name => fetcher.send("reflection_#{reflection.macro}", object, reflection))
        end
      end

      # Specify which resources the API can be included in the "linked" top-level key.
      def self.can_include
        []
      end

      # Specify which resources the API always include in the "linked" top-level key.
      def self.always_include
        []
      end

      # Specify which resources the API exposes URL to.
      # Default is can_include + always_include.
      def self.can_access
        can_include | always_include
      end

      def self.root_key
        model_class.name.pluralize.underscore.to_sym
      end

      def self.key_mappings
        {}
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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