Module: Locomotive::Extensions::Page::Render::ClassMethods

Defined in:
app/models/locomotive/extensions/page/render.rb

Instance Method Summary collapse

Instance Method Details

#_path_combinations(segments, can_include_template = true) ⇒ Object

:nodoc:



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/models/locomotive/extensions/page/render.rb', line 72

def _path_combinations(segments, can_include_template = true)
  return nil if segments.empty?

  segment = segments.shift

  (can_include_template ? [segment, 'content_type_template'] : [segment]).map do |_segment|
    if (_combinations = _path_combinations(segments.clone, can_include_template && _segment != 'content_type_template'))
      [*_combinations].map do |_combination|
        File.join(_segment, _combination)
      end
    else
      [_segment]
    end
  end.flatten
end

#fetch_page_from_path(site, path, logged_in) ⇒ Page

Given both a site and a path, this method tries to get the matching page. If the page is templatized, the related content entry is associated to the page (page.content_entry stores the entry). If no page is found, then it returns the 404 one instead.

Parameters:

  • site (Site)

    The site where to find the page

  • path (String)

    The fullpath got from the request

  • logged_in (Boolean)

    True if someone is logged in Locomotive

Returns:

  • (Page)

    The page matching the criteria OR the 404 one if none



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/models/locomotive/extensions/page/render.rb', line 26

def fetch_page_from_path(site, path, logged_in)
  page  = nil
  depth = path == 'index' ? 0 : path.split('/').size

  matching_paths = path == 'index' ? %w(index) : path_combinations(path)

  site.pages.where(:depth => depth, :fullpath.in => matching_paths).each do |_page|
    if !_page.published? && !logged_in
      next
    else
      if _page.templatized?
        %r(^#{_page.fullpath.gsub('content_type_template', '([^\/]+)')}$) =~ path

        permalink = $1

        _page.content_entry = _page.fetch_target_entry(permalink)

        if _page.content_entry.nil? || (!_page.content_entry.visible? && !logged_in) # content instance not found or not visible
          next
        end
      end
    end

    page = _page

    break
  end

  page || site.pages.not_found.published.first
end

#path_combinations(path) ⇒ Array

Calculate all the combinations possible based on the fact that one of the segment of the path could be a content type from a templatized page. We postulate that there is only one templatized page in a path (ie: no nested templatized pages)

Parameters:

  • path (String)

    The path to the page

Returns:

  • (Array)

    An array of all the combinations



67
68
69
# File 'app/models/locomotive/extensions/page/render.rb', line 67

def path_combinations(path)
  _path_combinations(path.split('/'))
end