mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
Merge pull request #666 from jastkand/include_url_helpers
Auto-include url_helpers for Rails-apps. Fixes #563
This commit is contained in:
commit
4a4bcdc57b
@ -6,5 +6,13 @@ module ActiveModel
|
||||
require 'active_model/serializer/generators/serializer/scaffold_controller_generator'
|
||||
require 'active_model/serializer/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
|
||||
|
||||
ActiveSupport.run_load_hooks(:active_model_serializers, ActiveModel::Serializer)
|
||||
|
||||
@ -3,6 +3,9 @@ class TestApp < Rails::Application
|
||||
config.eager_load = false
|
||||
config.secret_key_base = 'abc123'
|
||||
end
|
||||
config.after_initialize do
|
||||
Rails.application.routes.default_url_options = { host: 'http://example.com' }
|
||||
end
|
||||
|
||||
# Set up a logger to avoid creating a log directory on every run.
|
||||
config.logger = Logger.new(nil)
|
||||
|
||||
35
test/unit/active_model/serializer/url_helpers_test.rb
Normal file
35
test/unit/active_model/serializer/url_helpers_test.rb
Normal file
@ -0,0 +1,35 @@
|
||||
require 'test_helper'
|
||||
|
||||
module ActiveModel
|
||||
class Serializer
|
||||
class UrlHelpersTest < Minitest::Test
|
||||
include Rails.application.routes.url_helpers
|
||||
|
||||
def setup
|
||||
Object.const_set 'UserController', Class.new(ActionController::Base) do
|
||||
def show
|
||||
render text: 'profile'
|
||||
end
|
||||
end
|
||||
|
||||
Rails.application.routes.draw do
|
||||
get '/profile/:id', as: :profile, controller: :user, action: :show
|
||||
end
|
||||
end
|
||||
|
||||
def test_url_helpers_are_available
|
||||
serializer = Class.new(ActiveModel::Serializer) do
|
||||
attributes :url
|
||||
|
||||
def url
|
||||
profile_url(id: object.object_id)
|
||||
end
|
||||
end
|
||||
profile = Profile.new
|
||||
|
||||
assert_equal({ url: profile_url(id: profile.object_id) },
|
||||
serializer.new(profile).as_json)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Reference in New Issue
Block a user