Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,10 @@ article.title
# should not have returned the created_at
article.created_at
# => raise NoMethodError

# or you can use fieldsets from multiple resources
# makes request to /articles?fields[articles]=title,body&fields[comments]=tag
article = Article.select("title", "body",{comments: 'tag'}).first
```

## Sorting
Expand Down Expand Up @@ -485,7 +489,7 @@ class MyMoneyCaster
end
end
end

JsonApiClient::Schema.register money: MyMoneyCaster

```
Expand Down
39 changes: 33 additions & 6 deletions lib/json_api_client/query/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,7 @@ def includes(*tables)
end

def select(*fields)
fields = Array(fields).flatten
fields = fields.map { |i| i.to_s.split(",") }.flatten

@fields += fields.map(&:strip)

@fields += parse_fields(*fields)
self
end

Expand Down Expand Up @@ -143,7 +139,23 @@ def order_params
end

def select_params
@fields.empty? ? {} : {fields: {klass.table_name => @fields.join(",")}}
if @fields.empty?
{}
else
field_result = Hash.new { |h,k| h[k] = [] }
@fields.each do |field|
if field.is_a? Hash
field.each do |k,v|
field_result[k.to_s] << v
field_result[k.to_s] = field_result[k.to_s].flatten
end
else
field_result[klass.table_name] << field
end
end
field_result.each { |k,v| field_result[k] = v.join(',') }
{fields: field_result}
end
end

def parse_related_links(*tables)
Expand Down Expand Up @@ -179,6 +191,21 @@ def parse_orders(*args)
end.flatten
end

def parse_fields(*fields)
fields = fields.split(',') if fields.is_a? String
fields.map do |field|
case field
when Hash
field.each do |k,v|
field[k] = parse_fields(v)
end
field
else
Array(field).flatten.map { |i| i.to_s.split(",") }.flatten.map(&:strip)
end
end.flatten
end

end
end
end
37 changes: 37 additions & 0 deletions test/unit/query_builder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,41 @@ def test_can_select_fields_using_implicit_array
Article.select(:title, :body).to_a
end

def test_can_select_nested_fields_using_hashes
stub_request(:get, "http://example.com/articles")
.with(query: {fields: {articles: 'tags', comments: 'author'}})
.to_return(headers: {content_type: "application/vnd.api+json"}, body: {
data: []
}.to_json)
Article.select({comments: :author}, :tags).to_a
end


def test_can_select_nested_fields_using_hashes_of_arrays
stub_request(:get, "http://example.com/articles")
.with(query: {fields: {articles: 'tags', comments: 'author,text'}})
.to_return(headers: {content_type: "application/vnd.api+json"}, body: {
data: []
}.to_json)
Article.select({comments: [:author, :text]}, :tags).to_a
end

def test_can_select_nested_fields_using_strings
stub_request(:get, "http://example.com/articles")
.with(query: {fields: {articles: 'tags', comments: 'author,text'}})
.to_return(headers: {content_type: "application/vnd.api+json"}, body: {
data: []
}.to_json)
Article.select({comments: ['author', 'text']}, :tags).to_a
end

def test_can_select_nested_fields_using_comma_separated_strings
stub_request(:get, "http://example.com/articles")
.with(query: {fields: {articles: 'tags', comments: 'author,text'}})
.to_return(headers: {content_type: "application/vnd.api+json"}, body: {
data: []
}.to_json)
Article.select({comments: 'author,text'}, :tags).to_a
end

end