Skip to content

Commit 9caf694

Browse files
committed
Merge pull request #613 from kenzai/filter-apply-verify-symbol
Verify and apply filters using a callback name
2 parents 32a7861 + e63642b commit 9caf694

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
@@ -540,7 +540,11 @@ def apply_filter(records, filter, value, options = {})
540540
strategy = _allowed_filters.fetch(filter.to_sym, Hash.new)[:apply]
541541

542542
if strategy
543-
strategy.call(records, value, options)
543+
if strategy.is_a?(Symbol) || strategy.is_a?(String)
544+
send(strategy, records, value, options)
545+
else
546+
strategy.call(records, value, options)
547+
end
544548
else
545549
records.where(filter => value)
546550
end
@@ -639,7 +643,12 @@ def verify_filter(filter, raw, context = nil)
639643
strategy = _allowed_filters.fetch(filter, Hash.new)[:verify]
640644

641645
if strategy
642-
[filter, strategy.call(filter_values, context)]
646+
if strategy.is_a?(Symbol) || strategy.is_a?(String)
647+
values = send(strategy, filter_values, context)
648+
else
649+
values = strategy.call(filter_values, context)
650+
end
651+
[filter, values]
643652
else
644653
if is_filter_relationship?(filter)
645654
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
@@ -1280,15 +1283,7 @@ class BookResource < JSONAPI::Resource
12801283
has_many :aliased_comments, class_name: 'BookComments', relation_name: :approved_book_comments
12811284

12821285
filters :book_comments
1283-
filter :banned, apply: ->(records, value, options) {
1284-
context = options[:context]
1285-
current_user = context ? context[:current_user] : nil
1286-
1287-
# Only book admins my filter for banned books
1288-
if current_user && current_user.book_admin
1289-
records.where('books.banned = ?', value[0] == 'true')
1290-
end
1291-
}
1286+
filter :banned, apply: :apply_filter_banned
12921287

12931288
class << self
12941289
def books
@@ -1310,6 +1305,17 @@ def records(options = {})
13101305
end
13111306
records
13121307
end
1308+
1309+
def apply_filter_banned(records, value, options)
1310+
context = options[:context]
1311+
current_user = context ? context[:current_user] : nil
1312+
1313+
# Only book admins might filter for banned books
1314+
if current_user && current_user.book_admin
1315+
records.where('books.banned = ?', value[0] == 'true')
1316+
end
1317+
end
1318+
13131319
end
13141320
end
13151321

0 commit comments

Comments
 (0)