Class: Koi::Header::ShowComponent

Inherits:
ViewComponent::Base
  • Object
show all
Defined in:
app/components/koi/header/show_component.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource:, title: nil) ⇒ ShowComponent

Returns a new instance of ShowComponent.



10
11
12
13
14
15
16
17
# File 'app/components/koi/header/show_component.rb', line 10

def initialize(resource:, title: nil)
  super

  @title    = title
  @resource = resource

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

Instance Attribute Details

#resourceObject (readonly)

Returns the value of attribute resource.



6
7
8
# File 'app/components/koi/header/show_component.rb', line 6

def resource
  @resource
end

Instance Method Details

#add_edit(header) ⇒ Object



44
45
46
47
48
# File 'app/components/koi/header/show_component.rb', line 44

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

#add_index(header) ⇒ Object



38
39
40
41
42
# File 'app/components/koi/header/show_component.rb', line 38

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

#callObject



19
20
21
22
23
24
25
26
27
28
# File 'app/components/koi/header/show_component.rb', line 19

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

#titleObject



30
31
32
33
34
35
36
# File 'app/components/koi/header/show_component.rb', line 30

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