From f685c8cd127a566f6a65a9a70e19714112dfc9cc Mon Sep 17 00:00:00 2001 From: Benjamin Fleischer Date: Sun, 29 May 2016 16:21:45 -0500 Subject: [PATCH] Fix #1759, Grape integration, adds serialization_context - improves improves serialization_context to take options and not depend on a `request` object. - adds descriptive error on missing serialization_context. - Document overriding `CollectionSerializer#paginated?`. --- CHANGELOG.md | 2 ++ .../adapter/json_api/pagination_links.rb | 8 ++++- .../serialization_context.rb | 13 ++++++-- lib/grape/helpers/active_model_serializers.rb | 9 +++++ .../serialization_context_test_isolated.rb | 33 +++++++++++++------ .../adapter/json_api/pagination_links_test.rb | 11 +++++++ 6 files changed, 62 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5cb573f24..7fe7df63d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ Features: - [#1426](https://github.com/rails-api/active_model_serializers/pull/1426) Add ActiveModelSerializers.config.default_includes (@empact) Fixes: +- [#1754](https://github.com/rails-api/active_model_serializers/pull/1754) Fixes #1759, Grape integration, improves serialization_context + missing error message on pagination. Document overriding CollectionSerializer#paginated?. (@bf4) - [#1710](https://github.com/rails-api/active_model_serializers/pull/1710) Prevent association loading when `include_data` option is set to `false`. (@groyoh) - [#1747](https://github.com/rails-api/active_model_serializers/pull/1747) Improve jsonapi mime type registration for Rails 5 (@remear) diff --git a/lib/active_model_serializers/adapter/json_api/pagination_links.rb b/lib/active_model_serializers/adapter/json_api/pagination_links.rb index a07de69a9..6e0b51a0d 100644 --- a/lib/active_model_serializers/adapter/json_api/pagination_links.rb +++ b/lib/active_model_serializers/adapter/json_api/pagination_links.rb @@ -2,6 +2,7 @@ module ActiveModelSerializers module Adapter class JsonApi < Base class PaginationLinks + MissingSerializationContextError = Class.new(KeyError) FIRST_PAGE = 1 attr_reader :collection, :context @@ -9,7 +10,12 @@ class PaginationLinks def initialize(collection, adapter_options) @collection = collection @adapter_options = adapter_options - @context = adapter_options.fetch(:serialization_context) + @context = adapter_options.fetch(:serialization_context) do + fail MissingSerializationContextError, 'JsonApi::PaginationLinks requires a'\ + "ActiveModelSerializers::SerializationContext.\n"\ + "Please pass a ':serialization_context' option or override CollectionSerializer#paginated?"\ + "to return 'false'.".freeze + end end def as_json diff --git a/lib/active_model_serializers/serialization_context.rb b/lib/active_model_serializers/serialization_context.rb index c61a43b94..9ef604f2e 100644 --- a/lib/active_model_serializers/serialization_context.rb +++ b/lib/active_model_serializers/serialization_context.rb @@ -1,3 +1,4 @@ +require 'active_support/core_ext/array/extract_options' module ActiveModelSerializers class SerializationContext class << self @@ -22,9 +23,15 @@ def default_url_options attr_reader :request_url, :query_parameters, :key_transform - def initialize(request, options = {}) - @request_url = request.original_url[/\A[^?]+/] - @query_parameters = request.query_parameters + def initialize(*args) + options = args.extract_options! + if args.size == 1 + request = args.pop + options[:request_url] = request.original_url[/\A[^?]+/] + options[:query_parameters] = request.query_parameters + end + @request_url = options.delete(:request_url) + @query_parameters = options.delete(:query_parameters) @url_helpers = options.delete(:url_helpers) || self.class.url_helpers @default_url_options = options.delete(:default_url_options) || self.class.default_url_options end diff --git a/lib/grape/helpers/active_model_serializers.rb b/lib/grape/helpers/active_model_serializers.rb index 370c52d81..baaa166d8 100644 --- a/lib/grape/helpers/active_model_serializers.rb +++ b/lib/grape/helpers/active_model_serializers.rb @@ -1,4 +1,7 @@ # Helpers can be included in your Grape endpoint as: helpers Grape::Helpers::ActiveModelSerializers + +require 'active_model_serializers/serialization_context' + module Grape module Helpers module ActiveModelSerializers @@ -8,6 +11,12 @@ module ActiveModelSerializers # # Example: To include pagination meta data: render(posts, meta: { page: posts.page, total_pages: posts.total_pages }) def render(resource, active_model_serializer_options = {}) + active_model_serializer_options.fetch(:serialization_context) do + active_model_serializer_options[:serialization_context] = ::ActiveModelSerializers::SerializationContext.new( + original_url: request.url[/\A[^?]+/], + query_parameters: request.params + ) + end env[:active_model_serializer_options] = active_model_serializer_options resource end diff --git a/test/active_model_serializers/serialization_context_test_isolated.rb b/test/active_model_serializers/serialization_context_test_isolated.rb index 981d80752..5720e84a1 100644 --- a/test/active_model_serializers/serialization_context_test_isolated.rb +++ b/test/active_model_serializers/serialization_context_test_isolated.rb @@ -5,13 +5,19 @@ class SerializationContextTest < ActiveSupport::TestCase include ActiveSupport::Testing::Isolation - def create_request - request = Minitest::Mock.new - request.expect(:original_url, 'original_url') - request.expect(:query_parameters, 'query_parameters') - end - class WithRails < SerializationContextTest + def create_request + request = ActionDispatch::Request.new({}) + def request.original_url + 'http://example.com/articles?page=2' + end + + def request.query_parameters + { 'page' => 2 } + end + request + end + setup do require 'rails' require 'active_model_serializers' @@ -20,8 +26,8 @@ class WithRails < SerializationContextTest end test 'create context with request url and query parameters' do - assert_equal @context.request_url, 'original_url' - assert_equal @context.query_parameters, 'query_parameters' + assert_equal @context.request_url, 'http://example.com/articles' + assert_equal @context.query_parameters, 'page' => 2 end test 'url_helpers is set up for Rails url_helpers' do @@ -36,14 +42,21 @@ class WithRails < SerializationContextTest end class WithoutRails < SerializationContextTest + def create_request + { + request_url: 'http://example.com/articles', + query_parameters: { 'page' => 2 } + } + end + setup do require 'active_model_serializers/serialization_context' @context = ActiveModelSerializers::SerializationContext.new(create_request) end test 'create context with request url and query parameters' do - assert_equal @context.request_url, 'original_url' - assert_equal @context.query_parameters, 'query_parameters' + assert_equal @context.request_url, 'http://example.com/articles' + assert_equal @context.query_parameters, 'page' => 2 end test 'url_helpers is a module when Rails is not present' do diff --git a/test/adapter/json_api/pagination_links_test.rb b/test/adapter/json_api/pagination_links_test.rb index 071956ea6..46c6b56a7 100644 --- a/test/adapter/json_api/pagination_links_test.rb +++ b/test/adapter/json_api/pagination_links_test.rb @@ -161,6 +161,17 @@ def test_not_showing_pagination_links assert_equal expected_response_without_pagination_links, adapter.serializable_hash end + + def test_raises_descriptive_error_when_serialization_context_unset + render_options = { adapter: :json_api } + adapter = serializable(using_kaminari, render_options) + exception = assert_raises do + adapter.as_json + end + exception_class = ActiveModelSerializers::Adapter::JsonApi::PaginationLinks::MissingSerializationContextError + assert_equal exception_class, exception.class + assert_match(/CollectionSerializer#paginated\?/, exception.message) + end end end end