Module: Decidim::Proposals::ProposalVotesHelper

Included in:
ApplicationHelper, ProposalVotesController
Defined in:
app/helpers/decidim/proposals/proposal_votes_helper.rb

Overview

Simple helpers to handle markup variations for proposal votes partials

Instance Method Summary collapse

Instance Method Details

#can_accumulate_votes_beyond_threshold?Boolean

Public: Checks if can accumulate more than maximum is enabled

Returns true if enabled, false otherwise.

Returns:

  • (Boolean)


42
43
44
# File 'app/helpers/decidim/proposals/proposal_votes_helper.rb', line 42

def can_accumulate_votes_beyond_threshold?
  component_settings.can_accumulate_votes_beyond_threshold
end

#current_user_can_vote?Boolean

Public: Checks if the current user is allowed to vote in this step.

Returns true if the current user can vote, false otherwise.

Returns:

  • (Boolean)


63
64
65
# File 'app/helpers/decidim/proposals/proposal_votes_helper.rb', line 63

def current_user_can_vote?
  current_user && votes_enabled? && vote_limit_enabled? && !votes_blocked?
end

#remaining_votes_count_for(user) ⇒ Object

Return the remaining votes for a user if the current component has a vote limit

user - A User object

Returns a number with the remaining votes for that user



72
73
74
75
76
77
78
# File 'app/helpers/decidim/proposals/proposal_votes_helper.rb', line 72

def remaining_votes_count_for(user)
  return 0 unless vote_limit_enabled?

  proposals = Proposal.where(component: current_component)
  votes_count = ProposalVote.where(author: user, proposal: proposals).size
  component_settings.vote_limit - votes_count
end

#threshold_per_proposalObject

Public: Fetches the maximum amount of votes per proposal.

Returns an Integer with the maximum amount of votes, nil otherwise.



33
34
35
36
37
# File 'app/helpers/decidim/proposals/proposal_votes_helper.rb', line 33

def threshold_per_proposal
  return nil unless component_settings.threshold_per_proposal&.positive?

  component_settings.threshold_per_proposal
end

#threshold_per_proposal_enabled?Boolean

Public: Checks if threshold per proposal are set.

Returns true if set, false otherwise.

Returns:

  • (Boolean)


26
27
28
# File 'app/helpers/decidim/proposals/proposal_votes_helper.rb', line 26

def threshold_per_proposal_enabled?
  threshold_per_proposal.present?
end

#vote_limitObject

Public: Gets the vote limit for each user, if set.

Returns an Integer if set, nil otherwise.



10
11
12
13
14
# File 'app/helpers/decidim/proposals/proposal_votes_helper.rb', line 10

def vote_limit
  return nil if component_settings.vote_limit&.zero?

  component_settings.vote_limit
end

#vote_limit_enabled?Boolean

Check if the vote limit is enabled for the current component

Returns true if the vote limit is enabled

Returns:

  • (Boolean)


19
20
21
# File 'app/helpers/decidim/proposals/proposal_votes_helper.rb', line 19

def vote_limit_enabled?
  vote_limit.present?
end

#votes_blocked?Boolean

Public: Checks if voting is blocked in this step.

Returns true if blocked, false otherwise.

Returns:

  • (Boolean)


56
57
58
# File 'app/helpers/decidim/proposals/proposal_votes_helper.rb', line 56

def votes_blocked?
  current_settings.votes_blocked
end

#votes_enabled?Boolean

Public: Checks if voting is enabled in this step.

Returns true if enabled, false otherwise.

Returns:

  • (Boolean)


49
50
51
# File 'app/helpers/decidim/proposals/proposal_votes_helper.rb', line 49

def votes_enabled?
  current_settings.votes_enabled
end