parsley/app/models/concerns/tokenized_like.rb
2016-10-20 15:48:33 -05:00

32 lines
670 B
Ruby

module TokenizedLike
extend ActiveSupport::Concern
module ClassMethods
def matches_tokens(attribute, tokens)
table = self.arel_table
query = self.all
tokens.each do |t|
match1 = "#{t}%"
match2 = "% #{t}%"
match3 = "%,#{t}%"
matcher = ->(m) { table[attribute.to_sym].matches(m) }
cond = matcher.call(match1).or(matcher.call(match2)).or(matcher.call(match3))
query = query.where(cond)
end
query
end
def matches_token(attribute, token)
table = self.arel_table
query = self.all
query.where(table[attribute.to_sym].matches("#{token}%"))
end
end
end