Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Routes helpers available inside ArraySerializer #336

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/active_model/array_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ def initialize(object, options={})
@options = options
end

def url_options
@options[:url_options] || {}
end

def serialize_object
serializable_array
end
Expand Down
7 changes: 7 additions & 0 deletions lib/active_model_serializers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ class Railtie < Rails::Railtie
extend ::AbstractController::Railties::RoutesHelpers.with(app.routes)
include app.routes.mounted_helpers
end

ActiveSupport.on_load(:active_model_array_serializers) do
include AbstractController::UrlFor
extend ::AbstractController::Railties::RoutesHelpers.with(app.routes)
include app.routes.mounted_helpers
end
end

initializer "caching.active_model_serializer" do |app|
Expand Down Expand Up @@ -93,3 +99,4 @@ def active_model_serializer
end

ActiveSupport.run_load_hooks(:active_model_serializers, ActiveModel::Serializer)
ActiveSupport.run_load_hooks(:active_model_array_serializers, ActiveModel::ArraySerializer)
23 changes: 23 additions & 0 deletions test/array_serializer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,27 @@ def active_model_serializer
{ "value" => "something" }
], serializer.as_json)
end

def test_active_support_on_load_hooks_fired
loaded = nil
ActiveSupport.on_load(:active_model_array_serializers) do
loaded = self
end
assert_equal ActiveModel::ArraySerializer, loaded
end

def test_serializer_receives_url_options
array = []
options = {url_options: { host: "test.local" }}
serializer = array.active_model_serializer.new(array, options)

assert_equal({ host: "test.local" }, serializer.url_options)
end

def test_serializer_returns_empty_hash_without_url_options
array = []
serializer = array.active_model_serializer.new array

assert_equal({}, serializer.url_options)
end
end