lib/view_component/base.rb
# Compile templates to instance methods, assuming they haven't been compiled already.
# We could in theory do this on app boot, at least in production environments.
# Right now this just compiles the first time the component is rendered.
Compile templates to instance methods, assuming they haven't been compiled already.
We could in theory do this on app boot, at least in production environments.
Right now this just compiles the first time the component is rendered.
Compile templates to instance methods assuming they haven ' t been compiled already . We could in theory do this on app boot at least in production environments . Right now this just compiles the first time the component is rendered .
def compile(raise_template_errors: false)
        return if compiled?
        if template_errors.present?
          raise ViewComponent::TemplateError.new(template_errors) if raise_template_errors
          return false
        end
        define_singleton_method(:variants) do
          templates.map { |template| template[:variant] } + variants_from_inline_calls(inline_calls)
        end
        define_singleton_method(:collection_counter_parameter_name) do
          "#{collection_parameter_name}_counter".to_sym
        end
        define_singleton_method(:counter_argument_present?) do
          instance_method(:initialize).parameters.map(&:second).include?(collection_counter_parameter_name)
        end
        line_number =
          if ActionView::Base.respond_to?(:annotate_template_file_names) &&
            ActionView::Base.annotate_template_file_names
            -2
          else
            -1
          end
        templates.each do |template|
          class_eval <<-RUBY, template[:path], line_number
            def #{call_method_name(template[:variant])}
              @output_buffer = ActionView::OutputBuffer.new
              #{compiled_template(template[:path])}
            end
          RUBY
        end
        @compiled = true
      end
def compile ( raise_template_errors : false ) return if compiled? if template_errors . present? raise ViewComponent :: TemplateError . new ( template_errors ) if raise_template_errors return false end define_singleton_method ( :variants ) do templates . map { | template | template [ :variant ] } + variants_from_inline_calls ( inline_calls ) end define_singleton_method ( :collection_counter_parameter_name ) do "#{collection_parameter_name}_counter" . to_sym end define_singleton_method ( :counter_argument_present? ) do instance_method ( :initialize ) . parameters . map ( & :second ) . include? ( collection_counter_parameter_name ) end line_number = if ActionView :: Base . respond_to? ( :annotate_template_file_names ) && ActionView :: Base . annotate_template_file_names - 2 else - 1 end templates . each do | template | class_eval <<-RUBY , template [ :path ] , line_number 
            def  #{ call_method_name ( template [ :variant ] ) } 
              @output_buffer = ActionView::OutputBuffer.new
               #{ compiled_template ( template [ :path ] ) } 
            end
           RUBY end @compiled = true end
Compile templates to instance methods, assuming they haven't been compiled already.
Compile templates to instance methods assuming they haven ' t been compiled already .
# If template name annotations are turned on, a line is dynamically # added with a comment. In this case, we want to return a different # starting line number so errors that are raised will point to the # correct line in the component template.
raise_template_errors
<not_specific>