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
2 changes: 2 additions & 0 deletions lib/jsonapi/active_record_accessor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions lib/jsonapi/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions test/controllers/controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 18 additions & 0 deletions test/fixtures/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down