Skip to content

Commit

Permalink
make custom budgets optional
Browse files Browse the repository at this point in the history
  • Loading branch information
microstudi committed Sep 19, 2024
1 parent 339bfe0 commit c289854
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 14 deletions.
6 changes: 5 additions & 1 deletion app/cells/decidim/budgets/project_vote_button/show.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
disabled: vote_button_disabled?,
title: vote_button_label do %>
<% if resource_added? %>
<%= current_order.score_for(model) %>
<% if model.budget.settings.disable_custom_budgets %>
<%= icon("x", class: "icon--small", aria_label: vote_button_label, role: "img") %>
<% else %>
<%= current_order.score_for(model) %>
<% end %>
<% else %>
<%= icon("check", class: "icon--small", aria_label: vote_button_label, role: "img") %>
<% end %>
Expand Down
8 changes: 5 additions & 3 deletions app/views/decidim/budgets/projects/_budget_confirm.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
<ul class="card__content">
<% current_order.sorted_projects.each do |project| %>
<li class="budget-summary__selected-item">
<span class="badge success budget-summary_project-score">
<%= current_order.score_for(project) %>
</span>
<% unless budget.component.settings.disable_custom_budgets %>
<span class="badge success budget-summary_project-score">
<%= current_order.score_for(project) %>
</span>
<% end %>
<%= link_to translated_attribute(project.title), resource_locator([budget, project]).path %>
<strong class="budget-summary__selected-number">
<%= budget_to_currency project.budget_amount %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
<ul class="budget-summary__selected-list">
<% current_order.sorted_projects.each do |project| %>
<li class="budget-summary__selected-item">
<span class="badge success budget-summary_project-score">
<%= current_order.score_for(project) %>
</span>
<% unless budget.component.settings.disable_custom_budgets %>
<span class="badge success budget-summary_project-score">
<%= current_order.score_for(project) %>
</span>
<% end %>
<%= link_to translated_attribute(project.title), resource_locator([budget, project]).path %>
<strong class="budget-summary__selected-number">
Expand Down
34 changes: 27 additions & 7 deletions config/initializers/budgets_overrides.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,37 @@
# frozen_string_literal: true

Decidim.component_registry.find(:budgets).tap do |component|
component.settings(:global) do |settings|
settings.attribute :disable_custom_budgets, type: :boolean, default: false
end
end

Rails.application.config.to_prepare do
Decidim::Budgets::Project.class_eval do
alias_method :original_confirmed_orders_count, :confirmed_orders_count

def confirmed_votes_count
query = <<-SQL.squish
SELECT SUM(score) FROM (
SELECT dbli.*, #{budget.projects.count} - (RANK() OVER (
PARTITION BY dbli.decidim_order_id
ORDER BY dbli.id ASC
SELECT SUM(score) FROM (
SELECT dbli.*, #{budget.projects.count} - (RANK() OVER (
PARTITION BY dbli.decidim_order_id
ORDER BY dbli.id ASC
) - 1) score
FROM decidim_budgets_orders dbo
JOIN decidim_budgets_line_items dbli ON dbo.id = dbli.decidim_order_id AND dbo.checked_out_at IS NOT NULL
JOIN decidim_budgets_projects dbp ON dbp.id = dbli.decidim_project_id
) ranks
WHERE decidim_project_id = #{id}
) ranks
WHERE decidim_project_id = #{id}
SQL
@confirmed_votes_count ||= ActiveRecord::Base.connection.execute(Arel.sql(query))[0]["sum"].to_i
end

def confirmed_orders_count
confirmed_votes_count
if budget.component.settings.disable_custom_budgets
original_confirmed_orders_count
else
confirmed_votes_count
end
end
end

Expand Down Expand Up @@ -56,6 +68,8 @@ def project_count
Decidim::Budgets::ProjectVotesCountCell.class_eval do
delegate :current_order, to: :controller

alias_method :original_content, :content

def votes_count
@votes_count ||= if current_order.checked_out?
model.confirmed_orders_count
Expand All @@ -65,6 +79,8 @@ def votes_count
end

def content
return original_content if model.budget.component.settings.disable_custom_budgets

if options[:layout] == :one_line
safe_join([votes_count, " ", label(t("decidim.budgets.projects.project.votes", count: votes_count))])
else
Expand All @@ -78,9 +94,13 @@ def number
end

Decidim::Budgets::ProjectVotedHintCell.class_eval do
alias_method :original_hint, :hint

private

def hint
return original_hint if model.budget.component.settings.disable_custom_budgets

contents = []
contents << icon("check", role: "img", "aria-hidden": true)
contents << " "
Expand Down
5 changes: 5 additions & 0 deletions config/locales/es.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ es:
month: Mes
year: Año
decidim:
components:
budgets:
settings:
global:
disable_custom_budgets: Deshabilitar presupuestos personalizados
authorization_handlers:
census_authorization_handler:
connection_error: No ha sido posible realizar la verificación
Expand Down

0 comments on commit c289854

Please sign in to comment.