Sha256: a9a92c27d2c8f255bea92598fc68b3ffcfa2d2910817ca375ae09861a0f0cd61

Contents?: true

Size: 1.1 KB

Versions: 34

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 < Rectify::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

34 entries across 34 versions & 1 rubygems

Version Path
decidim-assemblies-0.26.10 app/queries/decidim/assemblies/parent_assemblies_for_select.rb
decidim-assemblies-0.26.9 app/queries/decidim/assemblies/parent_assemblies_for_select.rb
decidim-assemblies-0.26.8 app/queries/decidim/assemblies/parent_assemblies_for_select.rb
decidim-assemblies-0.26.7 app/queries/decidim/assemblies/parent_assemblies_for_select.rb
decidim-assemblies-0.26.5 app/queries/decidim/assemblies/parent_assemblies_for_select.rb
decidim-assemblies-0.26.4 app/queries/decidim/assemblies/parent_assemblies_for_select.rb
decidim-assemblies-0.26.3 app/queries/decidim/assemblies/parent_assemblies_for_select.rb
decidim-assemblies-0.26.2 app/queries/decidim/assemblies/parent_assemblies_for_select.rb
decidim-assemblies-0.26.1 app/queries/decidim/assemblies/parent_assemblies_for_select.rb
decidim-assemblies-0.26.0 app/queries/decidim/assemblies/parent_assemblies_for_select.rb
decidim-assemblies-0.26.0.rc2 app/queries/decidim/assemblies/parent_assemblies_for_select.rb
decidim-assemblies-0.26.0.rc1 app/queries/decidim/assemblies/parent_assemblies_for_select.rb
decidim-assemblies-0.25.2 app/queries/decidim/assemblies/parent_assemblies_for_select.rb
decidim-assemblies-0.25.1 app/queries/decidim/assemblies/parent_assemblies_for_select.rb
decidim-assemblies-0.25.0 app/queries/decidim/assemblies/parent_assemblies_for_select.rb
decidim-assemblies-0.25.0.rc4 app/queries/decidim/assemblies/parent_assemblies_for_select.rb
decidim-assemblies-0.25.0.rc3 app/queries/decidim/assemblies/parent_assemblies_for_select.rb
decidim-assemblies-0.25.0.rc2 app/queries/decidim/assemblies/parent_assemblies_for_select.rb
decidim-assemblies-0.25.0.rc1 app/queries/decidim/assemblies/parent_assemblies_for_select.rb
decidim-assemblies-0.24.3 app/queries/decidim/assemblies/parent_assemblies_for_select.rb