Skip to content

Commit 600a818

Browse files
committed
Apply default sort to related_resource requests
1 parent 5af1f17 commit 600a818

4 files changed

Lines changed: 34 additions & 14 deletions

File tree

lib/jsonapi/relationship_builder.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,8 @@ def define_resource_relationship_accessor(type, relationship_name)
6666
end
6767

6868
sort_criteria = options.fetch(:sort_criteria, {})
69-
unless sort_criteria.nil? || sort_criteria.empty?
70-
order_options = relationship.resource_klass.construct_order_options(sort_criteria)
71-
records = resource_klass.apply_sort(records, order_options, @context)
72-
end
69+
order_options = relationship.resource_klass.construct_order_options(sort_criteria)
70+
records = resource_klass.apply_sort(records, order_options, @context)
7371

7472
paginator = options[:paginator]
7573
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
@@ -1050,6 +1050,10 @@ class PostResource < JSONAPI::Resource
10501050
# Not needed - just for testing
10511051
primary_key :id
10521052

1053+
def self.default_sort
1054+
[{field: 'title', direction: :desc}, {field: 'id', direction: :desc}]
1055+
end
1056+
10531057
before_save do
10541058
msg = "Before save"
10551059
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)