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



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

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

Returns:

  • (Boolean)


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

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



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

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
15
# File 'app/helpers/admin/url_helper.rb', line 4

def format_path(path)
  parts = path.to_s.split('/').reject(&:empty?)

  case parts.size
  when 0 then ''
  when 1 then 'Root'
  when 2 then '/'
  else
    subpath = parts[1..-2].to_a.join('/')
    subpath.empty? ? '/' : "/#{subpath}"
  end
end

#generate_page_url(url, page) ⇒ Object



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

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

#lookup_page_path(page) ⇒ Object



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

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