class Ruhoh::Resources::Pages::Previewer

Public Class Methods

new(ruhoh) click to toggle source
# File lib/ruhoh/resources/pages/previewer.rb, line 6
def initialize(ruhoh)
  @ruhoh = ruhoh
end

Public Instance Methods

call(env) click to toggle source
# File lib/ruhoh/resources/pages/previewer.rb, line 10
def call(env)
  return favicon if env['PATH_INFO'] == '/favicon.ico'

  # Always remove trailing slash if sent unless it's the root page.
  env['PATH_INFO'].chomp!("/") unless env['PATH_INFO'] == "/"

  pointer = @ruhoh.routes.find(env['PATH_INFO'])
  Ruhoh::Friend.say {
    plain "- previewing page:"
    plain "   #{pointer.inspect}"
  }

  view = pointer ? @ruhoh.master_view(pointer) : paginator_view(env)

  if view
    [200, {'Content-Type' => 'text/html'}, [view.render_full]]
  else
    Ruhoh::UI::PageNotFound.new(@ruhoh, pointer).call(env)
  end
end
favicon() click to toggle source
# File lib/ruhoh/resources/pages/previewer.rb, line 61
def favicon
  [200, {'Content-Type' => 'image/x-icon'}, ['']]
end
paginator_view(env) click to toggle source

Try the paginator. search for the namespace and match it to a resource: need a way to register pagination namespaces then search the register.

# File lib/ruhoh/resources/pages/previewer.rb, line 34
def paginator_view(env)
  path = env['PATH_INFO'].reverse.chomp("/").reverse
  resource = @ruhoh.collections.paginator_urls.find do |a, b|
    "/#{ path }" =~ %r{^#{ b }}
  end
  resource = resource[0] if resource
  return false unless resource

  page_number = path.match(/([1-9]+)$/)[0] rescue nil

  return false unless @ruhoh.collections.exist?(resource)
  return false if page_number.to_i.zero?

  collection = @ruhoh.collection(resource)
  config = collection.config["paginator"] || {}

  url = "#{config["url"]}/#{page_number}"
  
  view = @ruhoh.master_view({"resource" => resource})
  view.page_data = {
    "layout" => config["layout"],
    "current_page" => page_number,
    "url" => @ruhoh.to_url(url)
  }
  view
end