Class: Scrapbook::HelperForTemplateView
- Inherits:
-
Object
- Object
- Scrapbook::HelperForTemplateView
- 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
-
#pathname ⇒ Object
readonly
Returns the value of attribute pathname.
-
#scrapbook ⇒ Object
readonly
Returns the value of attribute scrapbook.
Instance Method Summary collapse
-
#initialize(view, scrapbook, pathname) ⇒ HelperForTemplateView
constructor
A new instance of HelperForTemplateView.
-
#render_source(template: nil, partial: nil) ⇒ String
Takes in a relative path to a scrapbook page and renders its source code.
-
#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.
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
#pathname ⇒ Object
Returns the value of attribute pathname.
6 7 8 |
# File 'app/helpers/scrapbook/helper_for_template_view.rb', line 6 def pathname @pathname end |
#scrapbook ⇒ Object
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.
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.
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 |