Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/jsonapi/active_relation_resource_finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -434,10 +434,11 @@ def _build_joins(associations)
associations.inject do |prev, current|
prev_table_name = _join_table_name(prev)
curr_table_name = _join_table_name(current)
relationship_primary_key = current.options.fetch(:primary_key, "id")
if current.belongs_to?
joins << "LEFT JOIN #{current.table_name} AS #{curr_table_name} ON #{curr_table_name}.id = #{prev_table_name}.#{current.foreign_key}"
joins << "LEFT JOIN #{current.table_name} AS #{curr_table_name} ON #{curr_table_name}.#{relationship_primary_key} = #{prev_table_name}.#{current.foreign_key}"
else
joins << "LEFT JOIN #{current.table_name} AS #{curr_table_name} ON #{curr_table_name}.#{current.foreign_key} = #{prev_table_name}.id"
joins << "LEFT JOIN #{current.table_name} AS #{curr_table_name} ON #{curr_table_name}.#{current.foreign_key} = #{prev_table_name}.#{relationship_primary_key}"
end

current
Expand Down
47 changes: 47 additions & 0 deletions test/controllers/widget_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require File.expand_path('../../test_helper', __FILE__)

def set_content_type_header!
@request.headers['Content-Type'] = JSONAPI::MEDIA_TYPE
end

class WidgetsControllerTest < ActionController::TestCase
def teardown
Widget.delete_all
Indicator.delete_all
Agency.delete_all
end

def test_fetch_widgets_sort_by_agency_name
agency_1 = Agency.create! name: 'beta'
agency_2 = Agency.create! name: 'alpha'
indicator_1 = Indicator.create! import_id: 'foobar', name: 'bar', agency: agency_1
indicator_2 = Indicator.create! import_id: 'foobar2', name: 'foo', agency: agency_2
Widget.create! name: 'bar', indicator: indicator_1
widget = Widget.create! name: 'foo', indicator: indicator_2
assert_cacheable_get :index, params: {sort: 'indicator.agency.name'}
assert_response :success
assert_equal widget.id.to_s, json_response['data'].first['id']
end
end

class IndicatorsControllerTest < ActionController::TestCase
def teardown
Widget.delete_all
Indicator.delete_all
Agency.delete_all
end

def test_fetch_indicators_sort_by_widgets_name
agency = Agency.create! name: 'test'
indicator_1 = Indicator.create! import_id: 'bar', name: 'bar', agency: agency
indicator_2 = Indicator.create! import_id: 'foo', name: 'foo', agency: agency
Widget.create! name: 'omega', indicator: indicator_1
Widget.create! name: 'beta', indicator: indicator_1
Widget.create! name: 'alpha', indicator: indicator_2
Widget.create! name: 'zeta', indicator: indicator_2
assert_cacheable_get :index, params: {sort: 'widgets.name'}
assert_response :success
assert_equal indicator_2.id.to_s, json_response['data'].first['id']
assert_equal 2, json_response['data'].size
end
end
11 changes: 6 additions & 5 deletions test/fixtures/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -341,13 +341,14 @@

create_table :indicators, force: true do |t|
t.string :name
t.string :import_id
t.integer :agency_id, null: false
t.timestamps null: false
end

create_table :widgets, force: true do |t|
t.string :name
t.integer :indicator_id, null: false
t.string :indicator_import_id, null: false
t.timestamps null: false
end

Expand Down Expand Up @@ -724,11 +725,11 @@ class Agency < ActiveRecord::Base

class Indicator < ActiveRecord::Base
belongs_to :agency
has_many :widgets
has_many :widgets, primary_key: :import_id, foreign_key: :indicator_import_id
end

class Widget < ActiveRecord::Base
belongs_to :indicator
belongs_to :indicator, primary_key: :import_id, foreign_key: :indicator_import_id
end

class Robot < ActiveRecord::Base
Expand Down Expand Up @@ -2046,7 +2047,7 @@ class AgencyResource < JSONAPI::Resource
class IndicatorResource < JSONAPI::Resource
attributes :name
has_one :agency
has_many :widgets
has_many :widgets, foreign_key: :indicator_import_id, primary_key: :import_id

def self.sortable_fields(_context = nil)
super + [:'widgets.name']
Expand All @@ -2055,7 +2056,7 @@ def self.sortable_fields(_context = nil)

class WidgetResource < JSONAPI::Resource
attributes :name
has_one :indicator
has_one :indicator, foreign_key: :indicator_import_id, primary_key: :import_id

def self.sortable_fields(_context = nil)
super + [:'indicator.agency.name']
Expand Down
2 changes: 1 addition & 1 deletion test/unit/resource/active_relation_resource_finder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class ARPostResource < JSONAPI::Resource
model_name 'Post'
attribute :headline, delegate: :title
has_one :author
has_many :tags
has_many :tags, primary_key: :tags_import_id
end

class ActiveRelationResourceFinderTest < ActiveSupport::TestCase
Expand Down