-
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.
- Loading branch information
Showing
9 changed files
with
129 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,16 @@ | ||
module ActiveModel | ||
class Railtie < Rails::Railtie | ||
initializer 'generators' do |app| | ||
initializer 'active_model_serializers.setup_generators' do |app| | ||
app.load_generators | ||
require 'active_model/serializer/generators/serializer/serializer_generator' | ||
require 'active_model/serializer/generators/serializer/scaffold_controller_generator' | ||
require 'active_model/serializer/generators/resource_override' | ||
end | ||
|
||
initializer 'active_model_serializers.include_url_helpers' do |app| | ||
ActiveSupport.on_load(:active_model_serializers) do | ||
::ActiveModel::Serializer::UrlGenerator.send :include, app.routes.url_helpers | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module ActiveModel | ||
class Serializer | ||
class UrlGenerator | ||
|
||
def initialize(current_url_options = {}) | ||
@current_url_options = current_url_options | ||
end | ||
|
||
def url_options | ||
@url_options ||= | ||
(CONFIG.default_url_options || {}).merge(@current_url_options) | ||
end | ||
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
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,49 @@ | ||
require 'test_helper' | ||
|
||
module ActiveModel | ||
class Serializer | ||
class UrlGeneratorTest < ActiveModel::TestCase | ||
def setup | ||
@post = Post.new(title: 'Hi', body: 'How are you?') | ||
@url_generator = UrlGenerator.new(host: 'example.com') | ||
end | ||
|
||
def test_url_generator_available | ||
serializer = HypermediaPostSerializer.new(@post, url_generator: @url_generator) | ||
serialized_post = serializer.as_json['hypermedia_post'] | ||
|
||
assert_equal(@url_generator, serializer.url_generator) | ||
assert_equal('http://example.com/post', serialized_post[:link]) | ||
end | ||
|
||
def test_url_generator_available_in_associations | ||
category = Category.new(name: 'Welcome', posts: [@post]) | ||
serializer = CategorySerializer.new(category, url_generator: @url_generator) | ||
serialized_post = serializer.associations[:posts].first | ||
|
||
assert_equal('http://example.com/post', serialized_post[:link]) | ||
end | ||
end | ||
|
||
class UrlGeneratorDefaultsTest < ActiveModel::TestCase | ||
def setup | ||
@post = Post.new(title: 'Hi', body: 'How are you?') | ||
@old_url_options = CONFIG.default_url_options | ||
@url_generator = UrlGenerator.new | ||
end | ||
|
||
def test_url_generator_uses_default_url_options_from_config | ||
CONFIG.default_url_options = {host: 'default.local'} | ||
serializer = HypermediaPostSerializer.new(@post, url_generator: @url_generator) | ||
serialized_post = serializer.as_json['hypermedia_post'] | ||
|
||
assert_equal('http://default.local/post', serialized_post[:link]) | ||
end | ||
|
||
def teardown | ||
CONFIG.default_url_options = @old_url_options | ||
end | ||
end | ||
end | ||
end | ||
|