mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
Add URL to serializers (closes #61)
Thanks so much to @vanstee for the initial implementation!
This commit is contained in:
parent
43f32c868a
commit
56b61b1532
@ -49,6 +49,7 @@ module ActionController
|
||||
|
||||
if serializer
|
||||
options[:scope] = serialization_scope
|
||||
options[:url_options] = url_options
|
||||
json = serializer.new(json, options.merge(default_serializer_options || {}))
|
||||
end
|
||||
super
|
||||
|
||||
@ -380,6 +380,10 @@ module ActiveModel
|
||||
@object, @options = object, options
|
||||
end
|
||||
|
||||
def url_options
|
||||
@options[:url_options]
|
||||
end
|
||||
|
||||
# Returns a json representation of the serializable
|
||||
# object including the root.
|
||||
def as_json(options=nil)
|
||||
|
||||
@ -10,6 +10,12 @@ if defined?(Rails)
|
||||
Rails::Generators.configure!(app.config.generators)
|
||||
require "generators/resource_override"
|
||||
end
|
||||
|
||||
initializer "include_routes.active_model_serializer" do |app|
|
||||
ActiveSupport.on_load(:active_model_serializers) do
|
||||
include app.routes.url_helpers
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user