class Locomotive::Steam::PageFinderService

Public Instance Methods

by_handle(handle, with_cache = true) click to toggle source
# File lib/locomotive/steam/services/page_finder_service.rb, line 27
def by_handle(handle, with_cache = true)
  decorate do
    with_cache ? page_map[handle] : repository.by_handle(handle)
  end
end
find(path) click to toggle source
# File lib/locomotive/steam/services/page_finder_service.rb, line 10
def find(path)
  decorate do
    repository.by_fullpath(path)
  end
end
find_by_id(id) click to toggle source
# File lib/locomotive/steam/services/page_finder_service.rb, line 33
def find_by_id(id)
  decorate do
    repository.find(id)
  end
end
match(path) click to toggle source
# File lib/locomotive/steam/services/page_finder_service.rb, line 16
def match(path)
  decorate do
    repository.matching_fullpath(path_combinations(path))
  end.sort do |page_1, page_2|
    # normal pages have priority over the templatized ones if they're not in the same "folder"
    same_folder?(page_1, page_2) ?
      page_1.position <=> page_2.position :
      (page_2.fullpath.include?(WILDCARD) ? 0 : 1) <=> (page_1.fullpath.include?(WILDCARD) ? 0 : 1)
  end
end

Private Instance Methods

_path_combinations(segments, can_include_template = true) click to toggle source
# File lib/locomotive/steam/services/page_finder_service.rb, line 73
def _path_combinations(segments, can_include_template = true)
  return nil if segments.empty?
  segment = segments.shift

  (can_include_template ? [segment, WILDCARD] : [segment]).map do |_segment|
    if (_combinations = _path_combinations(segments.clone, can_include_template && _segment != WILDCARD))
      [*_combinations].map do |_combination|
        File.join(_segment, _combination)
      end
    else
      [_segment]
    end
  end.flatten
end
decorate(&block) click to toggle source
# File lib/locomotive/steam/services/page_finder_service.rb, line 41
def decorate(&block)
  super(Decorators::PageDecorator, &block)
end
page_map() click to toggle source

Instead of hitting the DB each time we want a page from its handle, just get all the handles at once and cache the result. (up to 20% boost)

# File lib/locomotive/steam/services/page_finder_service.rb, line 48
def page_map
  @page_map ||= {}

  return @page_map[repository.locale] if @page_map[repository.locale]

  {}.tap do |map|
    repository.only_handle_and_fullpath.each do |page|
      map[page.handle] = page
    end

    @page_map[repository.locale] = map
  end
end
path_combinations(path) click to toggle source
# File lib/locomotive/steam/services/page_finder_service.rb, line 62
def path_combinations(path)
  _path_combinations(path.split('/'))
end
same_folder?(page_1, page_2) click to toggle source
# File lib/locomotive/steam/services/page_finder_service.rb, line 66
def same_folder?(page_1, page_2)
  (path_1 = page_1.fullpath.split('/')).pop
  (path_2 = page_2.fullpath.split('/')).pop

  path_1.join('/') == path_2.join('/')
end