Sha256: 913aa2e4a1fc4561269e77775b7cf9772c8993e9988577449cbb4ced039cce96

Contents?: true

Size: 1.39 KB

Versions: 35

Compression:

Stored size: 1.39 KB

Contents

# frozen_string_literal: true

module Koi
  module Header
    class EditComponent < ViewComponent::Base
      attr_reader :model, :resource

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

      def initialize(resource:, title: nil)
        super

        @resource = resource
        @title = title

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

      def call
        render @header do |header|
          # capture nested component
          @header = header

          # render block, if any (delegating slots to header)
          content

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

      def title
        @title || "Edit #{resource.model_name.human.downcase}"
      end

      def resource_title
        title = Koi.config.resource_name_candidates.reduce(nil) do |name, key|
          name || resource.respond_to?(key) && resource.public_send(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_show(header)
        header.with_breadcrumb(resource_title, url_for(action: :show))
      rescue ActionController::UrlGenerationError
        nil
      end
    end
  end
end

Version data entries

35 entries across 35 versions & 1 rubygems

Version Path
katalyst-koi-4.7.3 app/components/koi/header/edit_component.rb
katalyst-koi-4.6.0 app/components/koi/header/edit_component.rb
katalyst-koi-4.5.9 app/components/koi/header/edit_component.rb
katalyst-koi-4.5.8 app/components/koi/header/edit_component.rb
katalyst-koi-4.5.7 app/components/koi/header/edit_component.rb
katalyst-koi-4.5.6 app/components/koi/header/edit_component.rb
katalyst-koi-4.5.5 app/components/koi/header/edit_component.rb
katalyst-koi-4.5.4 app/components/koi/header/edit_component.rb
katalyst-koi-4.5.3 app/components/koi/header/edit_component.rb
katalyst-koi-4.5.2 app/components/koi/header/edit_component.rb
katalyst-koi-4.5.1 app/components/koi/header/edit_component.rb
katalyst-koi-4.5.0 app/components/koi/header/edit_component.rb
katalyst-koi-4.5.0.beta.2 app/components/koi/header/edit_component.rb
katalyst-koi-4.5.0.beta.1 app/components/koi/header/edit_component.rb
katalyst-koi-4.4.1 app/components/koi/header/edit_component.rb
katalyst-koi-4.4.0 app/components/koi/header/edit_component.rb
katalyst-koi-4.3.5 app/components/koi/header/edit_component.rb
katalyst-koi-4.3.4 app/components/koi/header/edit_component.rb
katalyst-koi-4.3.3 app/components/koi/header/edit_component.rb
katalyst-koi-4.3.2 app/components/koi/header/edit_component.rb