Class: ActionView::Component::Base
- Includes:
- ActiveModel::Validations
- Defined in:
- lib/action_view/component/base.rb
Defined Under Namespace
Classes: DummyTemplate
Class Method Summary collapse
-
.compile ⇒ Object
Compile template to #call instance method, assuming it hasn’t been compiled already.
- .inherited(child) ⇒ Object
Instance Method Summary collapse
-
#initialize ⇒ Base
constructor
A new instance of Base.
-
#render_in(view_context, *args, &block) ⇒ Object
Entrypoint for rendering components.
Methods included from Base::RenderMonkeyPatch
Constructor Details
#initialize ⇒ Base
Returns a new instance of Base.
66 |
# File 'lib/action_view/component/base.rb', line 66 def initialize(*); end |
Class Method Details
.compile ⇒ Object
Compile template to #call instance method, assuming it hasn’t been compiled already. We could in theory do this on app boot, at least in production environments. Right now this just compiles the template the first time the component is rendered.
78 79 80 81 82 83 84 |
# File 'lib/action_view/component/base.rb', line 78 def compile return if @compiled && ActionView::Base.cache_template_loading class_eval("def call; @output_buffer = ActionView::OutputBuffer.new; #{compiled_template}; end") @compiled = true end |
.inherited(child) ⇒ Object
69 70 71 72 73 |
# File 'lib/action_view/component/base.rb', line 69 def inherited(child) child.include Rails.application.routes.url_helpers unless child < Rails.application.routes.url_helpers super end |
Instance Method Details
#render_in(view_context, *args, &block) ⇒ Object
Entrypoint for rendering components. Called by ActionView::Base#render.
view_context: ActionView context from calling view args(hash): params to be passed to component being rendered block: optional block to be captured within the view context
returns HTML that has been escaped by the respective template handler
Example subclass:
app/components/my_component.rb: class MyComponent < ActionView::Component::Base
def initialize(title:)
@title = title
end
end
app/components/my_component.html.erb <span title=“<%= @title %>”>Hello, <%= content %>!</span>
In use: <%= render MyComponent, title: “greeting” do %>world<% end %> returns: <span title=“greeting”>Hello, world!</span>
59 60 61 62 63 64 |
# File 'lib/action_view/component/base.rb', line 59 def render_in(view_context, *args, &block) self.class.compile @content = view_context.capture(&block) if block_given? validate! call end |