Sha256: 8e22907ef184fdce3bf7a9662ce032c8064ddb671d5066e4ea8badaa8af00d24

Contents?: true

Size: 1.05 KB

Versions: 13

Compression:

Stored size: 1.05 KB

Contents

# frozen_string_literal: true

# 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.
class BudgetsWorkflowRandom < Decidim::Budgets::Workflows::Base
  # Highlight the resource if the user did not vote and is allowed to vote on it.
  def highlighted?(resource)
    vote_allowed?(resource)
  end

  # User can vote in the resource where they have an order in progress or in the randomly selected resource.
  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

  def discardable
    []
  end

  private

  def random_resource
    @random_resource ||= budgets.reorder(id: :asc).to_a[user.id % budgets.count] if user
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
decidim-generators-0.29.1 lib/decidim/generators/app_templates/budgets_workflow_random.rb
decidim-generators-0.28.4 lib/decidim/generators/app_templates/budgets_workflow_random.rb
decidim-generators-0.29.0 lib/decidim/generators/app_templates/budgets_workflow_random.rb
decidim-generators-0.28.3 lib/decidim/generators/app_templates/budgets_workflow_random.rb
decidim-generators-0.29.0.rc4 lib/decidim/generators/app_templates/budgets_workflow_random.rb
decidim-generators-0.29.0.rc3 lib/decidim/generators/app_templates/budgets_workflow_random.rb
decidim-generators-0.29.0.rc2 lib/decidim/generators/app_templates/budgets_workflow_random.rb
decidim-generators-0.29.0.rc1 lib/decidim/generators/app_templates/budgets_workflow_random.rb
decidim-generators-0.28.2 lib/decidim/generators/app_templates/budgets_workflow_random.rb
decidim-generators-0.28.1 lib/decidim/generators/app_templates/budgets_workflow_random.rb
decidim-generators-0.28.0 lib/decidim/generators/app_templates/budgets_workflow_random.rb
decidim-generators-0.28.0.rc5 lib/decidim/generators/app_templates/budgets_workflow_random.rb
decidim-generators-0.28.0.rc4 lib/decidim/generators/app_templates/budgets_workflow_random.rb