Skip to content

Enable passing include_links and include_relationship_links to serializer instances#735

Open
mecampbellsoup wants to merge 1 commit intoJSONAPI-Resources:masterfrom
mecampbellsoup:include-links-options-in-resource-serializer
Open

Enable passing include_links and include_relationship_links to serializer instances#735
mecampbellsoup wants to merge 1 commit intoJSONAPI-Resources:masterfrom
mecampbellsoup:include-links-options-in-resource-serializer

Conversation

@mecampbellsoup
Copy link
Copy Markdown
Contributor

No description provided.

@mecampbellsoup mecampbellsoup deleted the include-links-options-in-resource-serializer branch June 10, 2016 17:54
@mecampbellsoup mecampbellsoup restored the include-links-options-in-resource-serializer branch June 10, 2016 19:32
@lgebhardt
Copy link
Copy Markdown
Contributor

@mecampbellsoup Did you mean to close this?

@mecampbellsoup
Copy link
Copy Markdown
Contributor Author

@lgebhardt woops! Sorrry 😄

@lgebhardt
Copy link
Copy Markdown
Contributor

@mecampbellsoup In the ActsAsResourceController module there's a serialization_options method. This gives you place in the controller to pass additional options into the ResourceSerializer as it's created during the standard request processing. I'd like to move you two options into this hash so these options can be used in the standard usage, which I think isn't possible as the PR stands now.

I think this will work:

    def initialize(primary_resource_klass, options = {})
      @primary_class_name = primary_resource_klass._type
      @fields             = options.fetch(:fields, {})
      @include            = options.fetch(:include, [])
      @include_directives = options[:include_directives]
      @key_formatter      = options.fetch(:key_formatter, JSONAPI.configuration.key_formatter)
      @id_formatter       = ValueFormatter.value_formatter_for(:id)
      @link_builder       = generate_link_builder(primary_resource_klass, options)
      @always_include_to_one_linkage_data = options.fetch(:always_include_to_one_linkage_data,
                                                          JSONAPI.configuration.always_include_to_one_linkage_data)
      @always_include_to_many_linkage_data = options.fetch(:always_include_to_many_linkage_data,
                                                           JSONAPI.configuration.always_include_to_many_linkage_data)
      @serialization_options = options.fetch(:serialization_options, {})
      @include_links      = @serialization_options.fetch(:include_links, [])
      @include_relationship_links = @serialization_options.fetch(:include_relationship_links, [])

      # Warning: This makes ResourceSerializer non-thread-safe. That's not a problem with the
      # request-specific way it's currently used, though.
      @value_formatter_type_cache = NaiveCache.new{|arg| ValueFormatter.value_formatter_for(arg) }
    end

Then these options can be added to a controller through the serialization_options method.

@mecampbellsoup
Copy link
Copy Markdown
Contributor Author

@lgebhardt okay I will work on this. By two options, you're referring to include_links and include_relationship_links correct?

@senid231
Copy link
Copy Markdown
Contributor

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