Sha256: 36225621beaf4ac146745ebe2261072acb25797d0f5b5791015a58233016e272

Contents?: true

Size: 1.61 KB

Versions: 22

Compression:

Stored size: 1.61 KB

Contents

# frozen_string_literal: true

module Ariadne
  module Form
    # :nodoc:
    class BaseComponent < Ariadne::BaseComponent
      include Ariadne::ClassNameHelper
      extend Ariadne::Forms::ActsAsComponent

      def self.inherited(base)
        base_path = Ariadne::Forms::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

22 entries across 22 versions & 1 rubygems

Version Path
ariadne_view_components-0.0.85 app/components/ariadne/form/base_component.rb
ariadne_view_components-0.0.84 app/components/ariadne/form/base_component.rb
ariadne_view_components-0.0.83.3 app/components/ariadne/form/base_component.rb
ariadne_view_components-0.0.83.2 app/components/ariadne/form/base_component.rb
ariadne_view_components-0.0.83.1 app/components/ariadne/form/base_component.rb
ariadne_view_components-0.0.83 app/components/ariadne/form/base_component.rb
ariadne_view_components-0.0.82 app/components/ariadne/form/base_component.rb
ariadne_view_components-0.0.81 app/components/ariadne/form/base_component.rb
ariadne_view_components-0.0.80.3 app/components/ariadne/form/base_component.rb
ariadne_view_components-0.0.79.2 app/components/ariadne/form/base_component.rb
ariadne_view_components-0.0.79.1 app/components/ariadne/form/base_component.rb
ariadne_view_components-0.0.79 app/components/ariadne/form/base_component.rb
ariadne_view_components-0.0.78 app/components/ariadne/form/base_component.rb
ariadne_view_components-0.0.77 app/components/ariadne/form/base_component.rb
ariadne_view_components-0.0.76.1 app/components/ariadne/form/base_component.rb
ariadne_view_components-0.0.76 app/components/ariadne/form/base_component.rb
ariadne_view_components-0.0.75 app/components/ariadne/form/base_component.rb
ariadne_view_components-0.0.74.1 app/components/ariadne/form/base_component.rb
ariadne_view_components-0.0.74 app/components/ariadne/form/base_component.rb
ariadne_view_components-0.0.73 app/components/ariadne/form/base_component.rb