Skip to content

Commit

Permalink
Fix parent scope filter (#550)
Browse files Browse the repository at this point in the history
* fix: scopes and their children are checked in the filters on proposals page

* test: update proposal controller test with extends tests

* test: update test to include 2 levels of children

* refactor: update extends after review
  • Loading branch information
Stef-Rousset authored Jun 4, 2024
1 parent 8991a55 commit 4cd181a
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
1 change: 1 addition & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class Application < Rails::Application
require "extends/forms/decidim/initiatives/admin/initiative_form_extends"
require "extends/commands/decidim/budgets/admin/import_proposals_to_budgets_extends"
require "extends/controllers/decidim/newsletters_controller_extends"
require "extends/controllers/decidim/proposals/proposals_controller_extends"

Decidim::GraphiQL::Rails.config.tap do |config|
config.initial_query = "{\n deployment {\n version\n branch\n remote\n upToDate\n currentCommit\n latestCommit\n locallyModified\n }\n}".html_safe
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

require "active_support/concern"
module ProposalsControllerExtends
extend ActiveSupport::Concern
included do
private

def default_filter_scope_params
return "all" unless current_component.scopes.any?

scope = current_component.scope
return current_component_scope_ids if scope.blank?

scope_children_ids = scope.children&.map(&:id)
ids = ["all", scope.id] + scope_children_ids
while Decidim::Scope.where(parent_id: scope_children_ids).present?
sub_ids = Decidim::Scope.where(parent_id: scope_children_ids).pluck(:id)
ids += sub_ids
scope_children_ids = sub_ids
end
ids.map(&:to_s)
end

def current_component_scope_ids
component_scopes = current_component.scopes.pluck(:id).map(&:to_s)
%w(all global) + component_scopes
end
end
end

Decidim::Proposals::ProposalsController.include(ProposalsControllerExtends)
31 changes: 31 additions & 0 deletions spec/controllers/decidim/proposals/proposals_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,37 @@ module Proposals
request.env["decidim.current_component"] = component
end

describe "default_filter_scope_params" do
let!(:component) { create(:proposal_component) }

context "when component has no scopes" do
it "returns all" do
expect(controller.instance_eval { default_filter_scope_params }).to eq("all")
end
end

context "when component has scope" do
let(:scope) { create(:scope, organization: component.organization) }

context "and no subscope" do
it "returns an array containing all and scope id" do
component.update!(settings: { scopes_enabled: true, scope_id: scope.id })
expect(controller.instance_eval { default_filter_scope_params }).to eq(["all", scope.id.to_s])
end
end

context "and subscopes" do
let!(:subscope_one) { create(:scope, organization: component.organization, parent: scope) }
let!(:subscope_two) { create(:scope, organization: component.organization, parent: subscope_one) }

it "returns an array containing all and scope id and subscopes ids" do
component.update!(settings: { scopes_enabled: true, scope_id: scope.id })
expect(controller.instance_eval { default_filter_scope_params }).to eq(["all", scope.id.to_s, subscope_one.id.to_s, subscope_two.id.to_s])
end
end
end
end

describe "GET index" do
context "when participatory texts are disabled" do
let(:component) { create(:proposal_component, :with_geocoding_enabled) }
Expand Down

0 comments on commit 4cd181a

Please sign in to comment.