Sha256: 9298fbddf5fbec3e2c5d99908d7d2d63ef8547b2d72034983bb44368992f83cf

Contents?: true

Size: 1.55 KB

Versions: 81

Compression:

Stored size: 1.55 KB

Contents

# frozen_string_literal: true

module Primer
  module Forms
    # :nodoc:
    class BaseComponent
      include Primer::ClassNameHelper
      extend ActsAsComponent

      def self.inherited(base)
        base_path = Utils.const_source_location(base.name)

        unless base_path
          warn "Could not identify the template for #{base}"
          return
        end

        dir = File.dirname(base_path)
        base.renders_template File.join(dir, "#{base.name.demodulize.underscore}.html.erb"), :render_template
      end

      delegate :required?, :disabled?, :hidden?, to: :@input

      def perform_render(&block)
        return "" unless render?

        @__prf_content_block = block
        compile_and_render_template
      end

      def content
        return @__prf_content if defined?(@__prf_content_evaluated) && @__prf_content_evaluated

        @__prf_content_evaluated = true
        @__prf_content = capture do
          @__prf_content_block.call
        end
      end

      # :nocov:
      def type
        :component
      end
      # :nocov:

      def input?
        false
      end

      def to_component
        self
      end

      def render?
        true
      end

      private

      def compile_and_render_template
        self.class.compile! unless self.class.instance_methods(false).include?(:render_template)
        render_template
      end

      def content_tag_if(condition, tag, **kwargs, &block)
        if condition
          content_tag(tag, **kwargs, &block)
        else
          capture(&block)
        end
      end
    end
  end
end

Version data entries

81 entries across 81 versions & 2 rubygems

Version Path
openproject-primer_view_components-0.48.2 lib/primer/forms/base_component.rb
openproject-primer_view_components-0.48.1 lib/primer/forms/base_component.rb
openproject-primer_view_components-0.48.0 lib/primer/forms/base_component.rb
openproject-primer_view_components-0.47.1 lib/primer/forms/base_component.rb
openproject-primer_view_components-0.47.0 lib/primer/forms/base_component.rb
primer_view_components-0.34.0 lib/primer/forms/base_component.rb
openproject-primer_view_components-0.46.1 lib/primer/forms/base_component.rb
openproject-primer_view_components-0.46.0 lib/primer/forms/base_component.rb
openproject-primer_view_components-0.45.0 lib/primer/forms/base_component.rb
openproject-primer_view_components-0.44.3 lib/primer/forms/base_component.rb
openproject-primer_view_components-0.44.2 lib/primer/forms/base_component.rb
openproject-primer_view_components-0.44.1 lib/primer/forms/base_component.rb
primer_view_components-0.33.0 lib/primer/forms/base_component.rb
openproject-primer_view_components-0.44.0 lib/primer/forms/base_component.rb
openproject-primer_view_components-0.43.1 lib/primer/forms/base_component.rb
openproject-primer_view_components-0.43.0 lib/primer/forms/base_component.rb
primer_view_components-0.32.0 lib/primer/forms/base_component.rb
openproject-primer_view_components-0.42.0 lib/primer/forms/base_component.rb
primer_view_components-0.31.0 lib/primer/forms/base_component.rb
openproject-primer_view_components-0.41.1 lib/primer/forms/base_component.rb