mirror of
https://github.com/samsonjs/grape-active_model_serializers.git
synced 2026-03-25 08:45:55 +00:00
As in issue #13 an example solution provided by @jrhe an implementation of said feature has been created. As per the discussion in the thread this is only a helper to be able to reach the available options provided in the active_model_serializer gem. usage is as follows: ```ruby get '/some_path' do collection = Collection.all render collection, { meta: { current_page: 5 }, meta_key: :pagination_info } end ``` The return value would be: `{ pagination_info: { current_page: 5 }, collection: [item, item] }` If given without a `meta_key` it would return as: `{ meta: { current_page: 5 }, collection: [item, item] }` Any feedback appreciated. @zph, @olleolleolle and @bjoska
52 lines
1.2 KiB
Ruby
52 lines
1.2 KiB
Ruby
#
|
|
# Make the Grape::Endpoint quack like a ActionController
|
|
#
|
|
# This allows us to rely on the ActiveModel::Serializer#build_json method
|
|
# to lookup the approriate serializer.
|
|
#
|
|
module Grape
|
|
module EndpointExtension
|
|
attr_accessor :controller_name
|
|
|
|
def namespace_options
|
|
settings[:namespace] ? settings[:namespace].options : {}
|
|
end
|
|
|
|
def route_options
|
|
options[:route_options]
|
|
end
|
|
|
|
def self.included(base)
|
|
mattr_accessor :_serialization_scope
|
|
self._serialization_scope = :current_user
|
|
|
|
base.class_eval do
|
|
def serialization_scope
|
|
send(_serialization_scope) if _serialization_scope && respond_to?(_serialization_scope, true)
|
|
end
|
|
end
|
|
end
|
|
|
|
def render(resources, meta={})
|
|
set_meta_and_meta_key(meta)
|
|
resources
|
|
end
|
|
|
|
def default_serializer_options; end
|
|
|
|
def url_options; end
|
|
|
|
private
|
|
|
|
def set_meta_and_meta_key(meta)
|
|
if meta.has_key?(:meta)
|
|
Formatter::ActiveModelSerializers.meta = meta[:meta]
|
|
if meta.has_key?(:meta_key)
|
|
Formatter::ActiveModelSerializers.meta_key = meta[:meta_key]
|
|
end
|
|
end
|
|
end
|
|
|
|
end
|
|
Endpoint.send(:include, EndpointExtension)
|
|
end
|