Skip to content

Commit

Permalink
backport: Reorder scopes in meetings (#639)
Browse files Browse the repository at this point in the history
  • Loading branch information
AyakorK authored Nov 27, 2024
1 parent e2fc405 commit cfe65a9
Show file tree
Hide file tree
Showing 5 changed files with 99 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 @@ -61,6 +61,7 @@ class Application < Rails::Application
# Services
require "extends/services/decidim/iframe_disabler_extends"
# Helpers
require "extends/helpers/decidim/meetings/directory/application_helper_extends"
require "extends/helpers/decidim/icon_helper_extends"
require "extends/helpers/decidim/check_boxes_tree_helper_extends"
# Forms
Expand Down
3 changes: 3 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ en:
see_all_initiatives: See all initiatives
unavailable_scope: Unavailable scope
meetings:
application_helper:
filter_scope_values:
all: All
directory:
meetings:
index:
Expand Down
3 changes: 3 additions & 0 deletions config/locales/fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ fr:
see_all_initiatives: Voir toutes les pétitions
unavailable_scope: Portée indisponible
meetings:
application_helper:
filter_scope_values:
all: Tous
directory:
meetings:
index:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# frozen_string_literal: true

require "active_support/concern"
module ApplicationHelperExtends
extend ActiveSupport::Concern
include Decidim::CheckBoxesTreeHelper

included do
def directory_filter_scopes_values
main_scopes = current_organization.scopes.top_level
scopes_values = main_scopes.includes(:scope_type, :children).sort_by(&:weight).flat_map do |scope|
TreeNode.new(
TreePoint.new(scope.id.to_s, translated_attribute(scope.name, current_organization)),
scope_children_to_tree(scope)
)
end

scopes_values.prepend(TreePoint.new("global", t("decidim.scopes.global")))

TreeNode.new(
TreePoint.new("", t("decidim.meetings.application_helper.filter_scope_values.all")),
scopes_values
)
end

def scope_children_to_tree(scope)
return unless scope.children.any?

scope.children.includes(:scope_type, :children).sort_by(&:weight).flat_map do |child|
TreeNode.new(
TreePoint.new(child.id.to_s, translated_attribute(child.name, current_organization)),
scope_children_to_tree(child)
)
end
end
end
end

Decidim::Meetings::Directory::ApplicationHelper.include(ApplicationHelperExtends)
53 changes: 53 additions & 0 deletions spec/helpers/decidim/meetings/directory/application_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# frozen_string_literal: true

require "spec_helper"

module Decidim
module Meetings
module Directory
describe ApplicationHelper do
let(:helper) do
Class.new(ActionView::Base) do
include ApplicationHelper
include CheckBoxesTreeHelper
include TranslatableAttributes
end.new(ActionView::LookupContext.new(ActionController::Base.view_paths), {}, [])
end
let!(:organization) { create(:organization) }
let!(:parent_scope) { create(:scope, organization: organization) }
let!(:scope_one) { create(:scope, organization: organization, parent: parent_scope, weight: 1) }
let!(:scope_two) { create(:scope, organization: organization, parent: parent_scope, weight: 2) }
let!(:scope_three) { create(:scope, organization: organization, parent: parent_scope, weight: 3) }

before do
allow(helper).to receive(:current_organization).and_return(organization)
end

describe "#directory_filter_scopes_values" do
let(:root) { helper.directory_filter_scopes_values }
let(:leaf) { root.leaf }
let(:nodes) { root.node }

context "when the organization has a scope with children" do
it "returns all the children ordered by weight" do
expect(root).to be_a(Decidim::CheckBoxesTreeHelper::TreeNode)
expect(nodes.last.node.count).to eq(3)
expect(nodes.last.node.first.leaf.label).to eq(scope_one.name["en"])
expect(nodes.last.node.last.leaf.label).to eq(scope_three.name["en"])
end

context "and the weight of scope's children changes" do
it "returns the children ordered by the new weight" do
scope_one.update(weight: 4)
expect(root).to be_a(Decidim::CheckBoxesTreeHelper::TreeNode)
expect(nodes.last.node.count).to eq(3)
expect(nodes.last.node.first.leaf.label).to eq(scope_two.name["en"])
expect(nodes.last.node.last.leaf.label).to eq(scope_one.name["en"])
end
end
end
end
end
end
end
end

0 comments on commit cfe65a9

Please sign in to comment.