class method self.precompile_filters

Ruby on Rails edge

Since v7.1.6

Available in: v7.1.6 v7.2.3.2 v8.0.5.1 v8.1.3.1 edge

Signature

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

filters req
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

Type at least 2 characters to search.

Use the arrow keys to navigate results, Enter to open one, Escape to close.

Keyboard shortcuts

/
Focus search
⌘K / Ctrl-K
Command palette
?
This help
Esc
Close