mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
Add URL to serializers (closes #61)
Thanks so much to @vanstee for the initial implementation!
This commit is contained in:
@@ -54,6 +54,18 @@ class RenderJsonTest < ActionController::TestCase
|
||||
end
|
||||
end
|
||||
|
||||
class HypermediaSerializable
|
||||
def active_model_serializer
|
||||
HypermediaSerializer
|
||||
end
|
||||
end
|
||||
|
||||
class HypermediaSerializer < ActiveModel::Serializer
|
||||
def as_json(*)
|
||||
{ :link => hypermedia_url }
|
||||
end
|
||||
end
|
||||
|
||||
class TestController < ActionController::Base
|
||||
protect_from_forgery
|
||||
|
||||
@@ -124,6 +136,10 @@ class RenderJsonTest < ActionController::TestCase
|
||||
render :json => [], :serializer => CustomSerializer
|
||||
end
|
||||
|
||||
def render_json_with_links
|
||||
render :json => HypermediaSerializable.new
|
||||
end
|
||||
|
||||
private
|
||||
def default_serializer_options
|
||||
if params[:check_defaults]
|
||||
@@ -229,4 +245,9 @@ class RenderJsonTest < ActionController::TestCase
|
||||
get :render_json_with_custom_serializer
|
||||
assert_match '{"hello":true}', @response.body
|
||||
end
|
||||
|
||||
def test_render_json_with_links
|
||||
get :render_json_with_links
|
||||
assert_match '{"link":"http://www.nextangle.com/hypermedia"}', @response.body
|
||||
end
|
||||
end
|
||||
|
||||
@@ -132,6 +132,12 @@ class SerializerTest < ActiveModel::TestCase
|
||||
}, hash)
|
||||
end
|
||||
|
||||
def test_serializer_receives_url_options
|
||||
user = User.new
|
||||
user_serializer = UserSerializer.new(user, :url_options => { :host => "test.local" })
|
||||
assert_equal({ :host => "test.local" }, user_serializer.url_options)
|
||||
end
|
||||
|
||||
def test_pretty_accessors
|
||||
user = User.new
|
||||
user.superuser = true
|
||||
|
||||
@@ -20,11 +20,13 @@ require 'rails'
|
||||
module TestHelper
|
||||
Routes = ActionDispatch::Routing::RouteSet.new
|
||||
Routes.draw do
|
||||
resource :hypermedia
|
||||
match ':controller(/:action(/:id))'
|
||||
match ':controller(/:action)'
|
||||
end
|
||||
|
||||
ActionController::Base.send :include, Routes.url_helpers
|
||||
ActiveModel::Serializer.send :include, Routes.url_helpers
|
||||
end
|
||||
|
||||
ActiveSupport::TestCase.class_eval do
|
||||
|
||||
Reference in New Issue
Block a user