Skip to content

Commit c544a06

Browse files
committed
Refine include_directives
1 parent 686f099 commit c544a06

File tree

4 files changed

+12
-17
lines changed

4 files changed

+12
-17
lines changed

lib/jsonapi/include_directives.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,6 @@ def [](name)
2929
@include_directives_hash[name]
3030
end
3131

32-
def include_directives_hash
33-
@include_directives_hash
34-
end
35-
3632
private
3733

3834
def parse_include(include)

lib/jsonapi/resource_serializer.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ def initialize(primary_resource_klass, options = {})
2222
@primary_resource_klass = primary_resource_klass
2323
@fields = options.fetch(:fields, {})
2424
@include = options.fetch(:include, [])
25-
@include_directives = options[:include_directives]
26-
@include_directives ||= JSONAPI::IncludeDirectives.new(@primary_resource_klass, @include)
25+
@include_directives = options.fetch(:include_directives,
26+
JSONAPI::IncludeDirectives.new(@primary_resource_klass, @include))
2727
@key_formatter = options.fetch(:key_formatter, JSONAPI.configuration.key_formatter)
2828
@id_formatter = ValueFormatter.value_formatter_for(:id)
2929
@link_builder = generate_link_builder(primary_resource_klass, options)
@@ -44,7 +44,7 @@ def initialize(primary_resource_klass, options = {})
4444

4545
# Converts a single resource, or an array of resources to a hash, conforming to the JSONAPI structure
4646
def serialize_to_hash(source)
47-
include_related = include_directives[:include_related] if include_directives
47+
include_related = include_directives[:include_related]
4848
resource_set = JSONAPI::ResourceSet.new(source, include_related, options)
4949
resource_set.populate!(self, options[:context], options)
5050

test/unit/processor/default_processor_test.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,5 +110,4 @@ def test_populated_resource_set_has_one_includes_relationships_are_resolved
110110
assert_equal 10, $populated_resource_set_has_one_includes.resource_klasses[PersonResource][1003][:relationships][:posts].first.id
111111
assert_equal 12, $populated_resource_set_has_one_includes.resource_klasses[PersonResource][1004][:relationships][:posts].first.id
112112
end
113-
114113
end

test/unit/serializer/include_directives_test.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
class IncludeDirectivesTest < ActiveSupport::TestCase
55

66
def test_one_level_one_include
7-
directives = JSONAPI::IncludeDirectives.new(PersonResource, ['posts']).include_directives_hash
7+
directives = JSONAPI::IncludeDirectives.new(PersonResource, ['posts']).instance_variable_get(:@include_directives_hash)
88

99
assert_hash_equals(
1010
{
@@ -18,7 +18,7 @@ def test_one_level_one_include
1818
end
1919

2020
def test_one_level_multiple_includes
21-
directives = JSONAPI::IncludeDirectives.new(PersonResource, ['posts', 'comments', 'expense_entries']).include_directives_hash
21+
directives = JSONAPI::IncludeDirectives.new(PersonResource, ['posts', 'comments', 'expense_entries']).instance_variable_get(:@include_directives_hash)
2222

2323
assert_hash_equals(
2424
{
@@ -38,7 +38,7 @@ def test_one_level_multiple_includes
3838
end
3939

4040
def test_multiple_level_multiple_includes
41-
directives = JSONAPI::IncludeDirectives.new(PersonResource, ['posts', 'posts.comments', 'comments', 'expense_entries']).include_directives_hash
41+
directives = JSONAPI::IncludeDirectives.new(PersonResource, ['posts', 'posts.comments', 'comments', 'expense_entries']).instance_variable_get(:@include_directives_hash)
4242

4343
assert_hash_equals(
4444
{
@@ -63,7 +63,7 @@ def test_multiple_level_multiple_includes
6363

6464

6565
def test_two_levels_include_full_path
66-
directives = JSONAPI::IncludeDirectives.new(PersonResource, ['posts.comments']).include_directives_hash
66+
directives = JSONAPI::IncludeDirectives.new(PersonResource, ['posts.comments']).instance_variable_get(:@include_directives_hash)
6767

6868
assert_hash_equals(
6969
{
@@ -81,7 +81,7 @@ def test_two_levels_include_full_path
8181
end
8282

8383
def test_two_levels_include_full_path_redundant
84-
directives = JSONAPI::IncludeDirectives.new(PersonResource, ['posts', 'posts.comments']).include_directives_hash
84+
directives = JSONAPI::IncludeDirectives.new(PersonResource, ['posts', 'posts.comments']).instance_variable_get(:@include_directives_hash)
8585

8686
assert_hash_equals(
8787
{
@@ -99,7 +99,7 @@ def test_two_levels_include_full_path_redundant
9999
end
100100

101101
def test_three_levels_include_full
102-
directives = JSONAPI::IncludeDirectives.new(PersonResource, ['posts.comments.tags']).include_directives_hash
102+
directives = JSONAPI::IncludeDirectives.new(PersonResource, ['posts.comments.tags']).instance_variable_get(:@include_directives_hash)
103103

104104
assert_hash_equals(
105105
{
@@ -127,19 +127,19 @@ def test_three_levels_include_full
127127
#
128128
def test_invalid_includes_1
129129
assert_raises JSONAPI::Exceptions::InvalidInclude do
130-
JSONAPI::IncludeDirectives.new(PersonResource, ['../../../../']).include_directives_hash
130+
JSONAPI::IncludeDirectives.new(PersonResource, ['../../../../']).instance_variable_get(:@include_directives_hash)
131131
end
132132
end
133133

134134
def test_invalid_includes_2
135135
assert_raises JSONAPI::Exceptions::InvalidInclude do
136-
JSONAPI::IncludeDirectives.new(PersonResource, ['posts./sdaa./........']).include_directives_hash
136+
JSONAPI::IncludeDirectives.new(PersonResource, ['posts./sdaa./........']).instance_variable_get(:@include_directives_hash)
137137
end
138138
end
139139

140140
def test_invalid_includes_3
141141
assert_raises JSONAPI::Exceptions::InvalidInclude do
142-
JSONAPI::IncludeDirectives.new(PersonResource, ['invalid../../../../']).include_directives_hash
142+
JSONAPI::IncludeDirectives.new(PersonResource, ['invalid../../../../']).instance_variable_get(:@include_directives_hash)
143143
end
144144
end
145145
end

0 commit comments

Comments
 (0)