Skip to content

Commit 035fcfd

Browse files
SamMarkGoldmanchingor13
authored andcommitted
allows sparse fieldsets for multiple resources (#264)
1 parent a4f5a74 commit 035fcfd

File tree

3 files changed

+75
-7
lines changed

3 files changed

+75
-7
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,10 @@ article.title
213213
# should not have returned the created_at
214214
article.created_at
215215
# => raise NoMethodError
216+
217+
# or you can use fieldsets from multiple resources
218+
# makes request to /articles?fields[articles]=title,body&fields[comments]=tag
219+
article = Article.select("title", "body",{comments: 'tag'}).first
216220
```
217221

218222
## Sorting
@@ -485,7 +489,7 @@ class MyMoneyCaster
485489
end
486490
end
487491
end
488-
492+
489493
JsonApiClient::Schema.register money: MyMoneyCaster
490494

491495
```

lib/json_api_client/query/builder.rb

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,7 @@ def includes(*tables)
3535
end
3636

3737
def select(*fields)
38-
fields = Array(fields).flatten
39-
fields = fields.map { |i| i.to_s.split(",") }.flatten
40-
41-
@fields += fields.map(&:strip)
42-
38+
@fields += parse_fields(*fields)
4339
self
4440
end
4541

@@ -143,7 +139,23 @@ def order_params
143139
end
144140

145141
def select_params
146-
@fields.empty? ? {} : {fields: {klass.table_name => @fields.join(",")}}
142+
if @fields.empty?
143+
{}
144+
else
145+
field_result = Hash.new { |h,k| h[k] = [] }
146+
@fields.each do |field|
147+
if field.is_a? Hash
148+
field.each do |k,v|
149+
field_result[k.to_s] << v
150+
field_result[k.to_s] = field_result[k.to_s].flatten
151+
end
152+
else
153+
field_result[klass.table_name] << field
154+
end
155+
end
156+
field_result.each { |k,v| field_result[k] = v.join(',') }
157+
{fields: field_result}
158+
end
147159
end
148160

149161
def parse_related_links(*tables)
@@ -179,6 +191,21 @@ def parse_orders(*args)
179191
end.flatten
180192
end
181193

194+
def parse_fields(*fields)
195+
fields = fields.split(',') if fields.is_a? String
196+
fields.map do |field|
197+
case field
198+
when Hash
199+
field.each do |k,v|
200+
field[k] = parse_fields(v)
201+
end
202+
field
203+
else
204+
Array(field).flatten.map { |i| i.to_s.split(",") }.flatten.map(&:strip)
205+
end
206+
end.flatten
207+
end
208+
182209
end
183210
end
184211
end

test/unit/query_builder_test.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,41 @@ def test_can_select_fields_using_implicit_array
132132
Article.select(:title, :body).to_a
133133
end
134134

135+
def test_can_select_nested_fields_using_hashes
136+
stub_request(:get, "http://example.com/articles")
137+
.with(query: {fields: {articles: 'tags', comments: 'author'}})
138+
.to_return(headers: {content_type: "application/vnd.api+json"}, body: {
139+
data: []
140+
}.to_json)
141+
Article.select({comments: :author}, :tags).to_a
142+
end
143+
144+
145+
def test_can_select_nested_fields_using_hashes_of_arrays
146+
stub_request(:get, "http://example.com/articles")
147+
.with(query: {fields: {articles: 'tags', comments: 'author,text'}})
148+
.to_return(headers: {content_type: "application/vnd.api+json"}, body: {
149+
data: []
150+
}.to_json)
151+
Article.select({comments: [:author, :text]}, :tags).to_a
152+
end
153+
154+
def test_can_select_nested_fields_using_strings
155+
stub_request(:get, "http://example.com/articles")
156+
.with(query: {fields: {articles: 'tags', comments: 'author,text'}})
157+
.to_return(headers: {content_type: "application/vnd.api+json"}, body: {
158+
data: []
159+
}.to_json)
160+
Article.select({comments: ['author', 'text']}, :tags).to_a
161+
end
162+
163+
def test_can_select_nested_fields_using_comma_separated_strings
164+
stub_request(:get, "http://example.com/articles")
165+
.with(query: {fields: {articles: 'tags', comments: 'author,text'}})
166+
.to_return(headers: {content_type: "application/vnd.api+json"}, body: {
167+
data: []
168+
}.to_json)
169+
Article.select({comments: 'author,text'}, :tags).to_a
170+
end
171+
135172
end

0 commit comments

Comments
 (0)