Skip to content

Commit

Permalink
Add config.relationship_links to toggle relationship link serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklandgrebe committed Oct 1, 2020
1 parent 9bb07a1 commit f6f3606
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 20 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -197,3 +197,4 @@
* Add `relationships_referenced` helper so that when `optimize_relationships` is enabled and relationships are changed as part of a persistence request (create, update), respond with the data for these relationships in the primary data instead of skipping them
* BREAKING: Use ActionController::API for Caprese::Controller
* Allow includes to overwrite past includes of same resource identifier
* Add config.relationship_links to toggle relationship link serialization
6 changes: 5 additions & 1 deletion lib/caprese/configuration.rb
Expand Up @@ -10,6 +10,7 @@ class Configuration
:max_page_size,
:only_path_links,
:optimize_relationships,
:relationship_links,
:resource_primary_key
)

Expand All @@ -34,6 +35,9 @@ def initialize
# If true, relationship data will not be serialized unless it is in `include`
@optimize_relationships = false

# If true, relationship links will be serialized
@relationship_links = true

# Defines the translation scope for model and controller errors
@i18n_scope = '' # 'api.v1.errors'

Expand All @@ -48,4 +52,4 @@ def initialize
@isolated_namespace = nil
end
end
end
end
40 changes: 21 additions & 19 deletions lib/caprese/serializer/concerns/relationships.rb
Expand Up @@ -81,27 +81,29 @@ def build_association_block(reflection_name)
reflection_name = reflection_name.to_sym

Proc.new do |serializer|
link :self do
url = "relationship_definition_#{serializer.version_name("#{serializer.unnamespace(object.class.name).underscore}_url")}"
if serializer.url_helpers.respond_to? url
serializer.url_helpers.send(
url,
id: object.read_attribute(Caprese.config.resource_primary_key),
relationship: reflection_name,
host: serializer.class.send(:caprese_default_url_options_host)
)
if Caprese.config.relationship_links
link :self do
url = "relationship_definition_#{serializer.version_name("#{serializer.unnamespace(object.class.name).underscore}_url")}"
if serializer.url_helpers.respond_to? url
serializer.url_helpers.send(
url,
id: object.read_attribute(Caprese.config.resource_primary_key),
relationship: reflection_name,
host: serializer.class.send(:caprese_default_url_options_host)
)
end
end
end

link :related do
url = "relationship_data_#{serializer.version_name("#{serializer.unnamespace(object.class.name).underscore}_url")}"
if serializer.url_helpers.respond_to? url
serializer.url_helpers.send(
url,
id: object.read_attribute(Caprese.config.resource_primary_key),
relationship: reflection_name,
host: serializer.class.send(:caprese_default_url_options_host)
)
link :related do
url = "relationship_data_#{serializer.version_name("#{serializer.unnamespace(object.class.name).underscore}_url")}"
if serializer.url_helpers.respond_to? url
serializer.url_helpers.send(
url,
id: object.read_attribute(Caprese.config.resource_primary_key),
relationship: reflection_name,
host: serializer.class.send(:caprese_default_url_options_host)
)
end
end
end

Expand Down

0 comments on commit f6f3606

Please sign in to comment.