Skip to content

Commit

Permalink
New voting layout
Browse files Browse the repository at this point in the history
  • Loading branch information
ahukkanen committed Jan 16, 2024
1 parent eb147c5 commit 4b2ef07
Show file tree
Hide file tree
Showing 50 changed files with 986 additions and 928 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ module BudgetingPipeline
module AdminCreateBudgetExtensions
extend ActiveSupport::Concern

include Decidim::AttachmentAttributesMethods

included do
def create_budget!
attributes = {
Expand All @@ -17,7 +19,7 @@ def create_budget!
total_budget: form.total_budget,
center_latitude: form.center_latitude,
center_longitude: form.center_longitude
}
}.merge(attachment_attributes(:list_image))

@budget = Decidim.traceability.create!(
Decidim::Budgets::Budget,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ module BudgetingPipeline
module AdminUpdateBudgetExtensions
extend ActiveSupport::Concern

include Decidim::AttachmentAttributesMethods

included do
def update_budget!
attributes = {
Expand All @@ -16,7 +18,7 @@ def update_budget!
total_budget: form.total_budget,
center_latitude: form.center_latitude,
center_longitude: form.center_longitude
}
}.merge(attachment_attributes(:list_image))

Decidim.traceability.update!(
budget,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

module Decidim
module BudgetingPipeline
module Authorizable
extend ActiveSupport::Concern

# This ensures that only people eligible to vote can enter the voting
# pipeline after the authorization step. The authorization conditions
# should be used to control user's ability to vote (e.g. where they live,
# how old they are, etc.).
def user_authorized?
@user_authorized ||= user_signed_in? && action_authorized_to("vote").ok?
end

def authorization_required?
@authorization_required ||= begin
permission = current_component.permissions&.fetch("vote", nil)
handlers = permission&.fetch("authorization_handlers", nil)&.keys
if handlers && handlers.any?
providers = Decidim::BudgetingPipeline.authorization_providers
providers = providers.call(current_organization) if providers.respond_to?(:call)
(handlers & providers).any?
else
false
end
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ module LineItemsControllerExtensions
include Decidim::BudgetingPipeline::ProjectItemUtilities # Overrides the `voted_for?` method for multiple orders.

included do
helper Decidim::Budgets::VotesHelper

def create
enforce_permission_to :vote, :project, project: project, budget: budget, workflow: current_workflow

@added = true

respond_to do |format|
Decidim::Budgets::AddLineItem.call(persisted_current_order, project, current_user) do
on(:ok) do |order|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ module ProjectsControllerExtensions

include Decidim::Paginable
include Decidim::BudgetingPipeline::VoteUtilities
include Decidim::BudgetingPipeline::Authorizable
include Decidim::BudgetingPipeline::Orderable

included do
helper_method :help_sections, :geocoded_projects, :budgets, :maximum_project_budget, :statuses_available?, :vote_success?
helper_method :authorization_required?, :user_authorized?, :help_sections, :geocoded_projects, :budgets, :maximum_project_budget, :statuses_available?, :vote_success?

helper Decidim::BudgetingPipeline::AuthorizationHelper

def index; end

Expand All @@ -28,7 +31,8 @@ def default_filter_params
decidim_budgets_budget_id_eq: nil,
budget_amount_gteq: 0,
budget_amount_lteq: maximum_project_budget,
activity: "all"
favorites: nil,
selected: nil
}
end

Expand Down
53 changes: 21 additions & 32 deletions app/controllers/decidim/budgets/votes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@ class VotesController < ApplicationController
include Decidim::FormFactory
include Decidim::FilterResource
include Decidim::Paginable
include Decidim::TranslatableAttributes
include Decidim::Budgets::Orderable
include Decidim::BudgetingPipeline::Authorizable
include Decidim::BudgetingPipeline::Orderable
include Decidim::BudgetingPipeline::OrdersUtilities
include Decidim::BudgetingPipeline::VoteUtilities

helper Decidim::BudgetingPipeline::AuthorizationHelper

helper_method(
:authorization_required?,
:user_authorized?,
:help_sections,
:voting_steps,
:current_step,
Expand All @@ -29,7 +33,7 @@ class VotesController < ApplicationController

before_action :ensure_voting_open!
before_action :ensure_authorized!
before_action :ensure_not_voted!
before_action :ensure_not_voted!, except: [:finished]
before_action :ensure_orders!, only: [:projects, :preview, :create]
before_action :ensure_orders_valid!, only: [:preview, :create]
before_action :set_current_step
Expand All @@ -38,9 +42,12 @@ class VotesController < ApplicationController
skip_before_action :ensure_not_voted!, only: [:show]

def show
return redirect_to(routes_proxy.projects_path) unless user_signed_in?

define_step(authorization_required? ? :authorization : :login)
return unless user_authorized?
return ensure_not_voted! if user_voted?
return if current_workflow.progress.blank? && translated_attribute(component_settings.vote_privacy_content).present?

redirect_to routes_proxy.budgets_vote_path
end
Expand All @@ -66,8 +73,7 @@ def start
end

def projects
@projects = search.result.page(params[:page]).per(current_component.settings.vote_projects_per_page)
@projects = reorder(@projects)
@projects = reorder(search.result)
end

def create
Expand All @@ -78,11 +84,11 @@ def create
# popup which is displayed after a successful vote. This is
# important for screen reader users who need to hear the modal
# content when it is displayed.
session["decidim-budgets.voted"] = true
# session["decidim-budgets.voted"] = true
if current_settings.show_votes?
redirect_to routes_proxy.results_path
else
redirect_to routes_proxy.projects_path
redirect_to routes_proxy.finished_vote_path
end
end

Expand All @@ -93,6 +99,10 @@ def create
end
end

def finished
redirect_to(routes_proxy.projects_path) if !user_signed_in? || !user_voted?
end

private

attr_accessor :current_step, :prev_step, :next_step
Expand Down Expand Up @@ -142,28 +152,6 @@ def ensure_orders_valid!
redirect_to routes_proxy.projects_vote_path
end

# This ensures that only people eligible to vote can enter the voting
# pipeline after the authorization step. The authorization conditions
# should be used to control user's ability to vote (e.g. where they live,
# how old they are, etc.).
def user_authorized?
@user_authorized ||= user_signed_in? && action_authorized_to("vote").ok?
end

def authorization_required?
@authorization_required ||= begin
permission = current_component.permissions&.fetch("vote", nil)
handlers = permission&.fetch("authorization_handlers", nil)&.keys
if handlers && handlers.any?
providers = Decidim::BudgetingPipeline.authorization_providers
providers = providers.call(current_organization) if providers.respond_to?(:call)
(handlers & providers).any?
else
false
end
end
end

def set_current_step
define_step(action_name.to_sym)
end
Expand Down Expand Up @@ -193,9 +181,9 @@ def help_sections
# one(s) that will be automatically selected.
def choose_budgets
@choose_budgets ||= begin
sticky_ids = sticky_budgets.map(&:id)
current_workflow.allowed.reject do |budget|
sticky_ids.include?(budget.id)
suggested_ids = suggested_budgets.map(&:id)
current_workflow.budgets.reject do |budget|
suggested_ids.include?(budget.id)
end
end
end
Expand Down Expand Up @@ -290,7 +278,8 @@ def default_filter_params
decidim_budgets_budget_id_eq: nil,
budget_amount_gteq: 0,
budget_amount_lteq: maximum_project_budget,
activity: "all"
favorites: nil,
selected: nil
}
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ module AdminBudgetFormExtensions
included do
attribute :center_latitude, Float
attribute :center_longitude, Float
attribute :list_image, Decidim::Attributes::Blob

validates :list_image, passthru: {
to: Decidim::Budgets::Budget,
with: { component: ->(form) { form.current_component } }
}

def geocoded?
center_latitude.present? && center_longitude.present?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ def projects_data_for_map(geocoded_projects_data)
end
end

def identity_providers
providers = Decidim::BudgetingPipeline.identity_providers
return providers unless providers.respond_to?(:call)

providers.call(current_organization)
end

def identity_provider_name(provider)
Decidim::BudgetingPipeline.identity_provider_name.call(provider)
end

def landing_page_content
translated_attribute(current_settings.landing_page_content).presence ||
translated_attribute(component_settings.landing_page_content)
Expand Down
52 changes: 52 additions & 0 deletions app/helpers/decidim/budgeting_pipeline/authorization_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# frozen_string_literal: true

module Decidim
module BudgetingPipeline
module AuthorizationHelper
include Decidim::BudgetingPipeline::ProjectsHelperExtensions
include Decidim::BudgetingPipeline::ProjectItemUtilities
include Decidim::BudgetingPipeline::TextUtilities

def available_authorization_provider_keys
providers = Decidim::BudgetingPipeline.authorization_providers
providers = providers.call(current_organization) if providers.respond_to?(:call)
providers
end

def authorization_providers
Verifications::Adapter.from_collection(
available_authorization_provider_keys - user_authorizations.pluck(:name)
)
end

def user_authorizations(type = :all)
base = Decidim::Authorization.where(
name: available_authorization_provider_keys,
user: current_user
)
return base.where.not(granted_at: nil) if type == :granted
return base.where(granted_at: nil) if type == :pending

base
end

def authorization_provider_name(provider)
Decidim::BudgetingPipeline.authorization_provider_name.call(provider)
end

def invalid_authorization_title
translated_attribute(component_settings.vote_identify_invalid_authorization_title)
end

def invalid_authorization_content
translated_attribute(component_settings.vote_identify_invalid_authorization_content)
end

# This is for the projects view that displays the project filters that
# refers the `budgets` variable.
def budgets
selected_budgets
end
end
end
end
Loading

0 comments on commit 4b2ef07

Please sign in to comment.