Skip to content

Commit ee4ae07

Browse files
committed
Tests sorting on related attributes of has_one relationships
1 parent 9cdc890 commit ee4ae07

3 files changed

Lines changed: 45 additions & 0 deletions

File tree

test/fixtures/active_record.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -903,6 +903,9 @@ class IsoCurrenciesController < JSONAPI::ResourceController
903903
end
904904

905905
module V6
906+
class AuthorsController < JSONAPI::ResourceController
907+
end
908+
906909
class PostsController < JSONAPI::ResourceController
907910
end
908911

@@ -1753,6 +1756,10 @@ def self.find_records(filters, options = {})
17531756
def fetchable_fields
17541757
super - [:email]
17551758
end
1759+
1760+
def self.sortable_fields(context)
1761+
super(context) + [:"author_detail.author_stuff"]
1762+
end
17561763
end
17571764

17581765
class AuthorDetailResource < JSONAPI::Resource
@@ -1772,6 +1779,23 @@ class EmployeeResource < EmployeeResource; end
17721779

17731780
module Api
17741781
module V6
1782+
class AuthorDetailResource < JSONAPI::Resource
1783+
attributes :author_stuff
1784+
end
1785+
1786+
class AuthorResource < JSONAPI::Resource
1787+
attributes :name, :email
1788+
model_name 'Person'
1789+
relationship :posts, to: :many
1790+
relationship :author_detail, to: :one, foreign_key_on: :related
1791+
1792+
filter :name
1793+
1794+
def self.sortable_fields(context)
1795+
super(context) + [:"author_detail.author_stuff"]
1796+
end
1797+
end
1798+
17751799
class PersonResource < PersonResource; end
17761800
class TagResource < TagResource; end
17771801

test/integration/requests/request_test.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,6 +1100,26 @@ def test_sort_parameter_openquoted
11001100
assert_jsonapi_response 400
11011101
end
11021102

1103+
def test_sort_primary_attribute
1104+
get '/api/v6/authors?sort=name', headers: { 'Accept' => JSONAPI::MEDIA_TYPE }
1105+
assert_jsonapi_response 200
1106+
assert_equal '1002', json_response['data'][0]['id']
1107+
1108+
get '/api/v6/authors?sort=-name', headers: { 'Accept' => JSONAPI::MEDIA_TYPE }
1109+
assert_jsonapi_response 200
1110+
assert_equal '1005', json_response['data'][0]['id']
1111+
end
1112+
1113+
def test_sort_included_attribute
1114+
get '/api/v6/authors?sort=author_detail.author_stuff', headers: { 'Accept' => JSONAPI::MEDIA_TYPE }
1115+
assert_jsonapi_response 200
1116+
assert_equal '1000', json_response['data'][0]['id']
1117+
1118+
get '/api/v6/authors?sort=-author_detail.author_stuff', headers: { 'Accept' => JSONAPI::MEDIA_TYPE }
1119+
assert_jsonapi_response 200
1120+
assert_equal '1002', json_response['data'][0]['id']
1121+
end
1122+
11031123
def test_include_parameter_quoted
11041124
get '/api/v2/posts?include=%22author%22', headers: { 'Accept' => JSONAPI::MEDIA_TYPE }
11051125
assert_jsonapi_response 200

test/test_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ class CatResource < JSONAPI::Resource
354354

355355
JSONAPI.configuration.route_format = :dasherized_route
356356
namespace :v6 do
357+
jsonapi_resources :authors
357358
jsonapi_resources :posts
358359
jsonapi_resources :sections
359360
jsonapi_resources :customers

0 commit comments

Comments
 (0)