class method
self.precompile_filters
Ruby on Rails edge
Since v7.1.6Signature
self.precompile_filters(filters)
Precompiles an array of filters that otherwise would be passed directly to #initialize. Depending on the quantity and types of filters, precompilation can improve filtering performance, especially in the case where the ParameterFilter instance itself cannot be retained (but the precompiled filters can be retained).
filters = [/foo/, :bar, "nested.baz", /nested\.qux/]
precompiled = ActiveSupport::ParameterFilter.precompile_filters(filters)
# => [/(?-mix:foo)|(?i:bar)/, /(?i:nested\.baz)|(?-mix:nested\.qux)/]
ActiveSupport::ParameterFilter.new(precompiled)
Parameters
-
filtersreq
Source
# File activesupport/lib/active_support/parameter_filter.rb, line 55
def self.precompile_filters(filters)
filters, patterns = filters.partition { |filter| filter.is_a?(Proc) }
regexps, patterns = patterns.partition { |filter| filter.is_a?(Regexp) }
patterns.map! do |p|
p.is_a?(Symbol) ? p.name : p.to_s
end
patterns.sort_by! { |p| [p.count("."), p.size] }
patterns.each do |pattern|
# Remove redundant patterns, e.g. it's pointless to search for `user.password` or `user.password_confirmation`
# if `password` is already a filter.
unless regexps.any? { |r| r.match?(pattern) }
regexps << Regexp.new(Regexp.escape(pattern), Regexp::IGNORECASE)
end
end
filters << Regexp.union(regexps)
end
Defined in activesupport/lib/active_support/parameter_filter.rb line 55
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActiveSupport::ParameterFilter