Module: Decidim::Proposals::Admin::ProposalRankingsHelper

Defined in:
app/helpers/decidim/proposals/admin/proposal_rankings_helper.rb

Overview

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

Instance Method Summary collapse

Instance Method Details

#endorsements_ranking_for(proposal) ⇒ Object

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



30
31
32
# File 'app/helpers/decidim/proposals/admin/proposal_rankings_helper.rb', line 30

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

#i18n_endorsements_ranking_for(proposal) ⇒ Object



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

def i18n_endorsements_ranking_for(proposal)
  rankings = endorsements_ranking_for(proposal)

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

#i18n_votes_ranking_for(proposal) ⇒ Object



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

def i18n_votes_ranking_for(proposal)
  rankings = votes_ranking_for(proposal)

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

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

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

Returns a Hash with two keys:

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

Examples:

ranking_for(proposal, proposal_votes_count: :desc)
ranking_for(proposal, endorsements_count: :desc)


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

def ranking_for(proposal, order = {})
  siblings = Decidim::Proposals::Proposal.where(component: proposal.component)
  ranked = siblings.order([order, { id: :asc }])
  ranked_ids = ranked.pluck(:id)

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

#votes_ranking_for(proposal) ⇒ Object

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



35
36
37
# File 'app/helpers/decidim/proposals/admin/proposal_rankings_helper.rb', line 35

def votes_ranking_for(proposal)
  ranking_for(proposal, proposal_votes_count: :desc)
end