-
-
Notifications
You must be signed in to change notification settings - Fork 196
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
[#6402] Warning notes on request preview #7963
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
require 'set' | ||
|
||
# Apply tags to a Taggable record based on configured attributes matching terms | ||
# mapped to tags get applied when there is a match. | ||
# | ||
# Example: | ||
# | ||
# Record.taggable_terms = { body: { /foo/ => 'foo' } } | ||
# r = Record.new | ||
# r.tagged?('foo') | ||
# # => false | ||
# | ||
# r.update(body: 'foo bar baz') | ||
# r.tagged?('foo') | ||
# # => true | ||
module TaggableTerms | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
cattr_accessor :taggable_terms, default: {} | ||
before_save :update_taggable_terms, if: :taggable_term_attribute_changed? | ||
end | ||
|
||
def update_taggable_terms | ||
tags_to_add, tags_to_remove = taggable_terms_changed_tags | ||
tags_to_add.each { |tag| add_tag_if_not_already_present(tag) } | ||
tags_to_remove.each { |tag| remove_tag(tag) } | ||
end | ||
|
||
private | ||
|
||
def taggable_term_attribute_changed? | ||
changed.include?(taggable_terms.keys) | ||
end | ||
|
||
def taggable_terms_changed_tags | ||
tags_to_add = Set.new | ||
tags_to_remove = Set.new | ||
|
||
taggable_terms_to_tag_to_attr_terms.each do |tag, attr_terms_pairs| | ||
tag_str = tag.to_s | ||
|
||
attr_terms_pairs.each do |attr, term| | ||
if attribute_matches_taggable_term?(attr, term) | ||
tags_to_add << tag_str | ||
break | ||
end | ||
end | ||
|
||
tags_to_remove << tag_str unless tags_to_add.include?(tag_str) | ||
end | ||
|
||
[tags_to_add, tags_to_remove] | ||
end | ||
|
||
def attribute_matches_taggable_term?(attr, term) | ||
read_attribute(attr) =~ Regexp.new(term) | ||
end | ||
|
||
# Restructure taggable_terms so that it's more processing friendly | ||
# | ||
# Before: | ||
# => {:body=>{/train/i=>"trains", /bus/i=>"bus", /locomotive/i=>"trains"}} | ||
# | ||
# After: | ||
# => {"trains"=>[[:body, /train/i], [:body, /locomotive/i]], | ||
# "bus"=>[[:body, /bus/i]]} | ||
def taggable_terms_to_tag_to_attr_terms | ||
seed = Hash.new { |h, k| h[k] = [] } | ||
|
||
taggable_terms.each_with_object(seed) do |(attr, terms_tags), memo| | ||
terms_tags.each do |term, tag| | ||
memo[tag] << [attr, term] | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
RSpec.shared_examples 'concerns/taggable_terms' do |*factory_opts, attr_under_test| | ||
let(:record) { FactoryBot.build(*factory_opts) } | ||
|
||
let(:terms_tags) do | ||
{ /train/i => 'trains', | ||
/bus/i => 'bus', | ||
/locomotive/i => 'trains', | ||
/bike/ => 'bicycles' } | ||
end | ||
|
||
before do | ||
record.taggable_terms = { attr_under_test => terms_tags } | ||
end | ||
|
||
describe '#update_taggable_terms' do | ||
subject { record.update_taggable_terms } | ||
before { subject } | ||
|
||
it 'applies a tag when a term matches' do | ||
expect(record).to be_tagged('bus') | ||
end | ||
|
||
it 'does not apply a tag when there is no match for a term' do | ||
expect(record).not_to be_tagged('bicycles') | ||
end | ||
|
||
it 'applies a tag when one term term matches but a later term with the same tag does not' do | ||
# i.e. keep "trains" because it matched /train/, so don't remove it | ||
# because it didn't match /locomotive/ | ||
expect(record).to be_tagged('trains') | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have you thought of using
Regexp.union
so this would return something like:Which should make
taggable_terms_changed_tags
simpler.Also what about if the body contains say "constrain", we could in theory convert the terms into a Regexp (if not already) and wrap between word boundaries EG:
\b...\b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did think about whether it might be worth munging into a single regexp. Are there any performance issues with that past a certain point?
In real use I'm expecting the regexps to be more like
/my 999 call/
,/my visa application/
, etc – more phrases than singular words. I think if we do want to match a singular word (SAR
) for example, then when we configure it we explicitly include things in the regexp that reduces the chances of false positives.