Class: Mascot::ActionControllerContext

Inherits:
Object
  • Object
show all
Defined in:
lib/mascot/action_controller_context.rb

Overview

Renders a mascot page via the params path via ActionController.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(controller:, resources:) ⇒ ActionControllerContext

Returns a new instance of ActionControllerContext.



6
7
8
9
# File 'lib/mascot/action_controller_context.rb', line 6

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

Instance Attribute Details

#controllerObject (readonly)

Returns the value of attribute controller.



4
5
6
# File 'lib/mascot/action_controller_context.rb', line 4

def controller
  @controller
end

#resourcesObject (readonly)

Returns the value of attribute resources.



4
5
6
# File 'lib/mascot/action_controller_context.rb', line 4

def resources
  @resources
end

Instance Method Details

#find_resourceObject

Default finder of the resource for the current controller context.###



42
43
44
# File 'lib/mascot/action_controller_context.rb', line 42

def find_resource
  get controller.params[:resource_path]
end

#get(path) ⇒ Object

Mascot::PageNotFoundError is handled in the default Mascot::SiteController with an execption that Rails can use to display a 404 error.



31
32
33
34
35
36
37
38
39
# File 'lib/mascot/action_controller_context.rb', line 31

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

#render(resource = find_resource, layout: nil, locals: {}) ⇒ Object

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.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/mascot/action_controller_context.rb', line 14

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