Sha256: abd7f338db727167ab89507004a0c446f80994b8412304ef31fd923ddba80b3c
Contents?: true
Size: 964 Bytes
Versions: 4
Compression:
Stored size: 964 Bytes
Contents
class Redirect < ActiveRecord::Base belongs_to :contents belongs_to :blog validates :from_path, uniqueness: true validates :to_path, presence: true validates :blog, presence: true def full_to_path path = to_path return path if path =~ /^(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
4 entries across 4 versions & 1 rubygems