From 814e448aea194f55a2d35fb862eadd49c86191b0 Mon Sep 17 00:00:00 2001 From: AyakorK Date: Tue, 19 Nov 2024 12:04:34 +0100 Subject: [PATCH] fix: Add the weight sorting on select input of scopes --- .../simple_proposal/scopes_helper_override.rb | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 app/helpers/concerns/decidim/simple_proposal/scopes_helper_override.rb diff --git a/app/helpers/concerns/decidim/simple_proposal/scopes_helper_override.rb b/app/helpers/concerns/decidim/simple_proposal/scopes_helper_override.rb new file mode 100644 index 0000000000..15a273bd7c --- /dev/null +++ b/app/helpers/concerns/decidim/simple_proposal/scopes_helper_override.rb @@ -0,0 +1,48 @@ +# frozen_string_literal: true + +module Decidim + module SimpleProposal + module ScopesHelperOverride + extend ActiveSupport::Concern + + included do + def scopes_picker_field(form, name, root: false, options: { checkboxes_on_top: true, sort_by_weight: true }) + options.merge!(selected: selected_scope(form)) if selected_scope(form) + form.select(name, simple_scope_options(root: root, options: options), include_blank: t("decidim.scopes.prompt")) + end + + private + + def selected_scope(form) + form.try(:scope_id) || + form.try(:settings).try(:scope_id) || + form.try(:object).try(:scope_id) || + form.try(:object).try(:decidim_scope_id) + end + + def simple_scope_options(root: false, options: {}) + scopes_array = [] + roots = root ? root.children : ancestors + + roots.sort_by { |ancestor| ancestor.weight || 0 }.each do |ancestor| + children_after_parent(ancestor, scopes_array, "") + end + + selected = options.has_key?(:selected) ? options[:selected] : params.dig(:filter, :decidim_scope_id) + options_for_select(scopes_array, selected) + end + + def ancestors + @ancestors ||= current_organization.scopes.where(parent_id: nil) + end + + def children_after_parent(ancestor, array, prefix) + array << ["#{prefix} #{translated_attribute(ancestor.name)}", ancestor.id, ancestor.weight] + ancestor.children.sort_by { |child| child.weight || 0 }.each do |child| + children_after_parent(child, array, "#{prefix}-") + end + end + end + end + end +end