Module: Decidim::EnhancedTextwork::Admin::ParagraphRankingsHelper

Defined in:
app/helpers/decidim/enhanced_textwork/admin/paragraph_rankings_helper.rb

Overview

This class contains helpers needed to get rankings for a given paragraph ordered by some given criteria.

Instance Method Summary collapse

Instance Method Details

#endorsements_ranking_for(paragraph) ⇒ Object

Public: Gets the ranking for a given paragraph, ordered by endorsements.



30
31
32
# File 'app/helpers/decidim/enhanced_textwork/admin/paragraph_rankings_helper.rb', line 30

def endorsements_ranking_for(paragraph)
  ranking_for(paragraph, endorsements_count: :desc)
end

#i18n_endorsements_ranking_for(paragraph) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'app/helpers/decidim/enhanced_textwork/admin/paragraph_rankings_helper.rb', line 39

def i18n_endorsements_ranking_for(paragraph)
  rankings = endorsements_ranking_for(paragraph)

  I18n.t(
    "ranking",
    scope: "decidim.enhanced_textwork.admin.paragraphs.show",
    ranking: rankings[:ranking],
    total: rankings[:total]
  )
end

#i18n_votes_ranking_for(paragraph) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'app/helpers/decidim/enhanced_textwork/admin/paragraph_rankings_helper.rb', line 50

def i18n_votes_ranking_for(paragraph)
  rankings = votes_ranking_for(paragraph)

  I18n.t(
    "ranking",
    scope: "decidim.enhanced_textwork.admin.paragraphs.show",
    ranking: rankings[:ranking],
    total: rankings[:total]
  )
end

#ranking_for(paragraph, order = {}) ⇒ Object

Public: Gets the ranking for a given paragraph, ordered by some given criteria. Paragraph is sorted amongst its own siblings.

Returns a Hash with two keys:

:ranking - an Integer representing the ranking for the given paragraph.
  Ranking starts with 1.
:total - an Integer representing the total number of ranked paragraphs.

Examples:

ranking_for(paragraph, paragraph_votes_count: :desc)
ranking_for(paragraph, endorsements_count: :desc)


21
22
23
24
25
26
27
# File 'app/helpers/decidim/enhanced_textwork/admin/paragraph_rankings_helper.rb', line 21

def ranking_for(paragraph, order = {})
  siblings = Decidim::EnhancedTextwork::Paragraph.where(component: paragraph.component)
  ranked = siblings.order([order, { id: :asc }])
  ranked_ids = ranked.pluck(:id)

  { ranking: ranked_ids.index(paragraph.id) + 1, total: ranked_ids.count }
end

#votes_ranking_for(paragraph) ⇒ Object

Public: Gets the ranking for a given paragraph, ordered by votes.



35
36
37
# File 'app/helpers/decidim/enhanced_textwork/admin/paragraph_rankings_helper.rb', line 35

def votes_ranking_for(paragraph)
  ranking_for(paragraph, paragraph_votes_count: :desc)
end