Created
February 9, 2015 01:48
-
-
Save devton/b07d559c4a3d61ab2e1d to your computer and use it in GitHub Desktop.
NegativeExpression model
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
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