Class: Koi::Header::EditComponent

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of EditComponent.



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

def initialize(resource:, title: nil)
  super

  @resource = resource
  @title = title

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

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



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

def model
  @model
end

#resourceObject (readonly)

Returns the value of attribute resource.



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

def resource
  @resource
end

Instance Method Details

#add_index(header) ⇒ Object



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

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

#add_show(header) ⇒ Object



51
52
53
54
55
# File 'app/components/koi/header/edit_component.rb', line 51

def add_show(header)
  header.with_breadcrumb(resource_title, url_for(action: :show))
rescue ActionController::UrlGenerationError
  nil
end

#callObject



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

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

#resource_titleObject



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

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

  title.presence || resource.model_name.human
end

#titleObject



33
34
35
# File 'app/components/koi/header/edit_component.rb', line 33

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