mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
rename context to serialization_context
This commit is contained in:
parent
e6d1d6be0c
commit
31172b1be5
@ -1,4 +1,5 @@
|
|||||||
require 'active_support/core_ext/class/attribute'
|
require 'active_support/core_ext/class/attribute'
|
||||||
|
require 'active_model_serializers/serialization_context'
|
||||||
|
|
||||||
module ActionController
|
module ActionController
|
||||||
module Serialization
|
module Serialization
|
||||||
@ -46,7 +47,7 @@ module ActionController
|
|||||||
|
|
||||||
[:_render_option_json, :_render_with_renderer_json].each do |renderer_method|
|
[:_render_option_json, :_render_with_renderer_json].each do |renderer_method|
|
||||||
define_method renderer_method do |resource, options|
|
define_method renderer_method do |resource, options|
|
||||||
options.fetch(:context) { options[:context] = request }
|
options.fetch(:serialization_context) { options[:serialization_context] = ActiveModelSerializers::SerializationContext.new(request) }
|
||||||
serializable_resource = get_serializer(resource, options)
|
serializable_resource = get_serializer(resource, options)
|
||||||
super(serializable_resource, options)
|
super(serializable_resource, options)
|
||||||
end
|
end
|
||||||
|
|||||||
@ -222,7 +222,7 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def pagination_links_for(serializer, options)
|
def pagination_links_for(serializer, options)
|
||||||
JsonApi::PaginationLinks.new(serializer.object, options[:context]).serializable_hash(options)
|
JsonApi::PaginationLinks.new(serializer.object, options[:serialization_context]).serializable_hash(options)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -41,11 +41,11 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def url(options)
|
def url(options)
|
||||||
@url ||= options.fetch(:links, {}).fetch(:self, nil) || original_url
|
@url ||= options.fetch(:links, {}).fetch(:self, nil) || request_url
|
||||||
end
|
end
|
||||||
|
|
||||||
def original_url
|
def request_url
|
||||||
@original_url ||= context.original_url[/\A[^?]+/]
|
@request_url ||= context.request_url
|
||||||
end
|
end
|
||||||
|
|
||||||
def query_parameters
|
def query_parameters
|
||||||
|
|||||||
10
lib/active_model_serializers/serialization_context.rb
Normal file
10
lib/active_model_serializers/serialization_context.rb
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
module ActiveModelSerializers
|
||||||
|
class SerializationContext
|
||||||
|
attr_reader :request_url, :query_parameters
|
||||||
|
|
||||||
|
def initialize(request)
|
||||||
|
@request_url = request.original_url[/\A[^?]+/]
|
||||||
|
@query_parameters = request.query_parameters
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
18
test/active_model_serializers/serialization_context_test.rb
Normal file
18
test/active_model_serializers/serialization_context_test.rb
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
require 'test_helper'
|
||||||
|
|
||||||
|
class ActiveModelSerializers::SerializationContextTest < Minitest::Test
|
||||||
|
def create_context
|
||||||
|
request = Minitest::Mock.new
|
||||||
|
request.expect(:original_url, 'original_url')
|
||||||
|
request.expect(:query_parameters, 'query_parameters')
|
||||||
|
|
||||||
|
ActiveModelSerializers::SerializationContext.new(request)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_create_context_with_request_url_and_query_parameters
|
||||||
|
context = create_context
|
||||||
|
|
||||||
|
assert_equal context.request_url, 'original_url'
|
||||||
|
assert_equal context.query_parameters, 'query_parameters'
|
||||||
|
end
|
||||||
|
end
|
||||||
@ -22,10 +22,10 @@ module ActiveModel
|
|||||||
|
|
||||||
def mock_request(query_parameters = {}, original_url = URI)
|
def mock_request(query_parameters = {}, original_url = URI)
|
||||||
context = Minitest::Mock.new
|
context = Minitest::Mock.new
|
||||||
context.expect(:original_url, original_url)
|
context.expect(:request_url, original_url)
|
||||||
context.expect(:query_parameters, query_parameters)
|
context.expect(:query_parameters, query_parameters)
|
||||||
@options = {}
|
@options = {}
|
||||||
@options[:context] = context
|
@options[:serialization_context] = context
|
||||||
end
|
end
|
||||||
|
|
||||||
def load_adapter(paginated_collection, options = {})
|
def load_adapter(paginated_collection, options = {})
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user