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

make serialization_context available in link #1976

wants to merge 2 commits into from

Conversation

f-mer
Copy link

@f-mer f-mer commented Nov 16, 2016

Purpose

module MyEngine
  class Engine < Rails::Engine
    isolate_namespace MyEngine
  end
end
class MySerializer < ActiveModel::Serializer
  link :self do
    context.url_helpers.api_resource_url(object)
  end
end
module MyEngine
  class MyController < ApplicationController
    def index
      render json: @resources, url_helpers: my_engine
    end
  end
end

Makes it possible to build urls of engines with isolated namespace by passing the ActionDispatch::Routing::RoutesProxy (my_engine).

Changes

Pass serialization_context to link.

@mention-bot
Copy link

@f-mer, thanks for your PR! By analyzing the history of the files in this pull request, we identified @domitian, @sigmike and @bf4 to be potential reviewers.

@_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?

@@ -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.

@f-mer
Copy link
Author

f-mer commented Nov 28, 2016

Thanks for getting back at this! Yep that was the idea.
While exploring the problem it may be possible to just pass the view_context:

render json: @resources, scope: view_context, scope_name: :view_context

Links could be build like this:

link :self do
  view_context.api_resource_url(object)
end

and the current_user could also be accessed via view_context.current_user.
This would make the change obsolet. Have not tested this yet.

@bf4
Copy link
Member

bf4 commented Jan 10, 2017

@f-mer how's it going? Have you seen #1981

@f-mer
Copy link
Author

f-mer commented Jan 11, 2017

Closing this pull request because scope / view_context could be used.
Thank you heaps!

@f-mer f-mer closed this Jan 11, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants