Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/action_controller/serialization.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'active_support/core_ext/class/attribute'
require 'action_controller/serialization/context'

module ActionController
module Serialization
Expand Down Expand Up @@ -46,7 +47,7 @@ def use_adapter?

[:_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
Expand Down
13 changes: 13 additions & 0 deletions lib/action_controller/serialization/context.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module ActionController
module Serialization
class Context
attr_reader :request_url, :query_parameters, :url_helpers
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'd rather have url_helpers a class method on the Context. It feels more natural here. Also, no need to include them. you could just

class Context
  class << self
    attr_accessor :url_helpers, :default_url_options
  end
end

and in the Railtie

Context.url_helpers = Rails.application.routes.url_helpers # for now, later would like to encapsulate it to control the boundary e.g. UrlHelpers.new(Rails.application.routes.url_helpers)
Context.default_url_options =  ActionController::Base.default_url_options

I'm trying to keep in mind how someone using grape would configure this. If we keep a good interface, the details of the implementation don't matter as much. That means more composition and less just mixing things in. Alternatively, there could be a UrlHelpers module that you mix in to the Context when the railtie is called, which allows others to mix in a different helper object

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm as far as I understand default_url_options needs to be defined on the same object where url_helpers are included in order to be taken in to account. Am I not understanding it correctly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also I usually use url helpers in serialisers a lot, so I am thinking that at some point I would like to be able to do in some serializer code something like this:

def validation_url
  context.validation_url(object)
end

or at least

def validation_url
  context.url_helpers.validation_url(object)
end

Are we on the same page here? Or do you see the usage differently?


def initialize(request)
@request_url = request.original_url[/\A[^?]+/]
@query_parameters = request.query_parameters
@url_helpers = ActiveModelSerializers.url_helpers
end
end
end
end
2 changes: 1 addition & 1 deletion lib/active_model/serializer/adapter/json_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ def add_included_resources_for(serializer, include_tree, primary_data, included)
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ def pages_from
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
Expand Down
12 changes: 12 additions & 0 deletions lib/active_model/serializer/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@ class Railtie < Rails::Railtie
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'
Expand Down
2 changes: 2 additions & 0 deletions lib/active_model_serializers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ module ActiveModelSerializers
mattr_accessor :logger
self.logger = Rails.logger || Logger.new(IO::NULL)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should really be set to Rails.logger in the Railtie, not here. womp womp (but that's on me)


mattr_accessor :url_helpers
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


extend ActiveSupport::Autoload
autoload :Model

Expand Down
4 changes: 2 additions & 2 deletions test/adapter/json_api/pagination_links_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ def setup

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 = {})
Expand Down