Sha256: ea24cd20c2d00665b02614c22b379d46986a0fbce03f8d5bad3ea643792f8ae6

Contents?: true

Size: 1.85 KB

Versions: 3

Compression:

Stored size: 1.85 KB

Contents

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

    def initialize(controller: , resources: )
      @controller = controller
      @resources = resources
    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(resource = find_resource, layout: nil, locals: {})
      # Users may set the layout from frontmatter.
      layout ||= resource.data.fetch("layout", controller_layout)
      type = resource.asset.template_extensions.last
      locals = locals.merge(current_page: resource, resources: resources)

      # @_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

    # Mascot::PageNotFoundError is handled in the default Mascot::SitemapController
    # with an execption that Rails can use to display a 404 error.
    def get(path)
      resource = resources.get(path)
      if resource.nil?
        # TODO: Display error in context of Reources class root.
        raise Mascot::PageNotFoundError, "No such page: #{path}"
      else
        resource
      end
    end

    # Default finder of the resource for the current controller context.###
    def find_resource
      get controller.params[:path]
    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

3 entries across 3 versions & 1 rubygems

Version Path
mascot-rails-0.1.8 lib/mascot/action_controller_context.rb
mascot-rails-0.1.7 lib/mascot/action_controller_context.rb
mascot-rails-0.1.6 lib/mascot/action_controller_context.rb