Skip to content
Closed
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
11 changes: 10 additions & 1 deletion lib/jsonapi/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def setup(params)
when 'index'
parse_fields(params[:fields])
parse_include_directives(params[:include])
set_default_filters
parse_filters(params[:filter])
parse_sort_criteria(params[:sort])
parse_pagination(params[:page])
Expand All @@ -39,6 +40,7 @@ def setup(params)
@source_id = @source_klass.verify_key(params.require(@source_klass._as_parent_key), @context)
parse_fields(params[:fields])
parse_include_directives(params[:include])
set_default_filters
parse_filters(params[:filter])
parse_sort_criteria(params[:sort])
parse_pagination(params[:page])
Expand Down Expand Up @@ -161,7 +163,7 @@ def parse_include_directives(include)

def parse_filters(filters)
return unless filters
@filters = {}

filters.each do |key, value|
filter = unformat_key(key)
if @resource_klass._allowed_filter?(filter)
Expand All @@ -172,6 +174,13 @@ def parse_filters(filters)
end
end

def set_default_filters
@resource_klass._allowed_filters.each do |filter, opts|
next if opts[:default].nil? || !@filters[filter].nil?
@filters[filter] = opts[:default]
end
end

def parse_sort_criteria(sort_criteria)
return unless sort_criteria

Expand Down
10 changes: 5 additions & 5 deletions lib/jsonapi/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -276,11 +276,11 @@ def model_name(model)
end

def filters(*attrs)
@_allowed_filters.merge(attrs)
@_allowed_filters.merge!(attrs.inject( Hash.new ) { |h, attr| h[attr] = {}; h })
end

def filter(attr)
@_allowed_filters.add(attr.to_sym)
def filter(attr, *args)
@_allowed_filters[attr.to_sym] = args.extract_options!
end

def primary_key(key)
Expand Down Expand Up @@ -477,7 +477,7 @@ def _as_parent_key
end

def _allowed_filters
!@_allowed_filters.nil? ? @_allowed_filters : Set.new([:id])
!@_allowed_filters.nil? ? @_allowed_filters : { :id => {} }
end

def _resource_name_from_type(type)
Expand All @@ -502,7 +502,7 @@ def _model_class
end

def _allowed_filter?(filter)
_allowed_filters.include?(filter)
!_allowed_filters[filter].nil?
end

def module_path
Expand Down
16 changes: 16 additions & 0 deletions test/controllers/controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2306,3 +2306,19 @@ def test_books_paged_pagination_invalid_page_format_interpret_int
assert_equal 'Book 20', json_response['data'][0]['attributes']['title']
end
end

class CategoriesControllerTest < ActionController::TestCase
def test_index_default_filter
get :index
assert_response :success
assert json_response['data'].is_a?(Array)
assert_equal 3, json_response['data'].size
end

def test_index_default_filter_override
get :index, { filter: { status: 'inactive' } }
assert_response :success
assert json_response['data'].is_a?(Array)
assert_equal 4, json_response['data'].size
end
end
22 changes: 22 additions & 0 deletions test/fixtures/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@
t.string :numero_telefone
t.timestamps null: false
end

create_table :categories, force: true do |t|
t.string :name
t.string :status, limit: 10
end
end

### MODELS
Expand Down Expand Up @@ -329,6 +334,9 @@ class LineItem < ActiveRecord::Base
class NumeroTelefone < ActiveRecord::Base
end

class Category < ActiveRecord::Base
end

### PORO Data - don't do this in a production app
$breed_data = BreedData.new
$breed_data.add(Breed.new(0, 'persian'))
Expand Down Expand Up @@ -368,6 +376,9 @@ class BreedsController < JSONAPI::ResourceController
class FactsController < JSONAPI::ResourceController
end

class CategoriesController < JSONAPI::ResourceController
end

### CONTROLLERS
module Api
module V1
Expand Down Expand Up @@ -728,6 +739,10 @@ class FactResource < JSONAPI::Resource
attribute :cool
end

class CategoryResource < JSONAPI::Resource
filter :status, default: 'active'
end

module Api
module V1
class WriterResource < JSONAPI::Resource
Expand Down Expand Up @@ -941,3 +956,10 @@ class BadlyNamedAttributesResource < JSONAPI::Resource
betay = Planet.create(name: 'Beta X', description: 'Newly discovered Planet Y', planet_type_id: unknown.id)
betaz = Planet.create(name: 'Beta X', description: 'Newly discovered Planet Z', planet_type_id: unknown.id)
betaw = Planet.create(name: 'Beta W', description: 'Newly discovered Planet W')
Category.create(name: 'Category A', status: 'active')
Category.create(name: 'Category B', status: 'active')
Category.create(name: 'Category C', status: 'active')
Category.create(name: 'Category D', status: 'inactive')
Category.create(name: 'Category E', status: 'inactive')
Category.create(name: 'Category F', status: 'inactive')
Category.create(name: 'Category G', status: 'inactive')
1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ def count_queries(&block)
jsonapi_resources :moons
jsonapi_resources :preferences
jsonapi_resources :facts
jsonapi_resources :categories

namespace :api do
namespace :v1 do
Expand Down