lib/mascot/action_controller_context.rb in mascot-rails-0.1.5 vs lib/mascot/action_controller_context.rb in mascot-rails-0.1.6

- old
+ new

@@ -1,36 +1,47 @@ 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 + attr_reader :controller, :resources - def initialize(controller: , sitemap: ) + def initialize(controller: , resources: ) @controller = controller - @sitemap = sitemap + @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(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? - + def render(resource = find_resource, layout: nil, locals: {}) # 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) + 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