Skip to content

Commit e63642b

Browse files
committed
Allow apply/verify filter callables to be a symbol/string of the method name to call.
1 parent 8e85d68 commit e63642b

2 files changed

Lines changed: 28 additions & 13 deletions

File tree

lib/jsonapi/resource.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,11 @@ def apply_filter(records, filter, value, options = {})
531531
strategy = _allowed_filters.fetch(filter.to_sym, Hash.new)[:apply]
532532

533533
if strategy
534-
strategy.call(records, value, options)
534+
if strategy.is_a?(Symbol) || strategy.is_a?(String)
535+
send(strategy, records, value, options)
536+
else
537+
strategy.call(records, value, options)
538+
end
535539
else
536540
records.where(filter => value)
537541
end
@@ -630,7 +634,12 @@ def verify_filter(filter, raw, context = nil)
630634
strategy = _allowed_filters.fetch(filter, Hash.new)[:verify]
631635

632636
if strategy
633-
[filter, strategy.call(filter_values, context)]
637+
if strategy.is_a?(Symbol) || strategy.is_a?(String)
638+
values = send(strategy, filter_values, context)
639+
else
640+
values = strategy.call(filter_values, context)
641+
end
642+
[filter, values]
634643
else
635644
if is_filter_relationship?(filter)
636645
verify_relationship_filter(filter, filter_values, context)

test/fixtures/active_record.rb

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -804,14 +804,17 @@ class PersonResource < BaseResource
804804
has_one :preferences
805805
has_one :hair_cut
806806

807-
filter :name, verify: ->(values, _context) {
807+
filter :name, verify: :verify_name_filter
808+
809+
def self.verify_name_filter(values, _context)
808810
values.each do |value|
809811
if value.length < 3
810812
raise JSONAPI::Exceptions::InvalidFilterValue.new(:name, value)
811813
end
812814
end
813815
return values
814-
}
816+
end
817+
815818
end
816819

817820
class SpecialBaseResource < BaseResource
@@ -1199,15 +1202,7 @@ class BookResource < JSONAPI::Resource
11991202
has_many :aliased_comments, class_name: 'BookComments', relation_name: :approved_book_comments
12001203

12011204
filters :book_comments
1202-
filter :banned, apply: ->(records, value, options) {
1203-
context = options[:context]
1204-
current_user = context ? context[:current_user] : nil
1205-
1206-
# Only book admins my filter for banned books
1207-
if current_user && current_user.book_admin
1208-
records.where('books.banned = ?', value[0] == 'true')
1209-
end
1210-
}
1205+
filter :banned, apply: :apply_filter_banned
12111206

12121207
class << self
12131208
def books
@@ -1229,6 +1224,17 @@ def records(options = {})
12291224
end
12301225
records
12311226
end
1227+
1228+
def apply_filter_banned(records, value, options)
1229+
context = options[:context]
1230+
current_user = context ? context[:current_user] : nil
1231+
1232+
# Only book admins might filter for banned books
1233+
if current_user && current_user.book_admin
1234+
records.where('books.banned = ?', value[0] == 'true')
1235+
end
1236+
end
1237+
12321238
end
12331239
end
12341240

0 commit comments

Comments
 (0)