Skip to content

Commit 00ffaf5

Browse files
authored
Merge pull request #975 from senid231/make-apply-filter-respect-attribute-delegate
apply_filter and apply_sort respects :delegate option of attribute
2 parents 84f3863 + 49f0aee commit 00ffaf5

File tree

5 files changed

+48
-0
lines changed

5 files changed

+48
-0
lines changed

lib/jsonapi/active_record_accessor.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ def apply_sort(records, order_options, context = {})
225225
order_by_query = "#{associations.last.name}_sorting.#{column_name} #{direction}"
226226
records = records.joins(joins_query).order(order_by_query)
227227
else
228+
field = _resource_klass._attribute_delegated_name(field)
228229
records = records.order(field => direction)
229230
end
230231
end
@@ -274,6 +275,7 @@ def apply_filter(records, filter, value, options = {})
274275
records.where("#{_resource_klass._relationships[filter].table_name}.#{_resource_klass._relationships[filter].primary_key}" => value)
275276
end
276277
else
278+
filter = _resource_klass._attribute_delegated_name(filter)
277279
records.where(filter => value)
278280
end
279281
end

lib/jsonapi/resource.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,10 @@ def _attribute_options(attr)
757757
default_attribute_options.merge(@_attributes[attr])
758758
end
759759

760+
def _attribute_delegated_name(attr)
761+
@_attributes.fetch(attr.to_sym, {}).fetch(:delegate, attr)
762+
end
763+
760764
def _updatable_attributes
761765
_attributes.map { |key, options| key unless options[:readonly] }.compact
762766
end

test/controllers/controller_test.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3767,3 +3767,26 @@ def test_complex_includes_nested_things_secondary_users
37673767
assert_equal '2', json_response['included'][1]['relationships']['things']['data'][0]['id']
37683768
end
37693769
end
3770+
3771+
class BlogPostsControllerTest < ActionController::TestCase
3772+
def test_filter_by_delegated_attribute
3773+
assert_cacheable_get :index, params: {filter: {name: 'some title'}}
3774+
assert_response :success
3775+
end
3776+
3777+
def test_sorting_by_delegated_attribute
3778+
assert_cacheable_get :index, params: {sort: 'name'}
3779+
assert_response :success
3780+
end
3781+
3782+
def test_fields_with_delegated_attribute
3783+
original_config = JSONAPI.configuration.dup
3784+
JSONAPI.configuration.json_key_format = :underscored_key
3785+
3786+
assert_cacheable_get :index, params: {fields: {blog_posts: 'name'}}
3787+
assert_response :success
3788+
assert_equal ['name'], json_response['data'].first['attributes'].keys
3789+
ensure
3790+
JSONAPI.configuration = original_config
3791+
end
3792+
end

test/fixtures/active_record.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1851,6 +1851,24 @@ class FlatPostResource < JSONAPI::Resource
18511851
class FlatPostsController < JSONAPI::ResourceController
18521852
end
18531853

1854+
class BlogPost < ActiveRecord::Base
1855+
self.table_name = 'posts'
1856+
end
1857+
1858+
class BlogPostsController < JSONAPI::ResourceController
1859+
1860+
end
1861+
1862+
class BlogPostResource < JSONAPI::Resource
1863+
model_name 'BlogPost', add_model_hint: false
1864+
model_hint model: 'BlogPost', resource: BlogPostResource
1865+
1866+
attribute :name, :delegate => :title
1867+
attribute :body
1868+
1869+
filter :name
1870+
end
1871+
18541872
# CustomProcessors
18551873
class Api::V4::BookProcessor < JSONAPI::Processor
18561874
after_find do

test/test_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ class CatResource < JSONAPI::Resource
252252
jsonapi_resources :cars
253253
jsonapi_resources :boats
254254
jsonapi_resources :flat_posts
255+
jsonapi_resources :blog_posts
255256

256257
jsonapi_resources :books
257258
jsonapi_resources :authors

0 commit comments

Comments
 (0)