Class: Decidim::Core::ComponentListBase

Inherits:
Object
  • Object
show all
Includes:
NeedsApiDefaultOrder, NeedsApiFilterAndOrder
Defined in:
lib/decidim/api/functions/component_list_base.rb

Overview

A very basic resolver for the GraphQL endpoint for listing components Used in the keyword that lists some type of a specific component. For instance, “proposals” if it is a Proposal:

participatoryProcesses {

components {
... on Proposals {
     proposals(...)
  }
}

}

Needs to be extended and add arguments Usually something like:

class ProposalListHelper < Decidim::Core::ComponentListBase
  argument :order, ProposalInputSort, "Provides several methods to order the results"
  argument :filter, ProposalInputFilter, "Provides several methods to filter the results"
end

For an example check decidim-proposals/app/types/decidim/proposals/proposals_type.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from NeedsApiFilterAndOrder

#add_filter_keys, #add_order_keys

Constructor Details

#initialize(model_class:) ⇒ ComponentListBase

Returns a new instance of ComponentListBase.



33
34
35
# File 'lib/decidim/api/functions/component_list_base.rb', line 33

def initialize(model_class:)
  @model_class = model_class
end

Instance Attribute Details

#model_classObject (readonly)

Returns the value of attribute model_class.



31
32
33
# File 'lib/decidim/api/functions/component_list_base.rb', line 31

def model_class
  @model_class
end

Instance Method Details

#call(component, args, _ctx) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/decidim/api/functions/component_list_base.rb', line 37

def call(component, args, _ctx)
  @query = query_scope.where(component:)
                      .includes(:component)

  add_filter_keys(args[:filter])
  order = filter_keys_by_settings(args[:order].to_h, component)
  add_order_keys(order)
  add_default_order
  @query
end

#query_scopeObject

By default, any model uses the default scope for queries Particular implementations may restrict this by adding additional constrains



51
52
53
# File 'lib/decidim/api/functions/component_list_base.rb', line 51

def query_scope
  model_class
end