Skip to content

Commit

Permalink
Add spec
Browse files Browse the repository at this point in the history
  • Loading branch information
alecslupu committed Mar 4, 2024
1 parent 67ab805 commit fb3a10a
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 254 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ jobs:
with:
ruby-version: 3.0.2
bundler-cache: true

- uses: nanasess/setup-chromedriver@v2
with:
chromedriver-version: 119.0.6045.105
Expand Down
21 changes: 15 additions & 6 deletions app/cells/decidim/budget_category_voting/rule_cell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def show
protected

delegate :current_participatory_space, :current_order, to: :controller
delegate :minimum_projects_rule?, :projects, to: :current_order
delegate :minimum_projects_rule?, :projects_rule?, :projects, :projects_count_for_rule, to: :current_order

def available_styles
return "" unless category.respond_to?(:text_color)
Expand All @@ -23,13 +23,14 @@ def title
end

def remaining_votes
return minimum_remaining_votes if current_order.minimum_projects_rule?
return minimum_projects_number if minimum_projects_rule?
return minimum_selected_number if projects_rule?

raise "Unknown order type:"
end

def label
return I18n.t("remaining_votes", scope: "decidim.budget_category_voting.rule") if minimum_projects_rule?
return I18n.t("remaining_votes", scope: "decidim.budget_category_voting.rule") if minimum_projects_rule? || projects_rule?

raise "Unknown order type:"
end
Expand All @@ -38,10 +39,18 @@ def category
@category ||= current_participatory_space.categories.find(model.fetch("decidim_category_id"))
end

def minimum_remaining_votes
return 0 unless minimum_projects_rule?
def minimum_projects_number
@minimum_projects_number ||= begin
count = model.fetch("vote_minimum_budget_projects_number", 0).to_i - projects_count_for_rule(model)
count >= 0 ? count : 0
end
end

model.fetch("vote_minimum_budget_projects_number", 0).to_i - projects.with_category(category).count
def minimum_selected_number
@minimum_selected_number ||= begin
count = model.fetch("vote_selected_projects_minimum", 0).to_i - projects_count_for_rule(model)
count >= 0 ? count : 0
end
end
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/decidim/budget_category_voting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module BudgetCategoryVoting
include ActiveSupport::Configurable

module Overrides
autoload :Order, "decidim/budget_category_voting/overrides/order"
module Admin
autoload :BudgetsController, "decidim/budget_category_voting/overrides/admin/budgets_controller"
autoload :BudgetForm, "decidim/budget_category_voting/overrides/admin/budget_form"
Expand Down
2 changes: 2 additions & 0 deletions lib/decidim/budget_category_voting/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class Engine < ::Rails::Engine
initializer "budget_category_voting.overrides", after: "decidim.action_controller" do
config.to_prepare do
ActiveSupport.on_load :action_controller do
Decidim::Budgets::Order.prepend Decidim::BudgetCategoryVoting::Overrides::Order

Decidim::Budgets::Admin::BudgetsController.prepend Decidim::BudgetCategoryVoting::Overrides::Admin::BudgetsController
Decidim::Budgets::Admin::BudgetForm.prepend Decidim::BudgetCategoryVoting::Overrides::Admin::BudgetForm
Decidim::Budgets::Admin::CreateBudget.prepend Decidim::BudgetCategoryVoting::Overrides::Admin::CreateBudget
Expand Down
59 changes: 59 additions & 0 deletions lib/decidim/budget_category_voting/overrides/order.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# frozen_string_literal: true

module Decidim
module BudgetCategoryVoting
module Overrides
module Order
def self.prepended(base)
base.class_eval do
def can_checkout?
if projects_rule?
projects_rules_condition_valid?
elsif minimum_projects_rule?
minimum_projects_condition_valid?
else
minimum_budget_condition_valid?
end
end

def projects_in_category_satisfied?(type, comparator)
return false unless budget

budget.category_budget_rules.all? do |category_budget_rule|
projects_count_for_rule(category_budget_rule).send(comparator, category_budget_rule.fetch(type, 0).to_i)
end
end

def projects_count_for_rule(category_budget_rule)
projects.with_category(category_for(category_budget_rule)).count
end

def category_for(category_budget_rule)
budget.participatory_space.categories.find(category_budget_rule.fetch("decidim_category_id", 0))
end

def minimum_budget_condition_valid?
total_budget.to_f >= minimum_budget
end

def minimum_projects_condition_valid?
[
projects_in_category_satisfied?("vote_minimum_budget_projects_number", :>=),
total_projects >= minimum_projects
].all?
end

def projects_rules_condition_valid?
[
projects_in_category_satisfied?("vote_selected_projects_minimum", :>=),
projects_in_category_satisfied?("vote_selected_projects_maximum", :<=),
total_projects >= minimum_projects,
total_projects <= maximum_projects
].all?
end
end
end
end
end
end
end
Loading

0 comments on commit fb3a10a

Please sign in to comment.