-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
backport: Reorder scopes in meetings (#639)
- Loading branch information
Showing
5 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
lib/extends/helpers/decidim/meetings/directory/application_helper_extends.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
53
spec/helpers/decidim/meetings/directory/application_helper_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |