Skip to content

Commit 019fa02

Browse files
committed
Merge pull request #237 from cerebris/includes
Rework includes logic to JSON API 1.0
2 parents 359935e + fce59f3 commit 019fa02

7 files changed

Lines changed: 19 additions & 289 deletions

File tree

lib/jsonapi/acts_as_resource_controller.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@ def create_response_document(operation_results)
137137
operation_results,
138138
{
139139
primary_resource_klass: resource_klass,
140-
include: @request ? @request.include : nil,
141140
include_directives: @request ? @request.include_directives : nil,
142141
fields: @request ? @request.fields : nil,
143142
base_url: base_url,

lib/jsonapi/include_directives.rb

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module JSONAPI
22
class IncludeDirectives
33

44
# Construct an IncludeDirectives Hash from an array of dot separated include strings.
5-
# For example [:posts, 'posts.comments', 'posts.comments.tags']
5+
# For example ['posts.comments.tags']
66
# will transform into =>
77
# {
88
# :posts=>{
@@ -55,13 +55,12 @@ def get_includes(directive)
5555

5656
def parse_include(include)
5757
parts = include.split('.')
58-
local_path = ''
59-
parts.each_with_index do |part, index|
60-
local_path += local_path.length > 0 ? ".#{part}" : part
58+
local_path = ''
59+
60+
parts.each do |name|
61+
local_path += local_path.length > 0 ? ".#{name}" : name
6162
related = get_related(local_path)
62-
if index == parts.length - 1
63-
related[:include] = true
64-
end
63+
related[:include] = true
6564
end
6665
end
6766
end

lib/jsonapi/request.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ def initialize(params = nil, options = {})
1313
@errors = []
1414
@operations = []
1515
@fields = {}
16-
@include = []
1716
@filters = {}
1817
@sort_criteria = [{field: 'id', direction: :asc}]
1918
@source_klass = nil
@@ -199,13 +198,13 @@ def parse_include_directives(include)
199198
included_resources = CSV.parse_line(include)
200199
return if included_resources.nil?
201200

202-
@include = []
201+
include = []
203202
included_resources.each do |included_resource|
204203
check_include(@resource_klass, included_resource.partition('.'))
205-
@include.push(unformat_key(included_resource).to_s)
204+
include.push(unformat_key(included_resource).to_s)
206205
end
207206

208-
@include_directives = JSONAPI::IncludeDirectives.new(@include)
207+
@include_directives = JSONAPI::IncludeDirectives.new(include)
209208
end
210209

211210
def parse_filters(filters)

lib/jsonapi/response_document.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ def status
2929
def serializer
3030
@serializer ||= JSONAPI::ResourceSerializer.new(
3131
@options.fetch(:primary_resource_klass),
32-
include: @options.fetch(:include, []),
3332
include_directives: @options[:include_directives],
3433
fields: @options[:fields],
3534
base_url: @options.fetch(:base_url, ''),

test/controllers/controller_test.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1437,10 +1437,10 @@ def test_show_has_one_relationship_nil
14371437

14381438
class TagsControllerTest < ActionController::TestCase
14391439
def test_tags_index
1440-
get :index, {filter: {id: '6,7,8,9'}, include: 'posts,posts.tags,posts.author.posts'}
1440+
get :index, {filter: {id: '6,7,8,9'}, include: 'posts.tags,posts.author.posts'}
14411441
assert_response :success
14421442
assert_equal 4, json_response['data'].size
1443-
assert_equal 2, json_response['included'].size
1443+
assert_equal 3, json_response['included'].size
14441444
end
14451445

14461446
def test_tags_show_multiple
@@ -1450,7 +1450,7 @@ def test_tags_show_multiple
14501450
end
14511451

14521452
def test_tags_show_multiple_with_include
1453-
get :show, {id: '6,7,8,9', include: 'posts,posts.tags,posts.author.posts'}
1453+
get :show, {id: '6,7,8,9', include: 'posts.tags,posts.author.posts'}
14541454
assert_response :bad_request
14551455
assert_match /6,7,8,9 is not a valid value for id/, response.body
14561456
end

test/unit/serializer/include_directives_test.rb

Lines changed: 6 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ def test_one_level_multiple_includes
4141
directives)
4242
end
4343

44-
def test_two_levels_include_full
45-
directives = JSONAPI::IncludeDirectives.new(['posts', 'posts.comments']).include_directives
44+
def test_two_levels_include_full_path
45+
directives = JSONAPI::IncludeDirectives.new(['posts.comments']).include_directives
4646

4747
assert_hash_equals(
4848
{
@@ -61,14 +61,14 @@ def test_two_levels_include_full
6161
directives)
6262
end
6363

64-
def test_two_levels_include_lowest_only
65-
directives = JSONAPI::IncludeDirectives.new(['posts.comments']).include_directives
64+
def test_two_levels_include_full_path_redundant
65+
directives = JSONAPI::IncludeDirectives.new(['posts','posts.comments']).include_directives
6666

6767
assert_hash_equals(
6868
{
6969
include_related: {
7070
posts: {
71-
include: false,
71+
include: true,
7272
include_related:{
7373
comments: {
7474
include: true,
@@ -82,7 +82,7 @@ def test_two_levels_include_lowest_only
8282
end
8383

8484
def test_three_levels_include_full
85-
directives = JSONAPI::IncludeDirectives.new(['posts', 'posts.comments', 'posts.comments.tags']).include_directives
85+
directives = JSONAPI::IncludeDirectives.new(['posts.comments.tags']).include_directives
8686

8787
assert_hash_equals(
8888
{
@@ -105,33 +105,4 @@ def test_three_levels_include_full
105105
},
106106
directives)
107107
end
108-
109-
def test_three_levels_skip_middle
110-
directives = JSONAPI::IncludeDirectives.new(['posts', 'tags', 'posts.comments.tags']).include_directives
111-
112-
assert_hash_equals(
113-
{
114-
include_related: {
115-
tags: {
116-
include: true,
117-
include_related:{}
118-
},
119-
posts: {
120-
include: true,
121-
include_related:{
122-
comments: {
123-
include: false,
124-
include_related:{
125-
tags: {
126-
include: true,
127-
include_related:{}
128-
}
129-
}
130-
}
131-
}
132-
}
133-
}
134-
},
135-
directives)
136-
end
137108
end

0 commit comments

Comments
 (0)