Class: Decidim::ComponentAttachmentCollectionPresenter

Inherits:
SimpleDelegator
  • Object
show all
Includes:
TranslatableAttributes
Defined in:
app/presenters/decidim/component_attachment_collection_presenter.rb

Overview

Decorator attachments of a component acting as an attachment collection

Instance Method Summary collapse

Methods included from TranslatableAttributes

#default_locale?

Constructor Details

#initialize(resource, opts = {}) ⇒ ComponentAttachmentCollectionPresenter

Returns a new instance of ComponentAttachmentCollectionPresenter.



10
11
12
13
14
# File 'app/presenters/decidim/component_attachment_collection_presenter.rb', line 10

def initialize(resource, opts = {})
  super(resource)

  @include_component_name = opts[:include_component_name]
end

Instance Method Details

#attachmentsObject



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/presenters/decidim/component_attachment_collection_presenter.rb', line 16

def attachments
  @attachments ||= if [resource_model_name, resource_table_name].all?(&:present?)
                     base_query = Decidim::Attachment.joins(resource_join.join_sources)
                                                     .where(resource_table_name => { decidim_component_id: __getobj__.id })
                     if resource_model&.include?(Decidim::Publicable)
                       base_query.where.not(resource_table_name => { published_at: nil })
                     else
                       base_query
                     end
                   else
                     Decidim::Attachment.none
                   end
end

#descriptionObject



56
# File 'app/presenters/decidim/component_attachment_collection_presenter.rb', line 56

def description; end

#documentsObject



30
31
32
# File 'app/presenters/decidim/component_attachment_collection_presenter.rb', line 30

def documents
  @documents ||= attachments.with_attached_file.order(:weight).select(&:document?)
end

#documents_visible_for(user) ⇒ Object



34
35
36
37
38
# File 'app/presenters/decidim/component_attachment_collection_presenter.rb', line 34

def documents_visible_for(user)
  return documents unless resource_model.respond_to?(:visible_for)

  attachments.merge(resource_model.visible_for(user)).with_attached_file.order(:weight).select(&:document?)
end

#idObject



44
45
46
# File 'app/presenters/decidim/component_attachment_collection_presenter.rb', line 44

def id
  "component-#{__getobj__.id}"
end

#nameObject



48
49
50
51
52
53
54
# File 'app/presenters/decidim/component_attachment_collection_presenter.rb', line 48

def name
  base_name = I18n.t(manifest_name, scope: "decidim.application.documents.component_documents")

  return base_name unless @include_component_name

  "#{base_name} - #{translated_attribute(__getobj__.name)}"
end

#unused?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'app/presenters/decidim/component_attachment_collection_presenter.rb', line 40

def unused?
  documents.blank?
end