Class: Scrapbook::HelperForTemplateView

Inherits:
Object
  • Object
show all
Defined in:
app/helpers/scrapbook/helper_for_template_view.rb

Overview

Implementation of methods that can be used in views by accessing the ‘sb` helper method.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(view, scrapbook, pathname) ⇒ HelperForTemplateView

Returns a new instance of HelperForTemplateView.



10
11
12
13
14
# File 'app/helpers/scrapbook/helper_for_template_view.rb', line 10

def initialize(view, scrapbook, pathname)
  self.view = view
  self.scrapbook = scrapbook
  self.pathname = pathname
end

Instance Attribute Details

#pathnameObject

Returns the value of attribute pathname.



6
7
8
# File 'app/helpers/scrapbook/helper_for_template_view.rb', line 6

def pathname
  @pathname
end

#scrapbookObject

Returns the value of attribute scrapbook.



6
7
8
# File 'app/helpers/scrapbook/helper_for_template_view.rb', line 6

def scrapbook
  @scrapbook
end

Instance Method Details

#render_source(template: nil, partial: nil) ⇒ String

Takes in a relative path to a scrapbook page and renders its source code. You specify the path as either a “template” or “partial” path similarly to how you would render a view in Rails. For example, ‘partial: “my/path/to/partial”` would look for a Scrapbook page named something like “my/path/to/_partial.html.erb” in the view paths.

Parameters:

  • template (String) (defaults to: nil)

    the relative path to a Scrapbook page similar to the path you would give when rendering a Rails template. (Leave ‘nil` if specifying a partial.)

  • partial (String) (defaults to: nil)

    the relative path to a Scrapbook page similar to the path you would give when rendering a Rails partial. (Leave ‘nil` if specifying a template.)

Returns:

  • (String)

    the source code of a file wrapped in “<pre><code>” tags.

Raises:

  • (ArgumentError)


26
27
28
29
30
31
32
# File 'app/helpers/scrapbook/helper_for_template_view.rb', line 26

def render_source(template: nil, partial: nil)
  raise ArgumentError, 'Missing named parameter of either "partial" or "template"' if partial.nil? && template.nil?
  raise ArgumentError, 'Can only pass one of either "partial" or "template"' if !partial.nil? && !template.nil?

  view_name = pathname.relative_path_from(scrapbook.root).dirname.join(partial || template).to_s
  tag.pre(tag.code(view.lookup_context.find(view_name, [], !partial.nil?).source))
end

#render_with_source(template: nil, partial: nil, source_first: false, locals: {}) ⇒ String

Takes in a relative path to a scrapbook page and renders its source code followed by rendering the page itself. You specify the page as either a “template” or “partial” path similarly to how you would render a view in Rails, and you can pass in ‘locals` too.

Parameters:

  • source_first (Boolean) (defaults to: false)

    when true, the source code is displayed before the rendered view. (Default: false)

  • locals (Hash) (defaults to: {})

    a key-value hash whose keys are the names of locals to assign when the page is rendered.

  • template (String) (defaults to: nil)

    the relative path to a Scrapbook page similar to the path you would give when rendering a Rails template. (Leave ‘nil` if specifying a partial.)

  • partial (String) (defaults to: nil)

    the relative path to a Scrapbook page similar to the path you would give when rendering a Rails partial. (Leave ‘nil` if specifying a template.)

Returns:

  • (String)

    the source code of a file wrapped in “<pre><code>” tags followed by rendering the page itself wrapped in a “<div>”.



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/helpers/scrapbook/helper_for_template_view.rb', line 46

def render_with_source(template: nil, partial: nil, source_first: false, locals: {})
  # NOTE: Parameter validation of "template" and "partial" handled `render_source`.
  source = render_source(template: template, partial: partial)
  render_params = {locals: locals}
  render_params[partial.nil? ? :template : :partial] =
    pathname.relative_path_from(scrapbook.root).dirname.join(partial || template).to_s

  if source_first
    source + tag.div(view.render(**render_params))
  else
    view.render(**render_params) + tag.div(source)
  end
end