Class: Redirect
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Redirect
- Defined in:
- app/models/redirect.rb
Instance Method Summary collapse
- #broken! ⇒ Object
- #broken? ⇒ Boolean
- #cache_jumps! ⇒ Object
- #calculate_jumps(total = 0) ⇒ Object
- #enabled? ⇒ Boolean
- #jumps_cacher ⇒ Object
- #next_in_chain ⇒ Object
- #previous_in_chain ⇒ Object
-
#resolves?(base_url: Udongo.config.base.host, follow_location: true) ⇒ Boolean
Tests the redirect including any redirects following this one.
-
#trace_down(stack = []) ⇒ Object
This builds a list of all redirects following the current one in its progression path.
-
#trace_up(stack = []) ⇒ Object
This builds a list of all redirects prior to the current one in its progression path.
- #used! ⇒ Object
- #working! ⇒ Object
Instance Method Details
#broken! ⇒ Object
20 21 22 |
# File 'app/models/redirect.rb', line 20 def broken! update_attribute(:working, false) end |
#broken? ⇒ 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
38 39 40 |
# File 'app/models/redirect.rb', line 38 def enabled? !disabled? end |
#jumps_cacher ⇒ Object
34 35 36 |
# File 'app/models/redirect.rb', line 34 def jumps_cacher @jumps_cacher ||= Udongo::Redirects::JumpsCacher.new(self) end |
#next_in_chain ⇒ Object
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_chain ⇒ Object
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.
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 |