Class: Decidim::Budgets::Workflows::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/decidim/budgets/workflows/base.rb

Overview

This is the base Workflow class.

Direct Known Subclasses

All, One

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(budgets_component, user) ⇒ Base

Returns a new instance of Base.



8
9
10
11
# File 'lib/decidim/budgets/workflows/base.rb', line 8

def initialize(budgets_component, user)
  @budgets_component = budgets_component
  @user = user
end

Instance Attribute Details

#budgets_componentObject

Returns the value of attribute budgets_component.



33
34
35
# File 'lib/decidim/budgets/workflows/base.rb', line 33

def budgets_component
  @budgets_component
end

#userObject

Returns the value of attribute user.



33
34
35
# File 'lib/decidim/budgets/workflows/base.rb', line 33

def user
  @user
end

Instance Method Details

#allowedObject

Public: Return the list of budgets where the user is allowed to vote.

Returns Array.



45
46
47
# File 'lib/decidim/budgets/workflows/base.rb', line 45

def allowed
  @allowed ||= budgets.select { |resource| vote_allowed?(resource) }
end

#budgetsObject

Public: Return all the budgets resources that should be taken into account for the budgets component

Returns an ActiveRecord::Relation.



104
105
106
# File 'lib/decidim/budgets/workflows/base.rb', line 104

def budgets
  @budgets ||= Decidim::Budgets::Budget.where(component: budgets_component).order(weight: :asc)
end

#discardableObject

Public: Return the list of budget resources where the user could discard their order to vote in other components.

Returns Array.



66
67
68
# File 'lib/decidim/budgets/workflows/base.rb', line 66

def discardable
  progress
end

#highlightedObject

Public: Return the list of budget resources that are highlighted for the user.

Returns Array.



38
39
40
# File 'lib/decidim/budgets/workflows/base.rb', line 38

def highlighted
  @highlighted ||= budgets.select { |resource| highlighted?(resource) }
end

#highlighted?(_resource) ⇒ Boolean

Public: Decides if the given resource should be highlighted. This method must be overwritten for each Workflow class.

  • resource: the budget resource to consider

Returns Boolean.

Returns:

  • (Boolean)

Raises:

  • (StandardError)


18
19
20
# File 'lib/decidim/budgets/workflows/base.rb', line 18

def highlighted?(_resource)
  raise StandardError, "Not implemented"
end

#limit_reached?Boolean

Public: Return if the user has reached the voting limit on budgets

Returns Boolean.

Returns:

  • (Boolean)


97
98
99
# File 'lib/decidim/budgets/workflows/base.rb', line 97

def limit_reached?
  (allowed - progress).none?
end

#progressObject

Public: Return the list of budget resources where the user has orders in progress.

Returns Array.



59
60
61
# File 'lib/decidim/budgets/workflows/base.rb', line 59

def progress
  @progress ||= orders.values.map { |order_info| order_info[:order].budget if order_info[:status] == :progress }.compact
end

#progress?(resource) ⇒ Boolean

Public: Return if the user has a pending order in the given budget resource

  • resource: the budgets resource to consider

Returns Boolean.

Returns:

  • (Boolean)


90
91
92
# File 'lib/decidim/budgets/workflows/base.rb', line 90

def progress?(resource)
  orders.dig(resource.id, :status) == :progress
end

#status(resource) ⇒ Object

Public: Return the status for the given budget resource and the user

  • resource: the budget resource to consider

Returns Boolean.



74
75
76
# File 'lib/decidim/budgets/workflows/base.rb', line 74

def status(resource)
  orders.dig(resource.id, :status) || (vote_allowed?(resource) ? :allowed : :not_allowed)
end

#vote_allowed?(_resource, consider_progress: true) ⇒ Boolean

Public: Decides if the given user should be allowed to vote in the given resource. This method must be overwritten for each Workflow class.

  • resource: the budget resource to consider

  • consider_progress: should consider user orders in progress?

    Using `false` allow UI to offer users to discard votes in progress to start voting in another resource.
    

Returns Boolean.

Returns:

  • (Boolean)

Raises:

  • (StandardError)


29
30
31
# File 'lib/decidim/budgets/workflows/base.rb', line 29

def vote_allowed?(_resource, consider_progress: true) # rubocop:disable Lint/UnusedMethodArgument
  raise StandardError, "Not implemented"
end

#votedObject

Public: Return the list of budget resources where the user has voted.

Returns Array.



52
53
54
# File 'lib/decidim/budgets/workflows/base.rb', line 52

def voted
  @voted ||= orders.values.map { |order_info| order_info[:order].budget if order_info[:status] == :voted }.compact
end

#voted?(resource) ⇒ Boolean

Public: Return if the user can vote in the given budget resource

  • resource: the budgets resource to consider

Returns Boolean.

Returns:

  • (Boolean)


82
83
84
# File 'lib/decidim/budgets/workflows/base.rb', line 82

def voted?(resource)
  orders.dig(resource.id, :status) == :voted
end