parsley/app/models/concerns/tokenized_like.rb

32 lines
670 B
Ruby
Raw Normal View History

2016-01-28 14:19:51 -06:00
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
2016-10-20 15:48:33 -05:00
def matches_token(attribute, token)
table = self.arel_table
query = self.all
query.where(table[attribute.to_sym].matches("#{token}%"))
end
2016-01-28 14:19:51 -06:00
end
end