tmiyamon / acts-as-taggable-array-on

A simple and high performance tagging gem for Rails using PostgreSQL array.
MIT License
125 stars 26 forks source link

Add regular expression tag searches #11

Open esb opened 6 years ago

esb commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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

esb commented 6 years ago

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 commented 6 years ago

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 commented 3 years ago

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 commented 3 years ago

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.