From 9d9f166925e95176799a8e1ff3dfe233ce94e5ec Mon Sep 17 00:00:00 2001 From: John Allen Date: Thu, 16 May 2013 09:09:28 -0400 Subject: [PATCH] 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. --- lib/grape-active_model_serializers.rb | 1 + .../formatter.rb | 16 +----------- lib/grape/endpoint_extension.rb | 25 +++++++++++++++++++ 3 files changed, 27 insertions(+), 15 deletions(-) create mode 100644 lib/grape/endpoint_extension.rb diff --git a/lib/grape-active_model_serializers.rb b/lib/grape-active_model_serializers.rb index 1c1e1d4..7847623 100644 --- a/lib/grape-active_model_serializers.rb +++ b/lib/grape-active_model_serializers.rb @@ -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" diff --git a/lib/grape-active_model_serializers/formatter.rb b/lib/grape-active_model_serializers/formatter.rb index 824a681..693acb2 100644 --- a/lib/grape-active_model_serializers/formatter.rb +++ b/lib/grape-active_model_serializers/formatter.rb @@ -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) diff --git a/lib/grape/endpoint_extension.rb b/lib/grape/endpoint_extension.rb new file mode 100644 index 0000000..944b836 --- /dev/null +++ b/lib/grape/endpoint_extension.rb @@ -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