Moves Grape::Endpoint extension into a module

Adding methods into the Grape::Endpoint class that
ActiveModel::Serializers are relying on from ActionController as well as
a few convenience methods for Route and Namespace options. I believe we
can expand on some of the empty methods here to allow for higher (api)
level option setting and defaults. This is a first step in that
direction.
This commit is contained in:
John Allen 2013-05-16 09:09:28 -04:00
parent 7f02d8f0d5
commit 9d9f166925
3 changed files with 27 additions and 15 deletions

View file

@ -1,4 +1,5 @@
require 'active_model_serializers'
require 'grape'
require 'grape/endpoint_extension'
require "grape-active_model_serializers/version"
require "grape-active_model_serializers/formatter"

View file

@ -1,15 +1,5 @@
require 'active_record'
# Make the Grape::Endpoint quiack like a controller
module Grape
class Endpoint
def default_serializer_options; end
def serialization_scope; end
def _serialization_scope; end
def url_options; end
end
end
module Grape
module Formatter
module ActiveModelSerializers
@ -21,11 +11,7 @@ module Grape
def call(resource, env)
@endpoint = env["api.endpoint"]
namespace = endpoint.settings[:namespace]
namespace_options = namespace ? namespace.options : {}
route_options = endpoint.options[:route_options]
options = namespace_options.merge(route_options)
options = endpoint.namespace_options.merge(endpoint.route_options)
serializer = ::ActiveModel::Serializer.build_json(endpoint, resource, options)

View file

@ -0,0 +1,25 @@
#
# 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
def namespace_options
settings[:namespace] ? settings[:namespace].options : {}
end
def route_options
options[:route_options]
end
def default_serializer_options; end
def serialization_scope; end
def _serialization_scope; end
def url_options; end
def controller_name; end
end
Endpoint.send(:include, EndpointExtension)
end