Module: Webmachine::ActionView::Resource

Defined in:
lib/webmachine/actionview/resource.rb

Overview

Include on Resource-derived classes to get access to ActionView-style templating.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



5
6
7
8
9
10
# File 'lib/webmachine/actionview/resource.rb', line 5

def self.included(base)
  base.class_eval do
    include ::ActionView::Helpers::CaptureHelper
    include ::ActionView::Context
  end
end

Instance Method Details

#default_template_nameString

The conventional template name for the resource. In a class called HomeResource, this would be 'home'

Returns:

  • (String)

    default template name for this resource



41
42
43
# File 'lib/webmachine/actionview/resource.rb', line 41

def default_template_name
  @default_template_name ||= self.class.name.underscore.sub(/_resource$/, '')
end

#lookup_context::ActionView::Context

The ::ActionView::Context in which views are looked up.

Returns:

  • (::ActionView::Context)


53
54
55
# File 'lib/webmachine/actionview/resource.rb', line 53

def lookup_context
  @lookup_context ||= ::ActionView::LookupContext.new(view_paths)
end

#render(options = nil) ⇒ ::ActionView::OutputBuffer

Renders a template or partial with an optional layout from a Webmachine::ActionView::Resource

Parameters:

  • options (Hash) (defaults to: nil)

    Describe what you want to render. Leave blank to render the #default_template_name

Options Hash (options):

  • :template (String)

    Name of the template to render. Do not use with :partial

  • :partial (String)

    Name of the template to render. Do not use with :template

  • :layout (String)

    Name of the layout within which to render the template.

  • :locals (String)

    Hash of locals to pass to a partial. Defaults to DEFAULT_LAYOUT. Suppress with layout: false or layout: nil

Returns:

  • (::ActionView::OutputBuffer)

    the output content



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/webmachine/actionview/resource.rb', line 22

def render(options = nil)
  options ||= default_template_name
  options = { :template => options } if options.is_a?(String)

  unless options[:partial]
    if options.has_key?(:layout) && !options[:layout]
      options.delete(:layout)
    else
      options.reverse_merge!(layout: Webmachine::ActionView.config.default_layout)
    end
    _prepare_context
  end

  view_renderer.render(self, options)
end

#view_paths::ActionView::PathSet

Lazily created path set. Configure first with Webmachine::ActionView.configure

Returns:

  • (::ActionView::PathSet)

    paths in which to find templates.



47
48
49
# File 'lib/webmachine/actionview/resource.rb', line 47

def view_paths
  @view_paths ||= ::ActionView::PathSet.new(Webmachine::ActionView.config.view_paths)
end

#view_renderer::ActionView::Renderer

The renderer responsible for rendering when #render is called

Returns:

  • (::ActionView::Renderer)

    the renderer



59
60
61
# File 'lib/webmachine/actionview/resource.rb', line 59

def view_renderer
  @view_renderer ||= ::ActionView::Renderer.new(lookup_context)
end