Class: Blacklight::DocumentComponent
- Includes:
- ContentAreasShim
- Defined in:
- app/components/blacklight/document_component.rb
Overview
Note:
when subclassing this component, if you override the initializer, you must explicitly specify the counter variable ‘document_counter` even if you don’t use it. Otherwise view_component will not provide the count value when calling the component.
A component for rendering a single document
Constant Summary collapse
- COLLECTION_INDEX_OFFSET =
ViewComponent 3 changes iteration counters to begin at 0 rather than 1
ViewComponent::VERSION::MAJOR < 3 ? 0 : 1
Constants inherited from Component
Instance Method Summary collapse
- #before_render ⇒ Object
-
#classes ⇒ Object
HTML classes to apply to the root element.
-
#initialize(document: nil, presenter: nil, partials: nil, id: nil, classes: [], component: :article, title_component: nil, metadata_component: nil, embed_component: nil, thumbnail_component: nil, counter: nil, document_counter: nil, counter_offset: 0, show: false, **args) ⇒ DocumentComponent
constructor
rubocop:disable Metrics/ParameterLists.
- #render_partials? ⇒ Boolean
Methods included from ContentAreasShim
Methods inherited from Component
Constructor Details
#initialize(document: nil, presenter: nil, partials: nil, id: nil, classes: [], component: :article, title_component: nil, metadata_component: nil, embed_component: nil, thumbnail_component: nil, counter: nil, document_counter: nil, counter_offset: 0, show: false, **args) ⇒ DocumentComponent
rubocop:disable Metrics/ParameterLists
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'app/components/blacklight/document_component.rb', line 100 def initialize(document: nil, presenter: nil, partials: nil, id: nil, classes: [], component: :article, title_component: nil, metadata_component: nil, embed_component: nil, thumbnail_component: nil, counter: nil, document_counter: nil, counter_offset: 0, show: false, **args) if presenter.nil? && document.nil? raise ArgumentError, 'missing keyword: :document or :presenter' end if document.is_a?(Blacklight::DocumentPresenter) && presenter.nil? @presenter = document @document = @presenter.document || args[self.class.collection_parameter] else @document = document || presenter&.document || args[self.class.collection_parameter] @presenter = presenter end @component = component @title_component = title_component @id = id || ('document' if show) @classes = classes Deprecation.warn(Blacklight::DocumentComponent, 'Passing embed_component is deprecated') if @embed_component.present? @embed_component = Deprecation.warn(Blacklight::DocumentComponent, 'Passing metadata_component is deprecated') if @metadata_component.present? @metadata_component = Deprecation.warn(Blacklight::DocumentComponent, 'Passing thumbnail_component is deprecated') if @thumbnail_component.present? @thumbnail_component = thumbnail_component @counter = counter @document_counter = document_counter || args.fetch(self.class.collection_counter_parameter, nil) @counter ||= @document_counter + COLLECTION_INDEX_OFFSET + counter_offset if @document_counter.present? @show = show @view_partials = partials end |
Instance Method Details
#before_render ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'app/components/blacklight/document_component.rb', line 152 def before_render set_slot(:title, nil) unless title set_slot(:thumbnail, nil, component: @thumbnail_component || presenter.view_config&.thumbnail_component) unless thumbnail || show? set_slot(:metadata, nil, component: @metadata_component || presenter&.view_config&., fields: presenter.field_presenters, show: @show) unless set_slot(:embed, nil, component: @embed_component || presenter.view_config&.) unless # Blacklight 8 allows applications to pass in the partials to render instead of requiring the template to render the slots. if partials.empty? && view_partials.present? # rubocop:disable Style/GuardClause @render_partials = true view_partials.each do |view_partial| with_partial(view_partial) do helpers.render_document_partial @document, view_partial, component: self, document_counter: @counter end end end end |
#classes ⇒ Object
HTML classes to apply to the root element
143 144 145 146 147 148 149 150 |
# File 'app/components/blacklight/document_component.rb', line 143 def classes [ @classes, helpers.render_document_class(@document), 'document', ("document-position-#{@counter}" if @counter) ].compact.flatten end |
#render_partials? ⇒ Boolean
169 170 171 |
# File 'app/components/blacklight/document_component.rb', line 169 def render_partials? @render_partials || presenter.view_config&.render_partials_in_component? end |