Skip to content

Commit ade6dde

Browse files
authored
Merge pull request #1106 from cerebris/pr/1061
Adds tests for 1061
2 parents 38757c6 + ee4ae07 commit ade6dde

6 files changed

Lines changed: 58 additions & 5 deletions

File tree

jsonapi-resources.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Gem::Specification.new do |spec|
2121

2222
spec.add_development_dependency 'bundler', '~> 1.5'
2323
spec.add_development_dependency 'rake'
24-
spec.add_development_dependency 'minitest'
24+
spec.add_development_dependency 'minitest', '~> 5.10', '!= 5.10.2'
2525
spec.add_development_dependency 'minitest-spec-rails'
2626
spec.add_development_dependency 'simplecov'
2727
spec.add_development_dependency 'pry'

lib/jsonapi/active_relation_resource_finder.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,12 @@ def _build_joins(associations)
423423
joins = []
424424

425425
associations.inject do |prev, current|
426-
joins << "LEFT JOIN #{current.table_name} AS #{current.name}_sorting ON #{current.name}_sorting.id = #{prev.table_name}.#{current.foreign_key}"
426+
if current.has_one?
427+
joins << "LEFT JOIN #{current.table_name} AS #{current.name}_sorting ON #{current.name}_sorting.#{current.foreign_key} = #{prev.table_name}.id"
428+
else
429+
joins << "LEFT JOIN #{current.table_name} AS #{current.name}_sorting ON #{current.name}_sorting.id = #{prev.table_name}.#{current.foreign_key}"
430+
end
431+
427432
current
428433
end
429434
joins.join("\n")

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

test/unit/resource/resource_test.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -360,12 +360,15 @@ def test_lookup_association_chain
360360
end
361361

362362
def test_build_joins
363-
model_names = %w(person posts parent_post author)
363+
model_names = %w(person posts parent_post author author_detail)
364364
associations = PostResource._lookup_association_chain(model_names)
365365
result = PostResource.send(:_build_joins, associations)
366366

367-
assert_equal "LEFT JOIN posts AS parent_post_sorting ON parent_post_sorting.id = posts.parent_post_id
368-
LEFT JOIN people AS author_sorting ON author_sorting.id = posts.author_id", result
367+
sql = "LEFT JOIN posts AS parent_post_sorting ON parent_post_sorting.parent_post_id = posts.id
368+
LEFT JOIN people AS author_sorting ON author_sorting.id = posts.author_id
369+
LEFT JOIN author_details AS author_detail_sorting ON author_detail_sorting.person_id = people.id"
370+
371+
assert_equal sql, result
369372
end
370373

371374
# ToDo: Implement relationship pagination

0 commit comments

Comments
 (0)