Files
mozo-backend/config/initializers/active_model_serializer.rb
T
2025-09-20 17:35:58 -05:00

90 lines
3.3 KiB
Ruby

#ActiveModel::Serializer.setup do |config|
#config.embed = :ids
#config.embed_in_root = true
##config.adapter = :json_api
#end
#module ActiveModel::SerializerSupport
# def read_attribute_for_serialization(attr)
# public_send attr
# end
#end
module ActiveModel::SerializerSupport
#only because it is used
end
=begin
class Mozo::JsonAdapter < ActiveModel::Serializer::Adapter::JsonApi
def add_resource_relationships_old1(attrs, serializer, options = {})
options[:add_included] = options.fetch(:add_included, true)
serializer.associations.each do |association| #do |name, association, opts|
name = association.key
association_options = association.options
next unless object = serializer.object
if association_options[:type] == :has_many
# todo check for object["#{name}_ids"]
association_value = association_options[:association_options][:url] ? [] : serializer.send(name)
association_serializer_class = ActiveModel::Serializer.serializer_for(association_value, association_options)
else
# Only load if the record has to be included anyway or no id value can be found
if include_assoc?(name)
association_value = serializer.send(name)
else
association_id = object["#{name}_id"]
association_class= name.to_s.classify.safe_constantize
if association_id && association_class
association_value = association_class.new(id: association_id)
else
association_value = serializer.send(name)
end
end
association_serializer_class = ActiveModel::Serializer.serializer_for(association_value, association_options)
end
if association_serializer_class
association_serializer = association_serializer_class.new(
association_value,
options.except(:serializer).merge(serializer.serializer_from_options(association_options))
)
elsif !association_value.nil? && !association_value.instance_of?(Object)
association_options[:association_options][:virtual_value] = association_value
end
association_serializer = association.serializer
opts = association_options[:association_options]
opts = association.options
attrs[:relationships] ||= {}
binding.pry
if association_serializer.respond_to?(:each)
if opts[:url]
add_related(attrs, name, serializer.object, opts[:url])
else
add_relationships(attrs, name, association_serializer)
end
else
if opts[:virtual_value]
add_relationship(attrs, name, nil, opts[:virtual_value])
else
add_relationship(attrs, name, association_serializer)
end
end
if options[:add_included]
Array(association_serializer).each do |association|
add_included(name, association)
end
end
end
end
def add_related(resource, name, record, related)
related_url = related.is_a?(Proc) ? related.call(record) : related
resource[:relationships] ||= {}
resource[:relationships][name] ||= { links: {} }
resource[:relationships][name][:links][:related] = related_url
end
end
=end
#ActiveModel::Serializer.config.adapter = :json_api
#ActiveModel::Serializer.config.adapter = Mozo::JsonAdapter
#ActiveModel::Serializer.config.adapter = :flatten_json