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.



47
48
49
# File 'lib/decidim/budgets/workflows/base.rb', line 47

def budgets_component
  @budgets_component
end

#userObject

Returns the value of attribute user.



47
48
49
# File 'lib/decidim/budgets/workflows/base.rb', line 47

def user
  @user
end

Instance Method Details

#allowedObject

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

Returns Array.



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

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.



118
119
120
# File 'lib/decidim/budgets/workflows/base.rb', line 118

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.



80
81
82
# File 'lib/decidim/budgets/workflows/base.rb', line 80

def discardable
  progress
end

#highlightedObject

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

Returns Array.



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

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)


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

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)


111
112
113
# File 'lib/decidim/budgets/workflows/base.rb', line 111

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

#progressObject

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

Returns Array.



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

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)


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

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

#singleObject

Public: Return the single budget resource of the component

Returns an ActiveRecord.



23
24
25
# File 'lib/decidim/budgets/workflows/base.rb', line 23

def single
  budgets.first if single?
end

#single?Boolean

Public: Checks if the component has only one budget resource.

Returns Boolean.

Returns:

  • (Boolean)


16
17
18
# File 'lib/decidim/budgets/workflows/base.rb', line 16

def single?
  budgets.one?
end

#status(resource) ⇒ Object

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

  • resource: the budget resource to consider

Returns Boolean.



88
89
90
# File 'lib/decidim/budgets/workflows/base.rb', line 88

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)


43
44
45
# File 'lib/decidim/budgets/workflows/base.rb', line 43

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.



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

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)


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

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