class BudgetsWorkflowRandom

This Workflow allows users to vote only in one budget, selected randomly.

Note: random selection should be deterministic for the same user and the same budgets component. As the budget resources list could change and affect the random selection, it also allows to finish orders created on other budgets.

Public Instance Methods

discardable() click to toggle source
# File lib/decidim/generators/app_templates/budgets_workflow_random.rb, line 24
def discardable
  []
end
highlighted?(resource) click to toggle source

Highlight the resource if the user didn't vote and is allowed to vote on it.

# File lib/decidim/generators/app_templates/budgets_workflow_random.rb, line 9
def highlighted?(resource)
  vote_allowed?(resource)
end
vote_allowed?(resource, consider_progress: true) click to toggle source

User can vote in the resource where they have an order in progress or in the randomly selected resource.

# File lib/decidim/generators/app_templates/budgets_workflow_random.rb, line 14
def vote_allowed?(resource, consider_progress: true)
  return false if voted.any?

  if consider_progress
    progress?(resource) || (progress.none? && resource == random_resource)
  else
    resource == random_resource
  end
end

Private Instance Methods

random_resource() click to toggle source
# File lib/decidim/generators/app_templates/budgets_workflow_random.rb, line 30
def random_resource
  @random_resource ||= budgets.reorder(id: :asc).to_a[user.id % budgets.count] if user
end