Skip to content

Commit 7ebba9b

Browse files
committed
Apply default sort to related_resource requests
(cherry picked from commit 600a818)
1 parent 2480458 commit 7ebba9b

File tree

4 files changed

+34
-14
lines changed

4 files changed

+34
-14
lines changed

lib/jsonapi/active_record_accessor.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,8 @@ def records_for_relationship(resource, relationship_name, options = {})
151151
end
152152

153153
sort_criteria = options.fetch(:sort_criteria, {})
154-
unless sort_criteria.nil? || sort_criteria.empty?
155-
order_options = relationship.resource_klass.construct_order_options(sort_criteria)
156-
records = apply_sort(records, order_options, context)
157-
end
154+
order_options = relationship.resource_klass.construct_order_options(sort_criteria)
155+
records = apply_sort(records, order_options, context)
158156

159157
paginator = options[:paginator]
160158
if paginator

test/controllers/controller_test.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1898,6 +1898,24 @@ def test_show_to_one_relationship_nil
18981898
}
18991899
}
19001900
end
1901+
1902+
def test_get_related_resources_sorted
1903+
assert_cacheable_get :get_related_resources, params: {person_id: '1', relationship: 'posts', source:'people', sort: 'title' }
1904+
assert_response :success
1905+
assert_equal 'JR How To', json_response['data'][0]['attributes']['title']
1906+
assert_equal 'New post', json_response['data'][2]['attributes']['title']
1907+
assert_cacheable_get :get_related_resources, params: {person_id: '1', relationship: 'posts', source:'people', sort: '-title' }
1908+
assert_response :success
1909+
assert_equal 'New post', json_response['data'][0]['attributes']['title']
1910+
assert_equal 'JR How To', json_response['data'][2]['attributes']['title']
1911+
end
1912+
1913+
def test_get_related_resources_default_sorted
1914+
assert_cacheable_get :get_related_resources, params: {person_id: '1', relationship: 'posts', source:'people'}
1915+
assert_response :success
1916+
assert_equal 'New post', json_response['data'][0]['attributes']['title']
1917+
assert_equal 'JR How To', json_response['data'][2]['attributes']['title']
1918+
end
19011919
end
19021920

19031921
class TagsControllerTest < ActionController::TestCase

test/fixtures/active_record.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,6 +1042,10 @@ class PostResource < JSONAPI::Resource
10421042
# Not needed - just for testing
10431043
primary_key :id
10441044

1045+
def self.default_sort
1046+
[{field: 'title', direction: :desc}, {field: 'id', direction: :desc}]
1047+
end
1048+
10451049
before_save do
10461050
msg = "Before save"
10471051
end

test/integration/requests/request_test.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -606,16 +606,16 @@ def test_pagination_empty_results
606606

607607

608608
def test_flow_self
609-
assert_cacheable_jsonapi_get '/posts'
610-
post_1 = json_response['data'][0]
609+
assert_cacheable_jsonapi_get '/posts/1'
610+
post_1 = json_response['data']
611611

612612
assert_cacheable_jsonapi_get post_1['links']['self']
613613
assert_hash_equals post_1, json_response['data']
614614
end
615615

616616
def test_flow_link_to_one_self_link
617-
assert_cacheable_jsonapi_get '/posts'
618-
post_1 = json_response['data'][0]
617+
assert_cacheable_jsonapi_get '/posts/1'
618+
post_1 = json_response['data']
619619

620620
assert_cacheable_jsonapi_get post_1['relationships']['author']['links']['self']
621621
assert_hash_equals(json_response, {
@@ -628,8 +628,8 @@ def test_flow_link_to_one_self_link
628628
end
629629

630630
def test_flow_link_to_many_self_link
631-
assert_cacheable_jsonapi_get '/posts'
632-
post_1 = json_response['data'][0]
631+
assert_cacheable_jsonapi_get '/posts/1'
632+
post_1 = json_response['data']
633633

634634
assert_cacheable_jsonapi_get post_1['relationships']['tags']['links']['self']
635635
assert_hash_equals(json_response,
@@ -647,10 +647,10 @@ def test_flow_link_to_many_self_link
647647
end
648648

649649
def test_flow_link_to_many_self_link_put
650-
assert_cacheable_jsonapi_get '/posts'
651-
post_1 = json_response['data'][4]
650+
assert_cacheable_jsonapi_get '/posts/5'
651+
post_5 = json_response['data']
652652

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

660660
assert_equal 204, status
661661

662-
assert_cacheable_jsonapi_get post_1['relationships']['tags']['links']['self']
662+
assert_cacheable_jsonapi_get post_5['relationships']['tags']['links']['self']
663663
assert_hash_equals(json_response,
664664
{
665665
'links' => {

0 commit comments

Comments
 (0)