mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
Provide Rails url_helpers via SerializationContext
This commit is contained in:
@@ -18,6 +18,12 @@ class RailtieTest < ActiveSupport::TestCase
|
||||
"ActionController::Serialization should be included in ActionController::Base, but isn't"
|
||||
end
|
||||
|
||||
test 'prepares url_helpers for SerializationContext' do
|
||||
assert ActiveModelSerializers::SerializationContext.url_helpers.respond_to? :url_for
|
||||
assert_equal Rails.application.routes.default_url_options,
|
||||
ActiveModelSerializers::SerializationContext.default_url_options
|
||||
end
|
||||
|
||||
test 'sets the ActiveModelSerializers.logger to Rails.logger' do
|
||||
refute_nil Rails.logger
|
||||
refute_nil ActiveModelSerializers.logger
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
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'
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,58 @@
|
||||
# Execute this test in isolation
|
||||
require 'support/isolated_unit'
|
||||
require 'minitest/mock'
|
||||
|
||||
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
|
||||
setup do
|
||||
require 'rails'
|
||||
require 'active_model_serializers'
|
||||
make_basic_app
|
||||
@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'
|
||||
end
|
||||
|
||||
test 'url_helpers is set up for Rails url_helpers' do
|
||||
assert_equal Module, ActiveModelSerializers::SerializationContext.url_helpers.class
|
||||
assert ActiveModelSerializers::SerializationContext.url_helpers.respond_to? :url_for
|
||||
end
|
||||
|
||||
test 'default_url_options returns Rails.application.routes.default_url_options' do
|
||||
assert_equal Rails.application.routes.default_url_options,
|
||||
ActiveModelSerializers::SerializationContext.default_url_options
|
||||
end
|
||||
end
|
||||
|
||||
class WithoutRails < SerializationContextTest
|
||||
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'
|
||||
end
|
||||
|
||||
test 'url_helpers is a module when Rails is not present' do
|
||||
assert_equal Module, ActiveModelSerializers::SerializationContext.url_helpers.class
|
||||
refute ActiveModelSerializers::SerializationContext.url_helpers.respond_to? :url_for
|
||||
end
|
||||
|
||||
test 'default_url_options return a Hash' do
|
||||
assert Hash, ActiveModelSerializers::SerializationContext.default_url_options.class
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -7,18 +7,24 @@ module ActiveModelSerializers
|
||||
LinkAuthor = Class.new(::Model)
|
||||
class LinkAuthorSerializer < ActiveModel::Serializer
|
||||
link :self do
|
||||
href "//example.com/link_author/#{object.id}"
|
||||
href "http://example.com/link_author/#{object.id}"
|
||||
meta stuff: 'value'
|
||||
end
|
||||
|
||||
link :other, '//example.com/resource'
|
||||
|
||||
link(:author) { link_author_url(object.id) }
|
||||
link(:link_authors) { url_for(controller: 'link_authors', action: 'index', only_path: false) }
|
||||
link(:posts) { link_author_posts_url(object.id) }
|
||||
link :resource, 'http://example.com/resource'
|
||||
link :yet_another do
|
||||
"//example.com/resource/#{object.id}"
|
||||
"http://example.com/resource/#{object.id}"
|
||||
end
|
||||
end
|
||||
|
||||
def setup
|
||||
Rails.application.routes.draw do
|
||||
resources :link_authors do
|
||||
resources :posts
|
||||
end
|
||||
end
|
||||
@post = Post.new(id: 1337, comments: [], author: nil)
|
||||
@author = LinkAuthor.new(id: 1337, posts: [@post])
|
||||
end
|
||||
@@ -29,7 +35,7 @@ module ActiveModelSerializers
|
||||
adapter: :json_api,
|
||||
links: {
|
||||
self: {
|
||||
href: '//example.com/posts',
|
||||
href: 'http://example.com/posts',
|
||||
meta: {
|
||||
stuff: 'value'
|
||||
}
|
||||
@@ -37,7 +43,7 @@ module ActiveModelSerializers
|
||||
}).serializable_hash
|
||||
expected = {
|
||||
self: {
|
||||
href: '//example.com/posts',
|
||||
href: 'http://example.com/posts',
|
||||
meta: {
|
||||
stuff: 'value'
|
||||
}
|
||||
@@ -68,13 +74,16 @@ module ActiveModelSerializers
|
||||
hash = serializable(@author, adapter: :json_api).serializable_hash
|
||||
expected = {
|
||||
self: {
|
||||
href: '//example.com/link_author/1337',
|
||||
href: 'http://example.com/link_author/1337',
|
||||
meta: {
|
||||
stuff: 'value'
|
||||
}
|
||||
},
|
||||
other: '//example.com/resource',
|
||||
yet_another: '//example.com/resource/1337'
|
||||
author: 'http://example.com/link_authors/1337',
|
||||
link_authors: 'http://example.com/link_authors',
|
||||
resource: 'http://example.com/resource',
|
||||
posts: 'http://example.com/link_authors/1337/posts',
|
||||
yet_another: 'http://example.com/resource/1337'
|
||||
}
|
||||
assert_equal(expected, hash[:data][:links])
|
||||
end
|
||||
|
||||
@@ -63,6 +63,7 @@ module TestHelpers
|
||||
# Set a fake logger to avoid creating the log directory automatically
|
||||
fake_logger = Logger.new(nil)
|
||||
config.logger = fake_logger
|
||||
Rails.application.routes.default_url_options = { host: 'example.com' }
|
||||
end
|
||||
@app.respond_to?(:secrets) && @app.secrets.secret_key_base = '3b7cd727ee24e8444053437c36cc66c4'
|
||||
|
||||
|
||||
@@ -10,6 +10,8 @@ class ActiveModelSerializers::RailsApplication < Rails::Application
|
||||
|
||||
config.action_controller.perform_caching = true
|
||||
ActionController::Base.cache_store = :memory_store
|
||||
|
||||
Rails.application.routes.default_url_options = { host: 'example.com' }
|
||||
end
|
||||
end
|
||||
ActiveModelSerializers::RailsApplication.initialize!
|
||||
|
||||
Reference in New Issue
Block a user