Class: Permalink

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/permalink.rb

Defined Under Namespace

Classes: Normalizer

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.filter_by(url = nil) ⇒ Object



81
82
83
84
85
86
87
88
# File 'app/models/permalink.rb', line 81

def self.filter_by(url = nil)
  permalinks =
    Permalink.includes(:topic, :post, :category, :tag).order("permalinks.created_at desc")

  permalinks.where!("url ILIKE :url OR external_url ILIKE :url", url: "%#{url}%") if url.present?
  permalinks.limit!(100)
  permalinks.to_a
end

.find_by_url(url) ⇒ Object



64
65
66
# File 'app/models/permalink.rb', line 64

def self.find_by_url(url)
  find_by(url: normalize_url(url))
end

.normalize_url(url) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/models/permalink.rb', line 51

def self.normalize_url(url)
  if url
    url = url.strip
    url = url[1..-1] if url[0, 1] == "/"
  end

  normalizations = SiteSetting.permalink_normalizations

  @normalizer = Normalizer.new(normalizations) unless @normalizer &&
    @normalizer.source == normalizations
  @normalizer.normalize(url)
end

Instance Method Details

#normalize_urlObject



68
69
70
# File 'app/models/permalink.rb', line 68

def normalize_url
  self.url = Permalink.normalize_url(url) if url
end

#target_urlObject



72
73
74
75
76
77
78
79
# File 'app/models/permalink.rb', line 72

def target_url
  return external_url if external_url
  return "#{Discourse.base_path}#{post.url}" if post
  return topic.relative_url if topic
  return category.url if category
  return tag.full_url if tag
  nil
end