Module: Admin::UrlHelper

Included in:
NodeHelper, PagesController, ApplicationHelper
Defined in:
app/helpers/admin/url_helper.rb

Instance Method Summary collapse

Instance Method Details

#build_url(base_url, page) ⇒ Object



21
22
23
24
25
26
27
28
# File 'app/helpers/admin/url_helper.rb', line 21

def build_url(base_url, page)
  return "#{base_url}#{page.path}" if default_route?(page)

  path = lookup_page_path(page)
  return nil unless path

  "#{base_url}/#{path}/#{page.slug}"
end

#default_route?(page) ⇒ Boolean



39
40
41
42
# File 'app/helpers/admin/url_helper.rb', line 39

def default_route?(page)
  page_class_name = page.class.name
  page_class_name == 'Page' || (defined?(DEFAULT_PAGE_TYPE_ROUTES) && DEFAULT_PAGE_TYPE_ROUTES.include?(page_class_name))
end

#extract_base_url(url) ⇒ Object



30
31
32
33
34
35
36
37
# File 'app/helpers/admin/url_helper.rb', line 30

def extract_base_url(url)
  uri = URI.parse(url)
  host = uri.host
  scheme = uri.scheme
  return "#{scheme}://#{host}:#{uri.port}" if host&.include?('localhost')

  "#{scheme}://#{host}"
end

#format_path(path) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
# File 'app/helpers/admin/url_helper.rb', line 4

def format_path(path)
  return '' if path.to_s.empty?

  parts = path.split('/').reject(&:empty?)
  parts_size = parts.size
  return 'Root' if parts_size == 1
  return '/' if parts_size == 2

  formatted_path = parts[1..-2].join('/')
  formatted_path.empty? ? '/' : "/#{formatted_path}"
end

#generate_page_url(url, page) ⇒ Object



16
17
18
19
# File 'app/helpers/admin/url_helper.rb', line 16

def generate_page_url(url, page)
  base_url = extract_base_url(url)
  build_url(base_url, page)
end

#lookup_page_path(page) ⇒ Object



44
45
46
47
48
# File 'app/helpers/admin/url_helper.rb', line 44

def lookup_page_path(page)
  return nil unless defined?(CUSTOM_PAGE_TYPE_ROUTES) && CUSTOM_PAGE_TYPE_ROUTES.is_a?(Hash)

  CUSTOM_PAGE_TYPE_ROUTES[page.class.name.to_sym]
end