Skip to content

Commit

Permalink
Support global exclude links configuration
Browse files Browse the repository at this point in the history
Closes gh-1256
  • Loading branch information
ouabing authored and lgebhardt committed Jun 26, 2019
1 parent 0a344a0 commit 34dc2c6
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 11 deletions.
11 changes: 10 additions & 1 deletion lib/jsonapi/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ class Configuration
:resource_cache,
:default_resource_cache_field,
:resource_cache_digest_function,
:resource_cache_usage_report_function
:resource_cache_usage_report_function,
:default_exclude_links

def initialize
#:underscored_key, :camelized_key, :dasherized_key, or custom
Expand Down Expand Up @@ -134,6 +135,12 @@ def initialize
# Optionally provide a callable which JSONAPI will call with information about cache
# performance. Should accept three arguments: resource name, hits count, misses count.
self.resource_cache_usage_report_function = nil

# Global configuration for links exclusion
# Controls whether to generate links like `self`, `related` with all the resources
# and relationships. Accepts either `:default`, `:none`, or array containing the
# specific default links to exclude, which may be `:self` and `:related`.
self.default_exclude_links = :none
end

def cache_formatters=(bool)
Expand Down Expand Up @@ -249,6 +256,8 @@ def default_processor_klass=(default_processor_klass)
attr_writer :resource_cache_digest_function

attr_writer :resource_cache_usage_report_function

attr_writer :default_exclude_links
end

class << self
Expand Down
3 changes: 1 addition & 2 deletions lib/jsonapi/relationship.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ def initialize(name, options = {})
@polymorphic = options.fetch(:polymorphic, false) == true
@always_include_linkage_data = options.fetch(:always_include_linkage_data, false) == true
@eager_load_on_include = options.fetch(:eager_load_on_include, true) == true

@_routed = false
@_warned_missing_route = false

exclude_links(options.fetch(:exclude_links, :none))
exclude_links(options.fetch(:exclude_links, JSONAPI.configuration.default_exclude_links))
end

alias_method :polymorphic?, :polymorphic
Expand Down
20 changes: 12 additions & 8 deletions lib/jsonapi/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1069,6 +1069,18 @@ def mutable?
end

def exclude_links(exclude)
_resolve_exclude_links(exclude)
end

def _exclude_links
@_exclude_links ||= _resolve_exclude_links(JSONAPI.configuration.default_exclude_links)
end

def exclude_link?(link)
_exclude_links.include?(link.to_sym)
end

def _resolve_exclude_links(exclude)
case exclude
when :default, "default"
@_exclude_links = [:self]
Expand All @@ -1081,14 +1093,6 @@ def exclude_links(exclude)
end
end

def _exclude_links
@_exclude_links ||= []
end

def exclude_link?(link)
_exclude_links.include?(link.to_sym)
end

def caching(val = true)
@caching = val
end
Expand Down
82 changes: 82 additions & 0 deletions test/unit/resource/relationship_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,88 @@ def test_polymorphic_type
assert_equal(relationship.polymorphic_type, "imageable_type")
end

def test_global_exclude_links_configuration_on_relationship
JSONAPI.configuration.default_exclude_links = :none
relationship = JSONAPI::Relationship::ToOne.new "foo"
assert_equal [], relationship._exclude_links
refute relationship.exclude_link?(:self)
refute relationship.exclude_link?("self")

JSONAPI.configuration.default_exclude_links = :default
relationship = JSONAPI::Relationship::ToOne.new "foo"
assert_equal [:self, :related], relationship._exclude_links
assert relationship.exclude_link?(:self)
assert relationship.exclude_link?("self")
assert relationship.exclude_link?(:related)
assert relationship.exclude_link?("related")

JSONAPI.configuration.default_exclude_links = "none"
relationship = JSONAPI::Relationship::ToOne.new "foo"
assert_equal [], relationship._exclude_links
refute relationship.exclude_link?(:self)
refute relationship.exclude_link?("self")

JSONAPI.configuration.default_exclude_links = "default"
relationship = JSONAPI::Relationship::ToOne.new "foo"
assert_equal [:self, :related], relationship._exclude_links
assert relationship.exclude_link?(:self)
assert relationship.exclude_link?("self")

JSONAPI.configuration.default_exclude_links = :none
relationship = JSONAPI::Relationship::ToOne.new "foo"
assert_equal [], relationship._exclude_links
refute relationship.exclude_link?(:self)
refute relationship.exclude_link?("self")

JSONAPI.configuration.default_exclude_links = [:self]
relationship = JSONAPI::Relationship::ToOne.new "foo"
assert_equal [:self], relationship._exclude_links
assert relationship.exclude_link?(:self)
assert relationship.exclude_link?("self")

JSONAPI.configuration.default_exclude_links = :none
relationship = JSONAPI::Relationship::ToOne.new "foo"
assert_equal [], relationship._exclude_links
refute relationship.exclude_link?(:self)
refute relationship.exclude_link?("self")

JSONAPI.configuration.default_exclude_links = ["self", :related]
relationship = JSONAPI::Relationship::ToOne.new "foo"
assert_equal [:self, :related], relationship._exclude_links
assert relationship.exclude_link?(:self)
assert relationship.exclude_link?("self")

JSONAPI.configuration.default_exclude_links = []
relationship = JSONAPI::Relationship::ToOne.new "foo"
assert_equal [], relationship._exclude_links
refute relationship.exclude_link?(:self)
refute relationship.exclude_link?("self")

assert_raises do
JSONAPI.configuration.default_exclude_links = :self
JSONAPI::Relationship::ToOne.new "foo"
end

# Test if the relationships will override the the global configuration
JSONAPI.configuration.default_exclude_links = :default
relationship = JSONAPI::Relationship::ToOne.new "foo", exclude_links: :none
assert_equal [], relationship._exclude_links
refute relationship.exclude_link?(:self)
refute relationship.exclude_link?("self")
refute relationship.exclude_link?(:related)
refute relationship.exclude_link?("related")

JSONAPI.configuration.default_exclude_links = :default
relationship = JSONAPI::Relationship::ToOne.new "foo", exclude_links: [:self]
assert_equal [:self], relationship._exclude_links
refute relationship.exclude_link?(:related)
refute relationship.exclude_link?("related")
assert relationship.exclude_link?(:self)
assert relationship.exclude_link?("self")
ensure
JSONAPI.configuration.default_exclude_links = :none
end

def test_exclude_links_on_relationship
relationship = JSONAPI::Relationship::ToOne.new "foo", exclude_links: :none
assert_equal [], relationship._exclude_links
Expand Down

0 comments on commit 34dc2c6

Please sign in to comment.