parsley/app/models/concerns/tokenized_like.rb
2016-01-28 14:19:51 -06:00

26 lines
504 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
end
end