-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1550 from remear/url-helpers
Add Rails url_helpers
- Loading branch information
Showing
15 changed files
with
177 additions
and
52 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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,24 @@ | ||
module ActiveModelSerializers | ||
class SerializationContext | ||
class << self | ||
attr_writer :url_helpers, :default_url_options | ||
end | ||
|
||
attr_reader :request_url, :query_parameters | ||
|
||
def initialize(request) | ||
def initialize(request, options = {}) | ||
@request_url = request.original_url[/\A[^?]+/] | ||
@query_parameters = request.query_parameters | ||
@url_helpers = options.delete(:url_helpers) || self.class.url_helpers | ||
@default_url_options = options.delete(:default_url_options) || self.class.default_url_options | ||
end | ||
|
||
def self.url_helpers | ||
@url_helpers ||= Module.new | ||
end | ||
|
||
def self.default_url_options | ||
@default_url_options ||= {} | ||
end | ||
end | ||
end |
This file contains 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
18 changes: 0 additions & 18 deletions
18
test/active_model_serializers/serialization_context_test.rb
This file was deleted.
Oops, something went wrong.
58 changes: 58 additions & 0 deletions
58
test/active_model_serializers/serialization_context_test_isolated.rb
This file contains 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 |
---|---|---|
@@ -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 |
This file contains 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 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 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