Sha256: e526f1c62cb777121bf1953f3ae557b1bbb4d80060f2badfd1689edada0378ae
Contents?: true
Size: 1.87 KB
Versions: 2
Compression:
Stored size: 1.87 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 reflections = object.try(:_reflections) || object.reflections 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(*value) if value.any? @can_include = value.flatten else @can_include ||= [] end end # Specify which resources the API always include in the "linked" top-level key. def self.always_include(*value) if value.any? @always_include = value.flatten else @always_include ||= [] end end # Specify which resources the API exposes URL to. # Default is can_include + always_include. def self.can_access(*value) if value.any? @can_access = value.flatten else @can_access ||= can_include | always_include end end def self.root_key(value = nil) if value @root_key = value else @root_key ||= model_class.name.pluralize.underscore.to_sym end end def self.key_mappings(value = nil) if value @key_mappings = value else @key_mappings ||= {} end end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
encore-0.2.3 | lib/encore/serializer/base.rb |
encore-0.2.2 | lib/encore/serializer/base.rb |