Skip to content

Commit

Permalink
Merge pull request #990 from lgebhardt/fix_sorting_related_resources
Browse files Browse the repository at this point in the history
Apply default sort to related_resource requests
  • Loading branch information
lgebhardt authored Feb 23, 2017
2 parents 5af1f17 + 600a818 commit b1277ab
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 14 deletions.
6 changes: 2 additions & 4 deletions lib/jsonapi/relationship_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,8 @@ def define_resource_relationship_accessor(type, relationship_name)
end

sort_criteria = options.fetch(:sort_criteria, {})
unless sort_criteria.nil? || sort_criteria.empty?
order_options = relationship.resource_klass.construct_order_options(sort_criteria)
records = resource_klass.apply_sort(records, order_options, @context)
end
order_options = relationship.resource_klass.construct_order_options(sort_criteria)
records = resource_klass.apply_sort(records, order_options, @context)

paginator = options[:paginator]
if paginator
Expand Down
18 changes: 18 additions & 0 deletions test/controllers/controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1898,6 +1898,24 @@ def test_show_to_one_relationship_nil
}
}
end

def test_get_related_resources_sorted
assert_cacheable_get :get_related_resources, params: {person_id: '1', relationship: 'posts', source:'people', sort: 'title' }
assert_response :success
assert_equal 'JR How To', json_response['data'][0]['attributes']['title']
assert_equal 'New post', json_response['data'][2]['attributes']['title']
assert_cacheable_get :get_related_resources, params: {person_id: '1', relationship: 'posts', source:'people', sort: '-title' }
assert_response :success
assert_equal 'New post', json_response['data'][0]['attributes']['title']
assert_equal 'JR How To', json_response['data'][2]['attributes']['title']
end

def test_get_related_resources_default_sorted
assert_cacheable_get :get_related_resources, params: {person_id: '1', relationship: 'posts', source:'people'}
assert_response :success
assert_equal 'New post', json_response['data'][0]['attributes']['title']
assert_equal 'JR How To', json_response['data'][2]['attributes']['title']
end
end

class TagsControllerTest < ActionController::TestCase
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,10 @@ class PostResource < JSONAPI::Resource
# Not needed - just for testing
primary_key :id

def self.default_sort
[{field: 'title', direction: :desc}, {field: 'id', direction: :desc}]
end

before_save do
msg = "Before save"
end
Expand Down
20 changes: 10 additions & 10 deletions test/integration/requests/request_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -606,16 +606,16 @@ def test_pagination_empty_results


def test_flow_self
assert_cacheable_jsonapi_get '/posts'
post_1 = json_response['data'][0]
assert_cacheable_jsonapi_get '/posts/1'
post_1 = json_response['data']

assert_cacheable_jsonapi_get post_1['links']['self']
assert_hash_equals post_1, json_response['data']
end

def test_flow_link_to_one_self_link
assert_cacheable_jsonapi_get '/posts'
post_1 = json_response['data'][0]
assert_cacheable_jsonapi_get '/posts/1'
post_1 = json_response['data']

assert_cacheable_jsonapi_get post_1['relationships']['author']['links']['self']
assert_hash_equals(json_response, {
Expand All @@ -628,8 +628,8 @@ def test_flow_link_to_one_self_link
end

def test_flow_link_to_many_self_link
assert_cacheable_jsonapi_get '/posts'
post_1 = json_response['data'][0]
assert_cacheable_jsonapi_get '/posts/1'
post_1 = json_response['data']

assert_cacheable_jsonapi_get post_1['relationships']['tags']['links']['self']
assert_hash_equals(json_response,
Expand All @@ -647,10 +647,10 @@ def test_flow_link_to_many_self_link
end

def test_flow_link_to_many_self_link_put
assert_cacheable_jsonapi_get '/posts'
post_1 = json_response['data'][4]
assert_cacheable_jsonapi_get '/posts/5'
post_5 = json_response['data']

post post_1['relationships']['tags']['links']['self'], params:
post post_5['relationships']['tags']['links']['self'], params:
{'data' => [{'type' => 'tags', 'id' => '10'}]}.to_json,
headers: {
'CONTENT_TYPE' => JSONAPI::MEDIA_TYPE,
Expand All @@ -659,7 +659,7 @@ def test_flow_link_to_many_self_link_put

assert_equal 204, status

assert_cacheable_jsonapi_get post_1['relationships']['tags']['links']['self']
assert_cacheable_jsonapi_get post_5['relationships']['tags']['links']['self']
assert_hash_equals(json_response,
{
'links' => {
Expand Down

0 comments on commit b1277ab

Please sign in to comment.