Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add regular expression tag searches #11

Open
esb opened this issue Jan 17, 2018 · 6 comments
Open

Add regular expression tag searches #11

esb opened this issue Jan 17, 2018 · 6 comments

Comments

@esb
Copy link

esb commented Jan 17, 2018

As a quick exercise, I have added the ability to search the array values with a regular expression.

The following scope was added to taggable.rb

scope :"with_#{tag_name}_like", ->(pattern) { subquery = unscoped.select("*, unnest(#{table_name}.#{tag_name}) AS subquery_tag"); select('*').from(subquery).where('subquery_tag ~* ?', pattern) }

This allows you to get tags as follows:

Object.with_tags_like('^Material')
Object.with_tags_like('^(Wood|Iron)')
@esb
Copy link
Author

esb commented Jan 17, 2018

Improved version

  scope :"with_#{tag_name}_like", ->(pattern) { subquery = unscoped.select("id, unnest(#{table_name}.#{tag_name}) AS subquery_tag"); where(:id => select('id').from(subquery).where('subquery_tag ~* ?', pattern)) }

@skatkov
Copy link
Collaborator

skatkov commented May 25, 2018

@esb do you actually use this? Hard to imagine big use for tags matching.

@esb
Copy link
Author

esb commented May 25, 2018

This is massively useful for finding items in categories/sub-categories.

To use a D&D analogy, I might have an item tagged with 'weapon-small' and another with 'weapon-medium'. With tag matching I can find all the items that match 'weapon' and also refine that by searching for just small weapons with 'weapon-small'.

@skatkov
Copy link
Collaborator

skatkov commented May 25, 2018

You want to try to commit this as a pull request? @tmiyamon still merges them from time to time.

I contacted him if he would like to have another maintainer for this gem (I'd be happy to, since i use it heavily in my own project) - hopefully he will answer positively to my offer ;-)

@noredeen
Copy link

Would love to see this feature. I've been digging for a neat way to use the % operator from pg_trgm as an alternative to complete matching. If others are interested, I would be happy to give it a shot and submit a pull request.

@skatkov
Copy link
Collaborator

skatkov commented Dec 30, 2020

I don't have a personal need for this feature, but now I can accept pull requests and release new gems.

If you can cover all bases with your PR (documentation, tests, feature) - I'll be happy to review/merge it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants