Skip to content

Instantly share code, notes, and snippets.

@devton
Created February 9, 2015 01:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devton/b07d559c4a3d61ab2e1d to your computer and use it in GitHub Desktop.
Save devton/b07d559c4a3d61ab2e1d to your computer and use it in GitHub Desktop.
NegativeExpression model
class NegativeExpression < ActiveRecord::Base
# Scope que verifica se existe alguma
# expressão negativa para o dominio (host) informado
scope :expressions_for, -> (host) {
where("lower(?) ~* ANY(domains)", host)
}
# Scope que faz um regex match no campo expressions
# verificando se o path informado retornando
# todas as expressões que passou no match
scope :with_path_regex, ->(path) {
where("lower(?) ~* ANY(expressions)", path)
}
# Verifica se existe alguma
# expressão negativa para url passada
def self.url_match? url
uri = URI url
# aqui eu uso os scopes passando um .exists? no final
# dessa maneira o postgres já faz todo o matching e retorna true ou false
expressions_for(uri.host).with_path_regex(uri.path).exists?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment