Sha256: 25159b5c02fac2ec56961984f668e36ab54b6d4c5a6beb05ab7e738afad625b6

Contents?: true

Size: 1.1 KB

Versions: 25

Compression:

Stored size: 1.1 KB

Contents

# frozen_string_literal: true

module Decidim
  module Assemblies
    # This query filters assemblies that can be assigned as parents for an assembly.
    class ParentAssembliesForSelect < Decidim::Query
      # Syntactic sugar to initialize the class and return the queried objects.
      def self.for(organization, assembly)
        new(organization, assembly).query
      end

      # Initializes the class.
      def initialize(organization, assembly)
        @organization = organization
        @assembly = assembly
      end

      # Finds the available assemblies
      #
      # Returns an ActiveRecord::Relation.
      def query
        available_assemblies = Assembly.where(organization: @organization).where.not(id: @assembly)

        return available_assemblies if @assembly.blank?

        available_assemblies.where.not(id: descendant_ids)
      end

      private

      def descendant_ids
        recursive_children(@assembly).flatten
      end

      def recursive_children(model)
        model.children.map do |child|
          [recursive_children(child), child.id]
        end
      end
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
decidim-assemblies-0.29.1 app/queries/decidim/assemblies/parent_assemblies_for_select.rb
decidim-assemblies-0.28.4 app/queries/decidim/assemblies/parent_assemblies_for_select.rb
decidim-assemblies-0.27.9 app/queries/decidim/assemblies/parent_assemblies_for_select.rb
decidim-assemblies-0.29.0 app/queries/decidim/assemblies/parent_assemblies_for_select.rb
decidim-assemblies-0.28.3 app/queries/decidim/assemblies/parent_assemblies_for_select.rb
decidim-assemblies-0.27.8 app/queries/decidim/assemblies/parent_assemblies_for_select.rb
decidim-assemblies-0.29.0.rc4 app/queries/decidim/assemblies/parent_assemblies_for_select.rb
decidim-assemblies-0.29.0.rc3 app/queries/decidim/assemblies/parent_assemblies_for_select.rb
decidim-assemblies-0.29.0.rc2 app/queries/decidim/assemblies/parent_assemblies_for_select.rb
decidim-assemblies-0.29.0.rc1 app/queries/decidim/assemblies/parent_assemblies_for_select.rb
decidim-assemblies-0.28.2 app/queries/decidim/assemblies/parent_assemblies_for_select.rb
decidim-assemblies-0.27.7 app/queries/decidim/assemblies/parent_assemblies_for_select.rb
decidim-assemblies-0.28.1 app/queries/decidim/assemblies/parent_assemblies_for_select.rb
decidim-assemblies-0.27.6 app/queries/decidim/assemblies/parent_assemblies_for_select.rb
decidim-assemblies-0.28.0 app/queries/decidim/assemblies/parent_assemblies_for_select.rb
decidim-assemblies-0.27.5 app/queries/decidim/assemblies/parent_assemblies_for_select.rb
decidim-assemblies-0.28.0.rc5 app/queries/decidim/assemblies/parent_assemblies_for_select.rb
decidim-assemblies-0.28.0.rc4 app/queries/decidim/assemblies/parent_assemblies_for_select.rb
decidim-assemblies-0.27.4 app/queries/decidim/assemblies/parent_assemblies_for_select.rb
decidim-assemblies-0.27.3 app/queries/decidim/assemblies/parent_assemblies_for_select.rb