mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
Merge pull request #1289 from tchak/serialization_context
rename context to serialization_context and add url helpers
This commit is contained in:
commit
a063cbea21
@ -1,4 +1,5 @@
|
||||
require 'active_support/core_ext/class/attribute'
|
||||
require 'action_controller/serialization/context'
|
||||
|
||||
module ActionController
|
||||
module Serialization
|
||||
@ -46,7 +47,7 @@ module ActionController
|
||||
|
||||
[:_render_option_json, :_render_with_renderer_json].each do |renderer_method|
|
||||
define_method renderer_method do |resource, options|
|
||||
options.fetch(:context) { options[:context] = request }
|
||||
options.fetch(:serialization_context) { options[:serialization_context] = Context.new(request) }
|
||||
serializable_resource = get_serializer(resource, options)
|
||||
super(serializable_resource, options)
|
||||
end
|
||||
|
||||
13
lib/action_controller/serialization/context.rb
Normal file
13
lib/action_controller/serialization/context.rb
Normal file
@ -0,0 +1,13 @@
|
||||
module ActionController
|
||||
module Serialization
|
||||
class Context
|
||||
attr_reader :request_url, :query_parameters, :url_helpers
|
||||
|
||||
def initialize(request)
|
||||
@request_url = request.original_url[/\A[^?]+/]
|
||||
@query_parameters = request.query_parameters
|
||||
@url_helpers = ActiveModelSerializers.url_helpers
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -206,7 +206,7 @@ module ActiveModel
|
||||
end
|
||||
|
||||
def 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
|
||||
|
||||
@ -41,11 +41,11 @@ module ActiveModel
|
||||
end
|
||||
|
||||
def url(options)
|
||||
@url ||= options.fetch(:links, {}).fetch(:self, nil) || original_url
|
||||
@url ||= options.fetch(:links, {}).fetch(:self, nil) || request_url
|
||||
end
|
||||
|
||||
def original_url
|
||||
@original_url ||= context.original_url[/\A[^?]+/]
|
||||
def request_url
|
||||
@request_url ||= context.request_url
|
||||
end
|
||||
|
||||
def query_parameters
|
||||
|
||||
@ -7,6 +7,18 @@ module ActiveModel
|
||||
end
|
||||
end
|
||||
|
||||
initializer 'active_model_serializers.url_helpers' do
|
||||
ActiveSupport.on_load(:action_controller) do
|
||||
ActiveModelSerializers.url_helpers = Module.new do
|
||||
include Rails.application.routes.url_helpers
|
||||
|
||||
def self.default_url_options
|
||||
ActionController::Base.default_url_options
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
initializer 'generators' do |app|
|
||||
app.load_generators
|
||||
require 'generators/serializer/resource_override'
|
||||
|
||||
@ -7,6 +7,8 @@ module ActiveModelSerializers
|
||||
mattr_accessor :logger
|
||||
self.logger = Rails.logger || Logger.new(IO::NULL)
|
||||
|
||||
mattr_accessor :url_helpers
|
||||
|
||||
extend ActiveSupport::Autoload
|
||||
autoload :Model
|
||||
|
||||
|
||||
@ -22,10 +22,10 @@ module ActiveModel
|
||||
|
||||
def mock_request(query_parameters = {}, original_url = URI)
|
||||
context = Minitest::Mock.new
|
||||
context.expect(:original_url, original_url)
|
||||
context.expect(:request_url, original_url)
|
||||
context.expect(:query_parameters, query_parameters)
|
||||
@options = {}
|
||||
@options[:context] = context
|
||||
@options[:serialization_context] = context
|
||||
end
|
||||
|
||||
def load_adapter(paginated_collection, options = {})
|
||||
|
||||
Loading…
Reference in New Issue
Block a user