Class: Redirect

Inherits:
ApplicationRecord
  • Object
show all
Includes:
StringLengthLimit
Defined in:
app/models/redirect.rb

Constant Summary

Constants included from StringLengthLimit

StringLengthLimit::STRING_LIMIT

Instance Method Summary collapse

Instance Method Details

#from_urlObject



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

def from_url
  File.join(blog.shortener_url, from_path)
end

#full_to_pathObject



14
15
16
17
18
19
20
21
22
23
24
# File 'app/models/redirect.rb', line 14

def full_to_path
  path = to_path
  # FIXME: Unify HTTP URI matchers
  return path if %r{^(https?)://([^/]*)(.*)}.match?(path)

  url_root = blog.root_path
  if url_root.length == 0 || path[0, url_root.length] != url_root
    path = blog.url_for(path, only_path: true)
  end
  path
end

#shortenObject



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

def shorten
  if (temp_token = random_token) && self.class.find_by(from_path: temp_token).nil?
    temp_token
  else
    shorten
  end
end

#to_urlObject



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

def to_url
  raise "Use #from_url"
end