Sha256: 568d6cc6dece87334b8f24bcb26fe63def3ca7dc64c8174860c060d009539dc2

Contents?: true

Size: 1.56 KB

Versions: 1

Compression:

Stored size: 1.56 KB

Contents

module Mascot
  # Rescued by ActionController to display page not found error.
  PageNotFoundError = Class.new(StandardError)

  # Renders a mascot page via the params path via ActionController.
  class ActionControllerContext
    attr_reader :controller, :sitemap

    def initialize(controller: , sitemap: )
      @controller = controller
      @sitemap = sitemap
    end

    # Renders a mascot page, given a path, and accepts parameters like layout
    # and locals if the user wants to provide additional context to the rendering
    # call.
    def render(path, layout: nil, locals: {})
      resource = sitemap.find_by_request_path(path)
      raise Mascot::PageNotFoundError, "No such page: #{path} in #{sitemap.file_path.expand_path}" if resource.nil?

      # Users may set the layout from frontmatter.
      layout ||= resource.data.fetch("layout", controller_layout)
      type = resource.template_extensions.last
      locals = locals.merge(sitemap: sitemap, current_page: resource)

      # @_mascot_locals variable is used by the wrap_template helper.
      controller.instance_variable_set(:@_mascot_locals, locals)
      controller.render inline: resource.body,
        type: type,
        layout: layout,
        locals: locals,
        content_type: resource.mime_type.to_s
    end

    private
    # Returns the current layout for the inline Mascot renderer.
    def controller_layout
      layout = controller.send(:_layout)
      if layout.instance_of? String
        layout
      else
        File.basename(layout.identifier).split('.').first
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mascot-rails-0.1.5 lib/mascot/action_controller_context.rb