Module: Locomotive::Concerns::Site::UrlRedirections

Extended by:
ActiveSupport::Concern
Included in:
Site
Defined in:
app/models/locomotive/concerns/site/url_redirections.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#add_or_update_url_redirection(source, target, information = nil) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/models/locomotive/concerns/site/url_redirections.rb', line 48

def add_or_update_url_redirection(source, target, information = nil)
  return false if source.blank? || target.blank?

  source, target = add_leading_slash_to(source), add_leading_slash_to(target)
  url_id = Digest::MD5.hexdigest(source)

  if redirection = url_redirections.detect { |(_source, _)| _source == source }
    redirection[1] = target
  else
    url_redirections << [source, target]
  end

  url_redirections_information[url_id] = information
end

#remove_url_redirection(source) ⇒ Object



63
64
65
66
67
68
69
# File 'app/models/locomotive/concerns/site/url_redirections.rb', line 63

def remove_url_redirection(source)
  source = add_leading_slash_to(source)
  url_id = Digest::MD5.hexdigest(source)

  url_redirections.delete_if { |(_source, _)| _source == source }
  url_redirections_information.delete(url_id)
end

#url_redirections=(array) ⇒ Object



71
72
73
74
75
# File 'app/models/locomotive/concerns/site/url_redirections.rb', line 71

def url_redirections=(array)
  super((array || []).flatten.map do |path|
    add_leading_slash_to(path)
  end.each_slice(2).to_a)
end

#url_redirections_plain_textObject



28
29
30
31
32
33
# File 'app/models/locomotive/concerns/site/url_redirections.rb', line 28

def url_redirections_plain_text
  url_redirections
  .sort { |a, b| a.first <=> b.first }
  .map { |redirection| redirection.join(' ') }
  .join("\n")
end

#url_redirections_with_information(with_hidden = true) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/models/locomotive/concerns/site/url_redirections.rb', line 35

def url_redirections_with_information(with_hidden = true)
  url_redirections.map do |(source, target)|
    url_id = Digest::MD5.hexdigest(source)
    information = url_redirections_information[url_id] || {}

    if !with_hidden && information['hidden']
      nil
    else
      { 'source' => source, 'target' => target }.merge(information)
    end
  end
end