Class: Decidim::EnhancedTextwork::ParagraphPresenter

Inherits:
ResourcePresenter
  • Object
show all
Includes:
ActionView::Helpers::UrlHelper
Defined in:
app/presenters/decidim/enhanced_textwork/paragraph_presenter.rb

Overview

Decorator for paragraphs

Direct Known Subclasses

CollaborativeDraftPresenter

Instance Method Summary collapse

Instance Method Details

#authorObject



12
13
14
15
16
17
18
19
# File 'app/presenters/decidim/enhanced_textwork/paragraph_presenter.rb', line 12

def author
  @author ||= if official?
                Decidim::EnhancedTextwork::OfficialAuthorPresenter.new
              else
                coauthorship = coauthorships.includes(:author, :user_group).first
                coauthorship.user_group&.presenter || coauthorship.author.presenter
              end
end

#body(links: false, extras: true, strip_tags: false, all_locales: false) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/presenters/decidim/enhanced_textwork/paragraph_presenter.rb', line 62

def body(links: false, extras: true, strip_tags: false, all_locales: false)
  return unless paragraph

  handle_locales(paragraph.body, all_locales) do |content|
    content = strip_tags(sanitize_text(content)) if strip_tags

    renderer = Decidim::ContentRenderers::HashtagRenderer.new(content)
    content = renderer.render(links: links, extras: extras).html_safe

    content = Decidim::ContentRenderers::LinkRenderer.new(content).render if links
    content
  end
end

#display_mentionObject



29
30
31
# File 'app/presenters/decidim/enhanced_textwork/paragraph_presenter.rb', line 29

def display_mention
  link_to title, paragraph_path
end

#id_and_title(links: false, extras: true, html_escape: false) ⇒ Object



58
59
60
# File 'app/presenters/decidim/enhanced_textwork/paragraph_presenter.rb', line 58

def id_and_title(links: false, extras: true, html_escape: false)
  "##{paragraph.id} - #{title(links: links, extras: extras, html_escape: html_escape)}"
end

#paragraphObject



21
22
23
# File 'app/presenters/decidim/enhanced_textwork/paragraph_presenter.rb', line 21

def paragraph
  __getobj__
end

#paragraph_pathObject



25
26
27
# File 'app/presenters/decidim/enhanced_textwork/paragraph_presenter.rb', line 25

def paragraph_path
  Decidim::ResourceLocatorPresenter.new(paragraph).path
end

#resource_manifestObject



102
103
104
# File 'app/presenters/decidim/enhanced_textwork/paragraph_presenter.rb', line 102

def resource_manifest
  paragraph.class.resource_manifest
end

#title(links: false, extras: true, html_escape: false, all_locales: false) ⇒ Object

Render the paragraph title

links - should render hashtags as links? extras - should include extra hashtags?

Returns a String.



39
40
41
42
43
# File 'app/presenters/decidim/enhanced_textwork/paragraph_presenter.rb', line 39

def title(links: false, extras: true, html_escape: false, all_locales: false)
  return unless paragraph

  super paragraph.title, links, html_escape, all_locales, extras: extras
end

#title_if_enabledObject

Render the paragraph title if it is not numeric and if showing it is enabled

links - should render hashtags as links? extras - should include extra hashtags?

Returns a String.



51
52
53
54
55
56
# File 'app/presenters/decidim/enhanced_textwork/paragraph_presenter.rb', line 51

def title_if_enabled
  return unless paragraph
  return "" if paragraph.component.settings.hide_participatory_text_titles_enabled? && translated_attribute(paragraph.title) !~ /\D/

  translated_attribute(paragraph.title)
end

#versionsObject

Returns the paragraph versions, hiding not published answers

Returns an Array.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/presenters/decidim/enhanced_textwork/paragraph_presenter.rb', line 79

def versions
  version_state_published = false
  pending_state_change = nil

  paragraph.versions.map do |version|
    state_published_change = version.changeset["state_published_at"]
    version_state_published = state_published_change.last.present? if state_published_change

    if version_state_published
      version.changeset["state"] = pending_state_change if pending_state_change
      pending_state_change = nil
    elsif version.changeset["state"]
      pending_state_change = version.changeset.delete("state")
    end

    next if version.event == "update" && Decidim::EnhancedTextwork::DiffRenderer.new(version).diff.empty?

    version
  end.compact
end