Class: Blacklight::Document::ActionComponent

Inherits:
Component
  • Object
show all
Defined in:
app/components/blacklight/document/action_component.rb

Overview

Render a bookmark widget to bookmark / unbookmark a document

Direct Known Subclasses

BookmarkComponent

Constant Summary

Constants inherited from Component

Component::EXCLUDE_VARIABLES

Instance Method Summary collapse

Methods inherited from Component

compiler, config, #inspect

Constructor Details

#initialize(document:, action:, options: {}, url_opts: {}, id: nil, link_classes: 'nav-link') ⇒ ActionComponent

Returns a new instance of ActionComponent.

Parameters:



10
11
12
13
14
15
16
17
# File 'app/components/blacklight/document/action_component.rb', line 10

def initialize(document:, action:, options: {}, url_opts: {}, id: nil, link_classes: 'nav-link')
  @document = document
  @action = action
  @options = options
  @url_opts = url_opts
  @id = id || @action.fetch(:id, "#{@action.name}Link")
  @link_classes = link_classes
end

Instance Method Details

#keyObject



64
65
66
# File 'app/components/blacklight/document/action_component.rb', line 64

def key
  @action.key
end

#labelObject



42
43
44
# File 'app/components/blacklight/document/action_component.rb', line 42

def label
  t("blacklight.tools.#{@action.name}", default: @action.label || @action.name.to_s.humanize)
end


29
30
31
32
33
34
35
# File 'app/components/blacklight/document/action_component.rb', line 29

def link_to_modal_control
  link_to label,
          url,
          id: @id,
          class: @link_classes,
          data: {}.merge(({ blacklight_modal: "trigger", turbo: false } if @action.modal != false) || {})
end

#render_controlObject



19
20
21
22
23
# File 'app/components/blacklight/document/action_component.rb', line 19

def render_control
  return link_to_modal_control if using_default_document_action?

  render_partial
end

#render_partialObject



37
38
39
40
# File 'app/components/blacklight/document/action_component.rb', line 37

def render_partial
  render(partial: @action.partial || @action.name.to_s,
         locals: { document: @document, document_action_config: @action }.merge(@options))
end

#urlObject

Action buttons get their URLs in one of three ways:

  • the action configuration explicitly specifies a helper method to call

  • a url route is inferred for ActiveModel-compliant objects (the default;

    note that, although Rails routing is available here, we still call out to
    helpers regardless, because that's where applications might have overridden the
    default Rails routing behavior)
    
  • calling out to an implicit helper method with a conventional name (unlikely)



53
54
55
56
57
58
59
60
61
62
# File 'app/components/blacklight/document/action_component.rb', line 53

def url
  url_opts = @url_opts.merge(({ id: @document } if @document) || {})
  if @action.path
    helpers.public_send(@action.path, url_opts)
  elsif url_opts[:id].class.respond_to?(:model_name)
    helpers.url_for([@action.key.to_sym, url_opts[:id]])
  else
    helpers.public_send("#{@action.key}_#{helpers.controller_name}_path", url_opts)
  end
end

#using_default_document_action?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'app/components/blacklight/document/action_component.rb', line 25

def using_default_document_action?
  @action.component || @action.partial == 'document_action'
end