Skip to content

Commit 49f0aee

Browse files
committed
apply_filter and apply_sort respects :delegate option of attribute
1 parent 3487650 commit 49f0aee

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
@@ -755,6 +755,10 @@ def _attribute_options(attr)
755755
default_attribute_options.merge(@_attributes[attr])
756756
end
757757

758+
def _attribute_delegated_name(attr)
759+
@_attributes.fetch(attr.to_sym, {}).fetch(:delegate, attr)
760+
end
761+
758762
def _updatable_attributes
759763
_attributes.map { |key, options| key unless options[:readonly] }.compact
760764
end

test/controllers/controller_test.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3757,3 +3757,26 @@ def test_complex_includes_nested_things_secondary_users
37573757
assert_equal '2', json_response['included'][1]['relationships']['things']['data'][0]['id']
37583758
end
37593759
end
3760+
3761+
class BlogPostsControllerTest < ActionController::TestCase
3762+
def test_filter_by_delegated_attribute
3763+
assert_cacheable_get :index, params: {filter: {name: 'some title'}}
3764+
assert_response :success
3765+
end
3766+
3767+
def test_sorting_by_delegated_attribute
3768+
assert_cacheable_get :index, params: {sort: 'name'}
3769+
assert_response :success
3770+
end
3771+
3772+
def test_fields_with_delegated_attribute
3773+
original_config = JSONAPI.configuration.dup
3774+
JSONAPI.configuration.json_key_format = :underscored_key
3775+
3776+
assert_cacheable_get :index, params: {fields: {blog_posts: 'name'}}
3777+
assert_response :success
3778+
assert_equal ['name'], json_response['data'].first['attributes'].keys
3779+
ensure
3780+
JSONAPI.configuration = original_config
3781+
end
3782+
end

test/fixtures/active_record.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1801,6 +1801,24 @@ class FlatPostResource < JSONAPI::Resource
18011801
class FlatPostsController < JSONAPI::ResourceController
18021802
end
18031803

1804+
class BlogPost < ActiveRecord::Base
1805+
self.table_name = 'posts'
1806+
end
1807+
1808+
class BlogPostsController < JSONAPI::ResourceController
1809+
1810+
end
1811+
1812+
class BlogPostResource < JSONAPI::Resource
1813+
model_name 'BlogPost', add_model_hint: false
1814+
model_hint model: 'BlogPost', resource: BlogPostResource
1815+
1816+
attribute :name, :delegate => :title
1817+
attribute :body
1818+
1819+
filter :name
1820+
end
1821+
18041822
# CustomProcessors
18051823
class Api::V4::BookProcessor < JSONAPI::Processor
18061824
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)