Class: RedirectRule

Inherits:
ActiveRecord::Base
  • Object
show all
Extended by:
Redirector::RegexAttribute
Defined in:
app/models/redirect_rule.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Redirector::RegexAttribute

regex_attribute

Class Method Details

.destination_for(source, environment) ⇒ Object



53
54
55
56
57
58
# File 'app/models/redirect_rule.rb', line 53

def self.destination_for(source, environment)
  return if Redirector.blacklisted_extensions.include? File.extname(source)

  rule = match_for(source, environment)
  rule.evaluated_destination_for(source) if rule
end

.match_for(source, environment) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/models/redirect_rule.rb', line 40

def self.match_for(source, environment)
  match_scope = where(match_sql_condition.strip, {:true => true, :false => false, :source => source})
  match_scope = match_scope.order('redirect_rules.source_is_regex ASC, LENGTH(redirect_rules.source) DESC')

  return match_scope.first unless Redirector.use_environment_variables

  match_scope = match_scope.includes(:request_environment_rules)
  match_scope = match_scope.references(:request_environment_rules) if Rails.version.to_i == 4
  match_scope.detect do |rule|
    rule.request_environment_rules.all? {|env_rule| env_rule.matches?(environment) }
  end
end

.match_sql_conditionObject



31
32
33
34
35
36
37
38
# File 'app/models/redirect_rule.rb', line 31

def self.match_sql_condition
  <<-SQL
    redirect_rules.active = :true AND
    ((source_is_regex = :false AND source_is_case_sensitive = :false AND LOWER(redirect_rules.source) = LOWER(:source)) OR
    (source_is_regex = :false AND source_is_case_sensitive = :true AND #{'BINARY' if connection_mysql?} redirect_rules.source = :source) OR
    (source_is_regex = :true AND (#{regex_expression})))
  SQL
end

.regex_expressionObject



21
22
23
24
25
26
27
28
29
# File 'app/models/redirect_rule.rb', line 21

def self.regex_expression
  if connection_mysql?
    '(redirect_rules.source_is_case_sensitive = :true AND :source REGEXP BINARY redirect_rules.source) OR '+
    '(redirect_rules.source_is_case_sensitive = :false AND :source REGEXP redirect_rules.source)'
  else
    '(redirect_rules.source_is_case_sensitive = :true AND :source ~ redirect_rules.source) OR '+
    '(redirect_rules.source_is_case_sensitive = :false AND :source ~* redirect_rules.source)'
  end
end

Instance Method Details

#evaluated_destination_for(request_path) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/models/redirect_rule.rb', line 60

def evaluated_destination_for(request_path)
  if source_is_regex? && request_path =~ source_regex
    matches = $~
    number_of_grouped_matches = matches.length - 1
    final_destination = destination.dup

    number_of_grouped_matches.downto(1) do |index|
      final_destination.gsub!(/\$#{index}/, matches[index].to_s)
    end

    final_destination
  else
    destination
  end
end