diff --git a/lib/jsonapi/active_record_accessor.rb b/lib/jsonapi/active_record_accessor.rb index 37df6fa22..4607d5903 100644 --- a/lib/jsonapi/active_record_accessor.rb +++ b/lib/jsonapi/active_record_accessor.rb @@ -225,6 +225,7 @@ def apply_sort(records, order_options, context = {}) order_by_query = "#{associations.last.name}_sorting.#{column_name} #{direction}" records = records.joins(joins_query).order(order_by_query) else + field = _resource_klass._attribute_delegated_name(field) records = records.order(field => direction) end end @@ -274,6 +275,7 @@ def apply_filter(records, filter, value, options = {}) records.where("#{_resource_klass._relationships[filter].table_name}.#{_resource_klass._relationships[filter].primary_key}" => value) end else + filter = _resource_klass._attribute_delegated_name(filter) records.where(filter => value) end end diff --git a/lib/jsonapi/resource.rb b/lib/jsonapi/resource.rb index fac409177..b8520aa5c 100644 --- a/lib/jsonapi/resource.rb +++ b/lib/jsonapi/resource.rb @@ -755,6 +755,10 @@ def _attribute_options(attr) default_attribute_options.merge(@_attributes[attr]) end + def _attribute_delegated_name(attr) + @_attributes.fetch(attr.to_sym, {}).fetch(:delegate, attr) + end + def _updatable_attributes _attributes.map { |key, options| key unless options[:readonly] }.compact end diff --git a/test/controllers/controller_test.rb b/test/controllers/controller_test.rb index 2f4909f48..9764ed916 100644 --- a/test/controllers/controller_test.rb +++ b/test/controllers/controller_test.rb @@ -3757,3 +3757,26 @@ def test_complex_includes_nested_things_secondary_users assert_equal '2', json_response['included'][1]['relationships']['things']['data'][0]['id'] end end + +class BlogPostsControllerTest < ActionController::TestCase + def test_filter_by_delegated_attribute + assert_cacheable_get :index, params: {filter: {name: 'some title'}} + assert_response :success + end + + def test_sorting_by_delegated_attribute + assert_cacheable_get :index, params: {sort: 'name'} + assert_response :success + end + + def test_fields_with_delegated_attribute + original_config = JSONAPI.configuration.dup + JSONAPI.configuration.json_key_format = :underscored_key + + assert_cacheable_get :index, params: {fields: {blog_posts: 'name'}} + assert_response :success + assert_equal ['name'], json_response['data'].first['attributes'].keys + ensure + JSONAPI.configuration = original_config + end +end diff --git a/test/fixtures/active_record.rb b/test/fixtures/active_record.rb index 4ae8b37f2..04bfe0e7f 100644 --- a/test/fixtures/active_record.rb +++ b/test/fixtures/active_record.rb @@ -1801,6 +1801,24 @@ class FlatPostResource < JSONAPI::Resource class FlatPostsController < JSONAPI::ResourceController end +class BlogPost < ActiveRecord::Base + self.table_name = 'posts' +end + +class BlogPostsController < JSONAPI::ResourceController + +end + +class BlogPostResource < JSONAPI::Resource + model_name 'BlogPost', add_model_hint: false + model_hint model: 'BlogPost', resource: BlogPostResource + + attribute :name, :delegate => :title + attribute :body + + filter :name +end + # CustomProcessors class Api::V4::BookProcessor < JSONAPI::Processor after_find do diff --git a/test/test_helper.rb b/test/test_helper.rb index 256258a5a..5bb96c98a 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -252,6 +252,7 @@ class CatResource < JSONAPI::Resource jsonapi_resources :cars jsonapi_resources :boats jsonapi_resources :flat_posts + jsonapi_resources :blog_posts jsonapi_resources :books jsonapi_resources :authors