Class: Decidim::Lausanne::Budgets::Order

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

Overview

The data store for a Order in the Decidim::Lausanne::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



177
178
179
# File 'app/models/decidim/lausanne/budgets/order.rb', line 177

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

.newsletter_participant_ids(component) ⇒ Object



181
182
183
184
185
186
187
188
189
190
# File 'app/models/decidim/lausanne/budgets/order.rb', line 181

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

.user_collection(user) ⇒ Object



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

def self.user_collection(user)
  where(user: user)
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.



71
72
73
74
75
# File 'app/models/decidim/lausanne/budgets/order.rb', line 71

def allocation_for(project)
  return 1 if projects_rule?

  project.budget_amount
end

#anonymous?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'app/models/decidim/lausanne/budgets/order.rb', line 64

def anonymous?
  !user
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.



59
60
61
62
63
# File 'app/models/decidim/lausanne/budgets/order.rb', line 59

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



114
115
116
117
118
# File 'app/models/decidim/lausanne/budgets/order.rb', line 114

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)


101
102
103
104
105
106
107
108
109
# File 'app/models/decidim/lausanne/budgets/order.rb', line 101

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)


96
97
98
# File 'app/models/decidim/lausanne/budgets/order.rb', line 96

def checked_out?
  checked_out_at.present?
end

#maximum_budgetObject

Public: Returns the required maximum budget to checkout



129
130
131
132
133
# File 'app/models/decidim/lausanne/budgets/order.rb', line 129

def maximum_budget
  return 0 unless budget

  budget.total_budget.to_f
end

#maximum_projectsObject

Public: Returns the required maximum projects to checkout



163
164
165
166
167
168
169
170
171
# File 'app/models/decidim/lausanne/budgets/order.rb', line 163

def maximum_projects
  return nil unless budget

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

#minimum_budgetObject

Public: Returns the required minimum budget to checkout



121
122
123
124
125
126
# File 'app/models/decidim/lausanne/budgets/order.rb', line 121

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



150
151
152
153
154
155
156
157
158
159
160
# File 'app/models/decidim/lausanne/budgets/order.rb', line 150

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)


136
137
138
139
140
# File 'app/models/decidim/lausanne/budgets/order.rb', line 136

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)


143
144
145
146
147
# File 'app/models/decidim/lausanne/budgets/order.rb', line 143

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.



89
90
91
92
93
# File 'app/models/decidim/lausanne/budgets/order.rb', line 89

def total
  return total_projects if projects_rule?

  total_budget
end

#total_budgetObject

Public: Returns the sum of project budgets



78
79
80
# File 'app/models/decidim/lausanne/budgets/order.rb', line 78

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

#total_projectsObject

Public: Returns the count of projects



83
84
85
# File 'app/models/decidim/lausanne/budgets/order.rb', line 83

def total_projects
  projects.count
end