Skip to content

Commit

Permalink
Merge pull request #141 from CodeandoMexico/hotfix/proposals
Browse files Browse the repository at this point in the history
corrección de implementación
  • Loading branch information
basicavisual committed Jun 14, 2023
2 parents 4150cd4 + c36d38e commit 229c6b4
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 51 deletions.
6 changes: 3 additions & 3 deletions app/helpers/proposal_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ module ProposalHelper

def user_can_vote_district_proposal?(user, proposal)
authorization = user_authorization(user)
return false unless authorization
return false unless authorization && proposal.scope

proposal.scope.code == authorization.metadata["district_code"]
end

def user_can_vote_sector_proposal?(user, proposal)
authorization = user_authorization(user)
return false unless authorization
return false unless authorization && proposal.scope

proposal.scope.code == authorization.metadata["sector_code"]
end
Expand All @@ -39,7 +39,7 @@ def get_proposals_component_scope_create_message_key(component_settings)
end

def is_component_scope_district?(component)
Decidim::Scope.find(component.settings.scope_id).code == "DISTRITOS" # what about using component.budget??.scope to account for those that don't have .code
Decidim::Scope.find(component.settings.scope_id).code == "DISTRITOS"
end

def is_component_scope_sector?(component)
Expand Down
70 changes: 37 additions & 33 deletions lib/extensions/decidim/geographic_scope_matcher.rb
Original file line number Diff line number Diff line change
@@ -1,46 +1,50 @@
module Decidim
class GeographicScopeMatcher < Rectify::Command
# This extension helps associate user's district or sector to their participation in a component. If the component has a scope, and the scope is permissions' associated, this extension will find the corresponding user's scope, and match it to the component's needs.

GEO_SCOPES = {
"DISTRITOS" => "district_code",
"SECTORES" => "sector_code"
}

def initialize(item, current_user)
@item = item
@component = item.component
@current_user = current_user
end
module Extensions
module Decidim
class GeographicScopeMatcher < Rectify::Command
# This extension helps associate user's district or sector to their participation in a component. If the component has a scope, and the scope is permissions' associated, this extension will find the corresponding user's scope, and match it to the component's needs.

def call
return broadcast(:nil) if @component.scope.nil?
GEO_SCOPES = {
"DISTRITOS" => "district_code",
"SECTORES" => "sector_code"
}

component_scope_type = scope_type_checker
return broadcast(:nil) if component_scope_type.nil?
def initialize(item, current_user)
@item = item
@component = item.component
@current_user = current_user
end

authorizations = user_authorization_checker
return broadcast(:nil) if authorizations.nil?
def call
return broadcast(:nil) if @component.scope.nil?

matcher = component_scope_matcher(component_scope_type, authorizations)
component_scope_type = scope_type_checker
return broadcast(:nil) if component_scope_type.nil?

broadcast(:ok, matcher)
end
authorizations = user_authorization_checker
return broadcast(:nil) if authorizations.nil?

private
matcher = item_scope_matcher(component_scope_type, authorizations)

def scope_type_checker
GEO_SCOPES[@component.scope.code]
end
broadcast(:ok, matcher)
end

def user_authorization_checker
authorizations = Decidim::Authorization.where.not(granted_at: nil).where(user: @current_user, name: ["ine", "managed_user_authorization_handler"])
private

authorizations.nil? || authorizations
end
attr_reader :current_user, :item

def scope_type_checker
GEO_SCOPES[@component.scope.code]
end

def user_authorization_checker
authorizations = ::Decidim::Authorization.where.not(granted_at: nil).where(user: @current_user, name: ["ine", "managed_user_authorization_handler"])

authorizations.nil? || authorizations
end

def item_scope_matcher(scope_type, authorization)
Decidim::Scope.find_by! code: authorizations.last.metadata[scope_type]
def item_scope_matcher(scope_type, authorizations)
::Decidim::Scope.find_by! code: authorizations.last.metadata[scope_type]
end
end
end
end
6 changes: 2 additions & 4 deletions lib/extensions/decidim/proposals/create_proposal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@ def create_proposal
component: form.component
)
proposal.add_coauthor(@current_user, user_group: user_group)
GeographicScopeMatcher.call(@proposal, @current_user) do
on(:ok) do
proposal.scope = matcher
end
Decidim::GeographicScopeMatcher.call(proposal, @current_user) do
on(:ok) { |matcher| proposal.scope = matcher }
end
proposal.save!
proposal
Expand Down
16 changes: 11 additions & 5 deletions lib/extensions/decidim/proposals/publish_proposal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,21 @@ def publish_proposal
@current_user,
visibility: "public-only"
) do
@proposal.update title: title, body: body, published_at: Time.current
GeographicScopeMatcher.call(@proposal, @current_user) do
on(:ok) do
@proposal.scope = matcher
end
Decidim::GeographicScopeMatcher.call(@proposal, @current_user) do
on(:ok) { |matcher| publisher(title, body, matcher) }
on(:nil) { publisher(title, body, false) }
end
@proposal.save!
end
end

def publisher(title, body, match)
if match == false
@proposal.update title: title, body: body, published_at: Time.current
else
@proposal.update title: title, body: body, published_at: Time.current, scope: match
end
end
end
end
end
Expand Down
6 changes: 2 additions & 4 deletions lib/extensions/decidim/proposals/update_proposal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ def update_proposal
)
@proposal.coauthorships.clear
@proposal.add_coauthor(current_user, user_group: user_group)
GeographicScopeMatcher.call(@proposal, @current_user) do
on(:ok) do
@roposal.scope = matcher
end
Decidim::GeographicScopeMatcher.call(proposal, @current_user) do
on(:ok) { |matcher| @proposal.scope = matcher }
end
@proposal.save!
end
Expand Down
4 changes: 2 additions & 2 deletions lib/extensions/decidim/proposals/vote_proposal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ def call
private

def can_user_vote_in_proposal?
GeographicScopeMatcher.call(@proposal, @current_user) do
return true if @proposal.scope.nil?
Decidim::GeographicScopeMatcher.call(@proposal, @current_user) do
on(:ok) do
return true
end
on(:nil) do
return false
end
end
# @proposal.scope == current_user_scope(@proposal)
end
end
end
Expand Down

0 comments on commit 229c6b4

Please sign in to comment.