Module: Cybertrain::Views

Defined in:
cybertrain/views.rb,
cybertrain/template/interpreter.rb

Defined Under Namespace

Classes: OutsideRequest

Class Method Summary collapse

Class Method Details

.component_builder ⇒ Object

->(name String, kwargs Hash<String, polymorphic>) { Component }: how render component: "card", title: "x" builds the component.



126
127
128
# File 'cybertrain/views.rb', line 126

def self.component_builder
  @component_builder
end

.component_builder=(builder) ⇒ Object



131
132
133
# File 'cybertrain/views.rb', line 131

def self.component_builder=(builder)
  @component_builder = builder
end

.helpers_builder ⇒ Object

->(controller Controller | nil) { Template::Helpers }: how controllers and render_partial build the helpers a template calls. gen/helpers.rb sets it to build the app’s ApplicationHelpers (spec 3.1).



114
115
116
# File 'cybertrain/views.rb', line 114

def self.helpers_builder
  @helpers_builder
end

.helpers_builder=(builder) ⇒ Object



119
120
121
# File 'cybertrain/views.rb', line 119

def self.helpers_builder=(builder)
  @helpers_builder = builder
end

.render_component(component, content = "") ⇒ String

A component rendered with no request, content (HTML, not escaped) as its block:

Cybertrain::Views.render_component(CardComponent.new(title: “x”))

A ComponentCollection (CommentComponent.with_collection(comments)) renders each item’s component, joined; content is not passed to them.

Parameters:

Returns:

  • (String)


205
206
207
208
209
# File 'cybertrain/views.rb', line 205

def self.render_component(component, content = "")
  engine = configured_engine
  helpers = detached_helpers
  component.render_with(helpers, content, engine.interpreter(helpers)).to_s
end

.render_mail(name, locals = {}) ⇒ String

A mail body rendered with no request: the partial as render_partial renders it, wrapped in app/views/layouts/mail.html.erb (its <%= yield %>) when the views have that layout, else the partial alone.

html = Cybertrain::Views.render_mail(“mail/welcome”, user: user) Cybertrain::Mailer.deliver_later(to: user.email, subject: “Welcome”, body: text, html: html)

“mail/welcome” is app/views/mail/_welcome.html.erb.

Parameters:

  • name (String) —

    the partial, as for render_partial

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

    its locals

Returns:

  • (String)


190
191
192
193
# File 'cybertrain/views.rb', line 190

def self.render_mail(name, locals = {})
  layout = configured_engine.exists?(MAIL_LAYOUT) ? MAIL_LAYOUT : ""
  render_detached(name, locals, layout)
end

.render_partial(name, locals = {}) ⇒ String

A partial rendered with no request (a broadcast, a mail body):

Cybertrain::Views.render_partial(“articles/article”, article: article)

“articles/article” is app/views/articles/_article.html.erb. The helpers are the app’s (helpers_builder) with no controller: path helpers work (url_resolver is process-wide), the request-bound ones (csrf_token, csrf_meta_tags, params, flash, request_path, *_url) raise OutsideRequest. Strict locals are checked as in a request.

Returns:

  • (String)


146
147
148
# File 'cybertrain/views.rb', line 146

def self.render_partial(name, locals = {})
  render_detached(name, locals, "")
end