-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Controller namespace lookup. #1436
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7d0292c
Controller namespace lookup.
beauby 611f72e
Lazify map now that 1.9.3 support has been dropped.
beauby 4f57b7b
Add comments.
beauby b7c8cb4
Make serialization_context more versatile.
beauby fbaa8a6
Fix tests.
beauby 09cea47
Clean up SerializationContext.
beauby b20c7bc
Implement lookup mechanism described in RFC + fix tests.
beauby File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,15 @@ | ||
module ActiveModelSerializers | ||
class SerializationContext | ||
attr_reader :request_url, :query_parameters | ||
attr_reader :request_url, :query_parameters, :controller_namespace | ||
|
||
def initialize(request) | ||
@request_url = request.original_url[/\A[^?]+/] | ||
@query_parameters = request.query_parameters | ||
def initialize(options = {}) | ||
self.controller = options[:controller] if options.key?(:controller) | ||
end | ||
|
||
def controller=(controller) | ||
@request_url = controller.request.original_url[/\A[^?]+/] | ||
@query_parameters = controller.request.query_parameters | ||
@controller_namespace = controller.class.to_s.deconstantize | ||
end | ||
end | ||
end |
23 changes: 9 additions & 14 deletions
23
test/active_model_serializers/serialization_context_test.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,13 @@ | ||
require 'test_helper' | ||
|
||
class ActiveModelSerializers::SerializationContextTest < ActiveSupport::TestCase | ||
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' | ||
module ActiveModelSerializers | ||
class SerializationContextTest < ActionController::TestCase | ||
def test_create_context_with_request_url_and_query_parameters | ||
context = ActiveModelSerializers::SerializationContext.new(controller: self) | ||
|
||
assert_equal('http://test.host', context.request_url) | ||
assert_equal({}, context.query_parameters) | ||
assert_equal('ActiveModelSerializers', context.controller_namespace) | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -115,19 +115,55 @@ def test_serializer_for_namespaced_resource_with_lookup_disabled | |
assert_equal nil, serializer | ||
end | ||
|
||
def test_serializer_for_nested_resource | ||
comment = ResourceNamespace::Comment.new | ||
serializer = ResourceNamespace::PostSerializer.serializer_for(comment) | ||
assert_equal ResourceNamespace::PostSerializer::CommentSerializer, serializer | ||
end | ||
|
||
def test_serializer_for_nested_resource_with_lookup_disabled | ||
comment = ResourceNamespace::Comment.new | ||
serializer = with_serializer_lookup_disabled do | ||
ResourceNamespace::PostSerializer.serializer_for(comment) | ||
end | ||
assert_equal nil, serializer | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test did not make sense anymore with the new lookup convention. |
||
end | ||
|
||
module ::ResourceNamespace | ||
class Resource; end | ||
end | ||
|
||
def test_serializer_lookup_chain | ||
chain = ActiveModel::Serializer.serializer_lookup_chain_for(::ResourceNamespace::Resource) | ||
expected = ['::ResourceNamespace::ResourceSerializer'] | ||
assert_equal(expected, chain) | ||
end | ||
|
||
def test_serializer_lookup_chain_nested | ||
chain = ::PostSerializer.serializer_lookup_chain_for(::ResourceNamespace::Resource) | ||
expected = ['::PostSerializer::ResourceNamespace::ResourceSerializer', | ||
'::ResourceNamespace::ResourceSerializer'] | ||
assert_equal(expected, chain) | ||
end | ||
|
||
def test_serializer_lookup_chain_controller | ||
context = ActiveModelSerializers::SerializationContext.new | ||
context.instance_variable_set(:@controller_namespace, 'Api::V1') | ||
assert_equal('Api::V1', context.controller_namespace) | ||
|
||
chain = ActiveModel::Serializer.serializer_lookup_chain_for(::ResourceNamespace::Resource, context) | ||
expected = ['::Api::V1::ResourceNamespace::ResourceSerializer', | ||
'::Api::ResourceNamespace::ResourceSerializer', | ||
'::ResourceNamespace::ResourceSerializer'] | ||
assert_equal(expected, chain) | ||
end | ||
|
||
def test_serializer_lookup_chain_controller_nested | ||
context = ActiveModelSerializers::SerializationContext.new | ||
context.instance_variable_set(:@controller_namespace, 'Api::V1') | ||
assert_equal('Api::V1', context.controller_namespace) | ||
|
||
chain = ::PostSerializer.serializer_lookup_chain_for(::ResourceNamespace::Resource, context) | ||
expected = ['::PostSerializer::ResourceNamespace::ResourceSerializer', | ||
'::Api::V1::ResourceNamespace::ResourceSerializer', | ||
'::Api::ResourceNamespace::ResourceSerializer', | ||
'::ResourceNamespace::ResourceSerializer'] | ||
assert_equal(expected, chain) | ||
end | ||
end | ||
end | ||
end | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to see this broken up in to multiple lines