Class: Redirect

Inherits:
ApplicationRecord show all
Defined in:
app/models/redirect.rb

Instance Method Summary collapse

Instance Method Details

#broken!Object



20
21
22
# File 'app/models/redirect.rb', line 20

def broken!
  update_attribute(:working, false)
end

#broken?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'app/models/redirect.rb', line 16

def broken?
  !working?
end

#cache_jumps!Object



24
25
26
# File 'app/models/redirect.rb', line 24

def cache_jumps!
  update_attribute(:jumps, calculate_jumps)
end

#calculate_jumps(total = 0) ⇒ Object



28
29
30
31
32
# File 'app/models/redirect.rb', line 28

def calculate_jumps(total = 0)
  total += 1
  return next_in_chain.calculate_jumps(total) if next_in_chain.present?
  total
end

#enabled?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'app/models/redirect.rb', line 38

def enabled?
  !disabled?
end

#jumps_cacherObject



34
35
36
# File 'app/models/redirect.rb', line 34

def jumps_cacher
  @jumps_cacher ||= Udongo::Redirects::JumpsCacher.new(self)
end

#next_in_chainObject



42
43
44
# File 'app/models/redirect.rb', line 42

def next_in_chain
  self.class.enabled.find_by(source_uri: destination_uri)
end

#previous_in_chainObject



46
47
48
# File 'app/models/redirect.rb', line 46

def previous_in_chain
  self.class.enabled.find_by(destination_uri: source_uri)
end

#resolves?(base_url: Udongo.config.base.host, follow_location: true) ⇒ Boolean

Tests the redirect including any redirects following this one.

Returns:

  • (Boolean)


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

def resolves?(base_url: Udongo.config.base.host, follow_location: true)
  response = Udongo::Redirects::Test.new(self).perform!(
    base_url: base_url,
    follow_location: follow_location
  )

  response.success? && response.endpoint_matches?(base_url + destination_uri)
end

#trace_down(stack = []) ⇒ Object

This builds a list of all redirects following the current one in its progression path. Includes the current redirect as the first item. See #next_in_chain for the trace conditions.



63
64
65
66
67
# File 'app/models/redirect.rb', line 63

def trace_down(stack = [])
  stack << self
  return next_in_chain.trace_down(stack) if next_in_chain.present?
  stack
end

#trace_up(stack = []) ⇒ Object

This builds a list of all redirects prior to the current one in its progression path. Includes the current redirect as the first item. See #previous_in_chain for the trace conditions.



72
73
74
75
76
# File 'app/models/redirect.rb', line 72

def trace_up(stack = [])
  stack << self
  return previous_in_chain.trace_up(stack) if previous_in_chain.present?
  stack.reverse # Reversing because the top most one will be at the end.
end

#used!Object



78
79
80
81
# File 'app/models/redirect.rb', line 78

def used!
  count = self.times_used.nil? ? 1 : times_used + 1
  update_attribute :times_used, count
end

#working!Object



83
84
85
# File 'app/models/redirect.rb', line 83

def working!
  update_attribute(:working, true)
end