diff --git a/lib/jsonapi/relationship_builder.rb b/lib/jsonapi/relationship_builder.rb index 9c7364d2f..0b7dec5db 100644 --- a/lib/jsonapi/relationship_builder.rb +++ b/lib/jsonapi/relationship_builder.rb @@ -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 diff --git a/test/controllers/controller_test.rb b/test/controllers/controller_test.rb index 7bdf19c2d..0e7f7ed42 100644 --- a/test/controllers/controller_test.rb +++ b/test/controllers/controller_test.rb @@ -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 diff --git a/test/fixtures/active_record.rb b/test/fixtures/active_record.rb index b5c11d72d..da68b5fe4 100644 --- a/test/fixtures/active_record.rb +++ b/test/fixtures/active_record.rb @@ -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 diff --git a/test/integration/requests/request_test.rb b/test/integration/requests/request_test.rb index 31821f8fd..8a27de962 100644 --- a/test/integration/requests/request_test.rb +++ b/test/integration/requests/request_test.rb @@ -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, { @@ -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, @@ -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, @@ -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' => {