Class: Decidim::Lausanne::Budgets::Order
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Decidim::Lausanne::Budgets::Order
- 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
- .export_serializer ⇒ Object
- .newsletter_participant_ids(component) ⇒ Object
- .user_collection(user) ⇒ Object
Instance Method Summary collapse
-
#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.
- #anonymous? ⇒ Boolean
-
#available_allocation ⇒ Object
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.
-
#budget_percent ⇒ Object
Public: Returns the order budget percent from the settings total budget or the progress for selected projects if the selected project rule is enabled.
-
#can_checkout? ⇒ Boolean
Public: Check if the order total budget is enough to checkout.
-
#checked_out? ⇒ Boolean
Public: Returns true if the order has been checked out.
-
#maximum_budget ⇒ Object
Public: Returns the required maximum budget to checkout.
-
#maximum_projects ⇒ Object
Public: Returns the required maximum projects to checkout.
-
#minimum_budget ⇒ Object
Public: Returns the required minimum budget to checkout.
-
#minimum_projects ⇒ Object
Public: Returns the required minimum projects to checkout.
-
#minimum_projects_rule? ⇒ Boolean
Public: Returns if it is required a minimum projects limit to checkout.
-
#projects_rule? ⇒ Boolean
Public: Returns true if the project voting rule is enabled.
-
#total ⇒ Object
Public: For budget voting returns the total budget and for project selection voting, returns the amount of selected projects.
-
#total_budget ⇒ Object
Public: Returns the sum of project budgets.
-
#total_projects ⇒ Object
Public: Returns the count of projects.
Class Method Details
.export_serializer ⇒ Object
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.(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
64 65 66 |
# File 'app/models/decidim/lausanne/budgets/order.rb', line 64 def anonymous? !user end |
#available_allocation ⇒ Object
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_percent ⇒ Object
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
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
96 97 98 |
# File 'app/models/decidim/lausanne/budgets/order.rb', line 96 def checked_out? checked_out_at.present? end |
#maximum_budget ⇒ Object
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_projects ⇒ Object
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_budget ⇒ Object
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_projects ⇒ Object
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
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
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 |
#total ⇒ Object
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_budget ⇒ Object
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_projects ⇒ Object
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 |