Skip to content

make serialization_context available in link #1976

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

Closed
wants to merge 2 commits 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
2 changes: 1 addition & 1 deletion lib/active_model_serializers/adapter/json_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ def relationships_for(serializer, requested_associations, include_slice)
# }.reject! {|_,v| v.nil? }
def links_for(serializer)
serializer._links.each_with_object({}) do |(name, value), hash|
result = Link.new(serializer, value).as_json
result = Link.new(serializer, value, instance_options).as_json
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hash[name] = result if result
end
end
Expand Down
5 changes: 3 additions & 2 deletions lib/active_model_serializers/adapter/json_api/link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@ class JsonApi
class Link
include SerializationContext::UrlHelpers

def initialize(serializer, value)
def initialize(serializer, value, adapter_options)
@_routes ||= nil # handles warning
# actionpack-4.0.13/lib/action_dispatch/routing/route_set.rb:417: warning: instance variable @_routes not initialized
@object = serializer.object
@scope = serializer.scope
@context = adapter_options[:serialization_context]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any reason you can't use the scope?

# Use the return value of the block unless it is nil.
if value.respond_to?(:call)
@value = instance_eval(&value)
Expand Down Expand Up @@ -76,7 +77,7 @@ def as_json

protected

attr_reader :object, :scope
attr_reader :object, :scope, :context
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def data_for(association)

def links_for(association)
association.links.each_with_object({}) do |(key, value), hash|
result = Link.new(parent_serializer, value).as_json
result = Link.new(parent_serializer, value, serializable_resource_options).as_json
hash[key] = result if result
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/active_model_serializers/serialization_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def default_url_options
end
end

attr_reader :request_url, :query_parameters, :key_transform
attr_reader :request_url, :query_parameters, :key_transform, :url_helpers

def initialize(*args)
options = args.extract_options!
Expand Down