Sha256: 1a2bd92fe39ff13c77400638cdf2b15d5b344e1c0e7d4e7eaf046c4c6d4ad860

Contents?: true

Size: 880 Bytes

Versions: 8

Compression:

Stored size: 880 Bytes

Contents

module Coco
  class PresentedComponent < ViewComponent::Base
    include Coco::ComponentsHelper

    attr_accessor :subject

    def render?
      !subject.nil?
    end

    class << self
      # Allow initializing PresentedComponent instances by
      # optionally passing the subject as the first argument.
      # Ensures presented components can still be rendered using
      # ViewComponent's `.with_collection` method.
      #
      # Example.new(example_model, **opts)
      def new(subject = :none, **kwargs)
        if subject == :none
          subject = kwargs.fetch(collection_parameter.to_sym) do
            raise ArgumentError, "Presented components require a subject to be provided when initializing"
          end
        end

        instance = kwargs.any? ? super : super(subject)
        instance.subject = subject
        instance
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
coveragebook_components-0.19.8 app/components/coco/presented_component.rb
coveragebook_components-0.19.7 app/components/coco/presented_component.rb
coveragebook_components-0.19.6 app/components/coco/presented_component.rb
coveragebook_components-0.19.5 app/components/coco/presented_component.rb
coveragebook_components-0.19.4 app/components/coco/presented_component.rb
coveragebook_components-0.19.3 app/components/coco/presented_component.rb
coveragebook_components-0.19.2 app/components/coco/presented_component.rb
coveragebook_components-0.19.1 app/components/coco/presented_component.rb