Sha256: bd9808f5494787197b627343440fe190ba49093f93d059919b623814a9c0d8c1

Contents?: true

Size: 1 KB

Versions: 1

Compression:

Stored size: 1 KB

Contents

class Redirect < ApplicationRecord
  belongs_to :contents, optional: true, touch: true
  belongs_to :blog

  validates :from_path, uniqueness: true
  validates :to_path, presence: true
  validates :blog, presence: true

  def full_to_path
    path = to_path
    # FIXME: Unify HTTP URI matchers
    return path if path =~ %r{^(https?)://([^/]*)(.*)}
    url_root = blog.root_path
    path = File.join(url_root, path) unless url_root.nil? || path[0, url_root.length] == url_root
    path
  end

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

  def to_url
    raise 'Use #from_url'
  end

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

  private

  def random_token
    characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890'
    temp_token = ''
    srand
    6.times do
      pos = rand(characters.length)
      temp_token += characters[pos..pos]
    end
    temp_token
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
publify_core-9.1.0 app/models/redirect.rb