Class: ActionView::Component::Base

Inherits:
Base
  • Object
show all
Includes:
ActionController::RequestForgeryProtection, ActiveModel::Validations, ActiveSupport::Configurable
Defined in:
lib/action_view/component/base.rb

Defined Under Namespace

Classes: DummyTemplate

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBase



74
# File 'lib/action_view/component/base.rb', line 74

def initialize(*); end

Class Method Details

.compileObject

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.



108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/action_view/component/base.rb', line 108

def compile
  return if @compiled && ActionView::Base.cache_template_loading
  ensure_initializer_defined

  class_eval <<-RUBY, __FILE__, __LINE__ + 1
    def call
      @output_buffer = ActionView::OutputBuffer.new
      #{compiled_template}
    end
  RUBY

  @compiled = true
end

.inherited(child) ⇒ Object



95
96
97
98
99
# File 'lib/action_view/component/base.rb', line 95

def inherited(child)
  child.include Rails.application.routes.url_helpers unless child < Rails.application.routes.url_helpers

  super
end

.source_locationObject



101
102
103
# File 'lib/action_view/component/base.rb', line 101

def source_location
  instance_method(:initialize).source_location[0]
end

Instance Method Details

#controllerObject



84
85
86
# File 'lib/action_view/component/base.rb', line 84

def controller
  @controller ||= view_context.controller
end

#render(options = {}, args = {}, &block) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/action_view/component/base.rb', line 76

def render(options = {}, args = {}, &block)
  if options.is_a?(String) || (options.is_a?(Hash) && options.has_key?(:partial))
    view_context.render(options, args, &block)
  else
    super
  end
end

#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>



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/action_view/component/base.rb', line 61

def render_in(view_context, *args, &block)
  self.class.compile
  @view_context = view_context
  @view_renderer ||= view_context.view_renderer
  @lookup_context ||= view_context.lookup_context
  @view_flow ||= view_context.view_flow
  @virtual_path ||= virtual_path

  @content = view_context.capture(&block) if block_given?
  validate!
  call
end

#virtual_pathObject

Looks for the source file path of the initialize method of the instance’s class. Removes the first part of the path and the extension.



90
91
92
# File 'lib/action_view/component/base.rb', line 90

def virtual_path
  self.class.source_location.gsub(%r{(.*app/)|(.rb)}, "")
end