Sha256: f6c9180b94d89bc89fad0e6e1fba1268c46a7488d599cbca883b692427f3dede

Contents?: true

Size: 1.22 KB

Versions: 30

Compression:

Stored size: 1.22 KB

Contents

# frozen_string_literal: true

module Koi
  module Header
    class ShowComponent < ViewComponent::Base
      attr_reader :resource

      delegate :with_breadcrumb, :with_action, to: :@header

      def initialize(resource:, title: nil)
        super

        @title    = title
        @resource = resource

        @header = HeaderComponent.new(title: self.title)
      end

      def call
        render @header do |header|
          # render block, if any (delegating slots to header)
          content

          # add our breadcrumbs and actions
          add_index(header)
          add_edit(header)
        end
      end

      def title
        title = Koi.config.resource_name_candidates.reduce(@title) do |name, key|
          name || (resource.public_send(key) if resource.respond_to?(key))
        end

        title.presence || resource.model_name.human
      end

      def add_index(header)
        header.with_breadcrumb(resource.model_name.human.pluralize, url_for(action: :index))
      rescue ActionController::UrlGenerationError
        nil
      end

      def add_edit(header)
        header.with_action("Edit", url_for(action: :edit))
      rescue ActionController::UrlGenerationError
        nil
      end
    end
  end
end

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
katalyst-koi-4.15.1 app/components/koi/header/show_component.rb
katalyst-koi-4.15.0 app/components/koi/header/show_component.rb
katalyst-koi-4.14.3 app/components/koi/header/show_component.rb
katalyst-koi-4.14.2 app/components/koi/header/show_component.rb
katalyst-koi-4.14.1 app/components/koi/header/show_component.rb
katalyst-koi-4.14.0 app/components/koi/header/show_component.rb
katalyst-koi-4.13.2 app/components/koi/header/show_component.rb
katalyst-koi-4.13.0 app/components/koi/header/show_component.rb
katalyst-koi-4.12.6 app/components/koi/header/show_component.rb
katalyst-koi-4.12.5 app/components/koi/header/show_component.rb
katalyst-koi-4.12.4 app/components/koi/header/show_component.rb
katalyst-koi-4.12.3 app/components/koi/header/show_component.rb
katalyst-koi-4.12.2 app/components/koi/header/show_component.rb
katalyst-koi-4.12.1 app/components/koi/header/show_component.rb
katalyst-koi-4.12.0 app/components/koi/header/show_component.rb
katalyst-koi-4.11.2 app/components/koi/header/show_component.rb
katalyst-koi-4.11.1 app/components/koi/header/show_component.rb
katalyst-koi-4.11.0 app/components/koi/header/show_component.rb
katalyst-koi-4.10.3 app/components/koi/header/show_component.rb
katalyst-koi-4.10.2 app/components/koi/header/show_component.rb