Method: Satis::ApplicationComponent.add_helper

Defined in:
app/components/satis/application_component.rb

.add_helper(name, component) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/components/satis/application_component.rb', line 24

def self.add_helper(name, component)
  if respond_to?(name)
    Satis.config.logger.warn("Helper #{name} already defined, skipping.")
    return
  end
  define_method(name) do |*args, **kwargs, &block|
    original_args = args.dup
    options = args.extract_options!
    instance = if options.key? :variant
      variant_component = component.to_s.sub(/::Component$/, "::#{options[:variant].to_s.camelize}::Component").safe_constantize
      (variant_component || component).new(*original_args, **kwargs)
    else
      kwargs[component_name.to_sym] = self
      component.new(*original_args, **kwargs)
    end
    original_view_context.render(instance, &block)
  end
end