Class: Decidim::Debates::DebatePresenter

Inherits:
SimpleDelegator
  • Object
show all
Includes:
ActionView::Helpers::DateHelper, ResourceHelper, SanitizeHelper, TranslatableAttributes, TranslationsHelper
Defined in:
decidim-debates/app/presenters/decidim/debates/debate_presenter.rb

Overview

Decorator for debates

Instance Method Summary collapse

Methods included from TranslatableAttributes

#default_locale?

Methods included from SanitizeHelper

#decidim_html_escape, #decidim_sanitize, #decidim_sanitize_newsletter, #decidim_url_escape, included

Methods included from ResourceHelper

#linked_classes_filter_values_for, #linked_classes_for, #linked_resources_for, #resource_locator, #resource_title

Methods included from TranslationsHelper

empty_translatable, ensure_translatable, multi_translation, translated_in_current_locale?

Instance Method Details

#authorObject


19
20
21
22
23
24
25
26
27
# File 'decidim-debates/app/presenters/decidim/debates/debate_presenter.rb', line 19

def author
  @author ||= if official?
                Decidim::Debates::OfficialAuthorPresenter.new
              elsif user_group
                Decidim::UserGroupPresenter.new(user_group)
              else
                Decidim::UserPresenter.new(super)
              end
end

#debateObject


15
16
17
# File 'decidim-debates/app/presenters/decidim/debates/debate_presenter.rb', line 15

def debate
  __getobj__
end

#description(strip_tags: false, links: false, all_locales: false) ⇒ Object


38
39
40
41
42
43
44
45
46
47
48
# File 'decidim-debates/app/presenters/decidim/debates/debate_presenter.rb', line 38

def description(strip_tags: false, links: false, all_locales: false)
  return unless debate

  handle_locales(debate.description, all_locales) do |content|
    content = strip_tags(content) if strip_tags
    renderer = Decidim::ContentRenderers::HashtagRenderer.new(content)
    content = renderer.render(links: links).html_safe
    content = Decidim::ContentRenderers::LinkRenderer.new(content).render if links
    content
  end
end

#groups_countObject


80
81
82
83
84
# File 'decidim-debates/app/presenters/decidim/debates/debate_presenter.rb', line 80

def groups_count
  comments_authors.count do |author|
    author.is_a?(Decidim::UserGroup)
  end
end

#handle_locales(content, all_locales, &block) ⇒ Object


50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'decidim-debates/app/presenters/decidim/debates/debate_presenter.rb', line 50

def handle_locales(content, all_locales, &block)
  if all_locales
    content.each_with_object({}) do |(key, value), parsed_content|
      parsed_content[key] = if key == "machine_translations"
                              handle_locales(value, all_locales, &block)
                            else
                              block.call(value)
                            end
    end
  else
    yield(translated_attribute(content))
  end
end

#last_comment_atObject


64
65
66
67
68
# File 'decidim-debates/app/presenters/decidim/debates/debate_presenter.rb', line 64

def last_comment_at
  return unless debate.last_comment_at

  time_ago_in_words(debate.last_comment_at)
end

#last_comment_byObject


70
71
72
# File 'decidim-debates/app/presenters/decidim/debates/debate_presenter.rb', line 70

def last_comment_by
  debate.last_comment_by&.presenter
end

#participants_countObject


74
75
76
77
78
# File 'decidim-debates/app/presenters/decidim/debates/debate_presenter.rb', line 74

def participants_count
  comments_authors.count do |author|
    author.is_a?(Decidim::User)
  end
end

#title(links: false, all_locales: false) ⇒ Object


29
30
31
32
33
34
35
36
# File 'decidim-debates/app/presenters/decidim/debates/debate_presenter.rb', line 29

def title(links: false, all_locales: false)
  return unless debate

  handle_locales(debate.title, all_locales) do |content|
    renderer = Decidim::ContentRenderers::HashtagRenderer.new(decidim_html_escape(content))
    renderer.render(links: links).html_safe
  end
end