Class: Decidim::Budgets::Order

Inherits:
ApplicationRecord show all
Includes:
DataPortability, NewsletterParticipant
Defined in:
decidim-budgets/app/models/decidim/budgets/order.rb

Overview

The data store for a Order in the Decidim::Budgets component. It is unique for each user and component and contains a collection of projects

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.export_serializerObject


173
174
175
# File 'decidim-budgets/app/models/decidim/budgets/order.rb', line 173

def self.export_serializer
  Decidim::Budgets::DataPortabilityBudgetsOrderSerializer
end

.newsletter_participant_ids(component) ⇒ Object


177
178
179
180
181
182
183
184
185
186
# File 'decidim-budgets/app/models/decidim/budgets/order.rb', line 177

def self.newsletter_participant_ids(component)
  Decidim::Budgets::Order.finished
                         .joins(budget: [:component])
                         .where(budget: {
                                  decidim_components: { id: component.id }
                                })
                         .group(:decidim_user_id)
                         .pluck(:decidim_user_id)
                         .flatten.compact
end

.user_collection(user) ⇒ Object


169
170
171
# File 'decidim-budgets/app/models/decidim/budgets/order.rb', line 169

def self.user_collection(user)
  where(decidim_user_id: user.id)
end

Instance Method Details

#allocation_for(project) ⇒ Object

Public: Returns the numeric amount the given project should allocate from the total available allocation when it is added to the order. The allocation is normally the project's budget but for project selection voting, the allocation is one.


67
68
69
70
71
# File 'decidim-budgets/app/models/decidim/budgets/order.rb', line 67

def allocation_for(project)
  return 1 if projects_rule?

  project.budget_amount
end

#available_allocationObject

Public: Returns the available budget allocation the user is able to allocate to this order or the maximum amount of projects to be selected in case the project selection voting is enabled.


57
58
59
60
61
# File 'decidim-budgets/app/models/decidim/budgets/order.rb', line 57

def available_allocation
  return maximum_projects if projects_rule?

  maximum_budget
end

#budget_percentObject

Public: Returns the order budget percent from the settings total budget or the progress for selected projects if the selected project rule is enabled


110
111
112
113
114
# File 'decidim-budgets/app/models/decidim/budgets/order.rb', line 110

def budget_percent
  return (total_projects.to_f / maximum_projects) * 100 if projects_rule?

  (total_budget.to_f / budget.total_budget) * 100
end

#can_checkout?Boolean

Public: Check if the order total budget is enough to checkout

Returns:

  • (Boolean)

97
98
99
100
101
102
103
104
105
# File 'decidim-budgets/app/models/decidim/budgets/order.rb', line 97

def can_checkout?
  if projects_rule?
    total_projects >= minimum_projects && total_projects <= maximum_projects
  elsif minimum_projects_rule?
    total_projects >= minimum_projects
  else
    total_budget.to_f >= minimum_budget
  end
end

#checked_out?Boolean

Public: Returns true if the order has been checked out

Returns:

  • (Boolean)

92
93
94
# File 'decidim-budgets/app/models/decidim/budgets/order.rb', line 92

def checked_out?
  checked_out_at.present?
end

#maximum_budgetObject

Public: Returns the required maximum budget to checkout


125
126
127
128
129
# File 'decidim-budgets/app/models/decidim/budgets/order.rb', line 125

def maximum_budget
  return 0 unless budget

  budget.total_budget.to_f
end

#maximum_projectsObject

Public: Returns the required maximum projects to checkout


159
160
161
162
163
164
165
166
167
# File 'decidim-budgets/app/models/decidim/budgets/order.rb', line 159

def maximum_projects
  return nil unless budget

  if projects_rule?
    budget.settings.vote_selected_projects_maximum
  else
    0
  end
end

#minimum_budgetObject

Public: Returns the required minimum budget to checkout


117
118
119
120
121
122
# File 'decidim-budgets/app/models/decidim/budgets/order.rb', line 117

def minimum_budget
  return 0 unless budget
  return 0 if minimum_projects_rule? || projects_rule?

  budget.total_budget.to_f * (budget.settings.vote_threshold_percent.to_f / 100)
end

#minimum_projectsObject

Public: Returns the required minimum projects to checkout


146
147
148
149
150
151
152
153
154
155
156
# File 'decidim-budgets/app/models/decidim/budgets/order.rb', line 146

def minimum_projects
  return 0 unless budget

  if minimum_projects_rule?
    budget.settings.vote_minimum_budget_projects_number
  elsif projects_rule?
    budget.settings.vote_selected_projects_minimum
  else
    0
  end
end

#minimum_projects_rule?Boolean

Public: Returns if it is required a minimum projects limit to checkout

Returns:

  • (Boolean)

132
133
134
135
136
# File 'decidim-budgets/app/models/decidim/budgets/order.rb', line 132

def minimum_projects_rule?
  return unless budget

  budget.settings.vote_rule_minimum_budget_projects_enabled
end

#projects_rule?Boolean

Public: Returns true if the project voting rule is enabled

Returns:

  • (Boolean)

139
140
141
142
143
# File 'decidim-budgets/app/models/decidim/budgets/order.rb', line 139

def projects_rule?
  return unless budget

  budget.settings.vote_rule_selected_projects_enabled
end

#totalObject

Public: For budget voting returns the total budget and for project selection voting, returns the amount of selected projects.


85
86
87
88
89
# File 'decidim-budgets/app/models/decidim/budgets/order.rb', line 85

def total
  return total_projects if projects_rule?

  total_budget
end

#total_budgetObject

Public: Returns the sum of project budgets


74
75
76
# File 'decidim-budgets/app/models/decidim/budgets/order.rb', line 74

def total_budget
  projects.to_a.sum(&:budget_amount)
end

#total_projectsObject

Public: Returns the count of projects


79
80
81
# File 'decidim-budgets/app/models/decidim/budgets/order.rb', line 79

def total_projects
  projects.count
end