Module: Bridgetown::ViewComponentHelpers

Extended by:
Forwardable
Defined in:
lib/bridgetown-view-component/bridgetown/view_component_helpers.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, **kwargs, &block) ⇒ Object


50
51
52
53
54
55
56
# File 'lib/bridgetown-view-component/bridgetown/view_component_helpers.rb', line 50

def method_missing(method, *args, **kwargs, &block)
  if helpers.respond_to?(method.to_sym)
    helpers.send method.to_sym, *args, **kwargs, &block
  else
    super
  end
end

Instance Attribute Details

#siteObject (readonly)

will be nil unless you explicitly set a ‘@site` ivar


9
10
11
# File 'lib/bridgetown-view-component/bridgetown/view_component_helpers.rb', line 9

def site
  @site
end

Class Method Details

.allow_rails_helpers(*helpers) ⇒ Object


15
16
17
# File 'lib/bridgetown-view-component/bridgetown/view_component_helpers.rb', line 15

def self.allow_rails_helpers(*helpers)
  helper_allow_list.concat(helpers)
end

.helper_allow_listObject


11
12
13
# File 'lib/bridgetown-view-component/bridgetown/view_component_helpers.rb', line 11

def self.helper_allow_list
  @helper_allow_list ||= %i(capture render t with_output_buffer)
end

.included(klass) ⇒ Object


19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/bridgetown-view-component/bridgetown/view_component_helpers.rb', line 19

def self.included(klass)
  helper_consts = ActionView::Helpers.constants.select do |c|
    ActionView::Helpers.const_get(c).is_a?(Module)
  end
  helper_consts.map { |c| ActionView::Helpers.const_get(c) }.each do |mod|
    (mod.public_instance_methods - Object.public_instance_methods).each do |method_name|
      klass.undef_method(method_name) unless helper_allow_list.include?(method_name)
    rescue NameError
      nil
    end
  end
end

Instance Method Details

#helpersObject


44
45
46
47
48
# File 'lib/bridgetown-view-component/bridgetown/view_component_helpers.rb', line 44

def helpers
  @helpers ||= Bridgetown::RubyTemplateView::Helpers.new(
    self, view_context&.site || Bridgetown::Current.site
  )
end

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


32
33
34
35
36
37
38
39
40
41
42
# File 'lib/bridgetown-view-component/bridgetown/view_component_helpers.rb', line 32

def render(item, options = {}, &block)
  if item.respond_to?(:render_in)
    result = ""
    capture do # this ensures no leaky interactions between BT<=>VC blocks
      result = item.render_in(self, &block)
    end
    result&.html_safe
  else
    view_context.partial(item, options, &block)&.html_safe
  end
end

#respond_to_missing?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)

58
59
60
# File 'lib/bridgetown-view-component/bridgetown/view_component_helpers.rb', line 58

def respond_to_missing?(method, include_private = false)
  helpers.respond_to?(method.to_sym, include_private) || super
end