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:



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'app/models/locomotive/extensions/page/render.rb', line 78

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.



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
56
57
58
59
60
61
# 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)
  
  query = if I18n.locale != I18n.default_locale
    site.pages.where(depth: depth).any_of({:fullpath.in => matching_paths}, {"fullpath.#{I18n.default_locale}" => {'$in' => matching_paths}})
  else
    site.pages.where(depth: depth, :fullpath.in => matching_paths)
  end

  query.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)



73
74
75
# File 'app/models/locomotive/extensions/page/render.rb', line 73

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