Sha256: 49fa5ba28f502af0ecaa96d51290fc748c5c588cfac0007ec6c1029a4efb6c3b

Contents?: true

Size: 882 Bytes

Versions: 1

Compression:

Stored size: 882 Bytes

Contents

# frozen_string_literal: true

module ViewComponent::UseHelpers
  extend ActiveSupport::Concern

  class_methods do
    def use_helpers(*args, from: nil)
      args.each { |helper_method| use_helper(helper_method, from: from) }
    end

    def use_helper(helper_method, from: nil)
      class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
        def #{helper_method}(*args, &block)
          raise HelpersCalledBeforeRenderError if view_context.nil?

          #{define_helper(helper_method: helper_method, source: from)}
        end
      RUBY
      ruby2_keywords(helper_method) if respond_to?(:ruby2_keywords, true)
    end

    private

    def define_helper(helper_method:, source:)
      return "__vc_original_view_context.#{helper_method}(*args, &block)" unless source.present?

      "#{source}.instance_method(:#{helper_method}).bind(self).call(*args, &block)"
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
view_component-3.13.0 lib/view_component/use_helpers.rb