Skip to content

Allow extensibility via custom features #555

@kstevens715

Description

@kstevens715

If there were a way to add entries to PgSearch::ScopeOptions::FEATURE_CLASSES, then users could extend pg_search with their own custom features. For example, I created a Trigram (Feature) sub-class that adds some additional scoping which greatly improved the performance of the resulting queries.

I should also mention #542 would also probably allow me to solve the same problem. However, I think there's value in having both options available.

FEATURE_CLASSES = { # standard:disable Lint/UselessConstantScoping
dmetaphone: Features::DMetaphone,
tsearch: Features::TSearch,
trigram: Features::Trigram
}.freeze

Right now, I'm getting around the fact that FEATURE_CLASSES is frozen by doing this:

Rails.application.config.after_initialize do
  feature_classes = PgSearch::ScopeOptions::FEATURE_CLASSES.dup.merge(
    scoped_trigram: PgSearch::Features::ScopedTrigram,
  ).freeze

  PgSearch::ScopeOptions.send(:remove_const, :FEATURE_CLASSES)
  PgSearch::ScopeOptions.const_set(:FEATURE_CLASSES, feature_classes)
end

Ideally, I could add a new feature like this:

PgSearch.add_feature(scoped_trigram: ScopedTrigram)

If there is interest in this feature, I would be happy to make a pull request with tests. Please let me know!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions