JSONApi part1
This commit is contained in:
@@ -1,4 +1,78 @@
|
||||
ActiveModel::Serializer.setup do |config|
|
||||
config.embed = :ids
|
||||
config.embed_in_root = true
|
||||
#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
|
||||
|
||||
class Qwaiter::JsonAdapter < ActiveModel::Serializer::Adapter::JsonApi
|
||||
def add_resource_relationships(attrs, serializer, options = {})
|
||||
options[:add_included] = options.fetch(:add_included, true)
|
||||
serializer.class._associations.dup.each do |name, association_options| #do |name, association, opts|
|
||||
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
|
||||
opts = association_options[:association_options]
|
||||
attrs[:relationships] ||= {}
|
||||
|
||||
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
|
||||
#ActiveModel::Serializer.config.adapter = :json_api
|
||||
ActiveModel::Serializer.config.adapter = Qwaiter::JsonAdapter
|
||||
|
||||
@@ -82,6 +82,7 @@ Qwaiter::Application.routes.draw do
|
||||
collection do
|
||||
get :current
|
||||
end
|
||||
resources :orders, only: [:index]
|
||||
end
|
||||
|
||||
resources :orders, only: [:create]
|
||||
|
||||
Reference in New Issue
Block a user