Skip to content

Commit

Permalink
Add URL to serializers (closes rails-api#61)
Browse files Browse the repository at this point in the history
Thanks so much to @vanstee for the initial
implementation!
  • Loading branch information
wycats committed May 17, 2012
1 parent 43f32c8 commit 56b61b1
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/action_controller/serialization.rb
Expand Up @@ -49,6 +49,7 @@ def _render_option_json(json, options)

if serializer
options[:scope] = serialization_scope
options[:url_options] = url_options
json = serializer.new(json, options.merge(default_serializer_options || {}))
end
super
Expand Down
4 changes: 4 additions & 0 deletions lib/active_model/serializer.rb
Expand Up @@ -380,6 +380,10 @@ def initialize(object, options={})
@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)
Expand Down
6 changes: 6 additions & 0 deletions lib/active_model_serializers.rb
Expand Up @@ -10,6 +10,12 @@ class Railtie < Rails::Railtie
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
Expand Down
21 changes: 21 additions & 0 deletions test/serialization_test.rb
Expand Up @@ -54,6 +54,18 @@ def as_json(*)
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

Expand Down Expand Up @@ -124,6 +136,10 @@ def render_json_with_custom_serializer
render :json => [], :serializer => CustomSerializer
end

def render_json_with_links
render :json => HypermediaSerializable.new
end

private
def default_serializer_options
if params[:check_defaults]
Expand Down Expand Up @@ -229,4 +245,9 @@ def test_render_json_with_custom_serializer
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
6 changes: 6 additions & 0 deletions test/serializer_test.rb
Expand Up @@ -132,6 +132,12 @@ def test_serializer_receives_scope
}, 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
Expand Down
2 changes: 2 additions & 0 deletions test/test_helper.rb
Expand Up @@ -20,11 +20,13 @@
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
Expand Down

0 comments on commit 56b61b1

Please sign in to comment.