Sha256: 7110eecca53b4d1314b50853257cd92ca7efcda04aae3cf639f3869d92f8168b

Contents?: true

Size: 1.33 KB

Versions: 13

Compression:

Stored size: 1.33 KB

Contents

= Queries

The Query class is where you can store any complex queries your application may require.

Query classes are located in the `app/queries/decidim/<my_module>` directory, and named: `<my_resource>_query.rb`.

A typical query class looks like this:

```ruby
#frozen_string_literal: true

# app/queries/decidim/my_module/my_resource_query.rb
module Decidim
  module MyModule
    class MyResourceQuery < Decidim::Query
      def self.for(components, extra_params)
        new(components, extra_params).query
      end

      def initialize(components, options)
        @components = components
        @start_at = options.delete(:start_at)
        @end_at = options.delete(:end_at)
      end

      def query
        resources = Decidim::MyModule::MyResource.where(component: @components)
        resources = resources.where("created_at >= ?", @start_at) if @start_at.present?
        resources = resources.where("created_at <= ?", @end_at) if @end_at.present?
        # and you can add any other query you need
        resources
      end
    end
  end
end
```

Then you can use the query in your code:

```ruby
 @resources = Decidim::MyModule::MyQuery.for(components, start_at: 1.month.ago, end_at: Time.current)
```

== More information

- `Decidim::Query` is an internalization of https://github.com/andypike/rectify[Rectify] gem created by Andy Pike

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
decidim-0.29.1 docs/modules/develop/pages/classes/queries.adoc
decidim-0.28.4 docs/modules/develop/pages/classes/queries.adoc
decidim-0.29.0 docs/modules/develop/pages/classes/queries.adoc
decidim-0.28.3 docs/modules/develop/pages/classes/queries.adoc
decidim-0.29.0.rc4 docs/modules/develop/pages/classes/queries.adoc
decidim-0.29.0.rc3 docs/modules/develop/pages/classes/queries.adoc
decidim-0.29.0.rc2 docs/modules/develop/pages/classes/queries.adoc
decidim-0.29.0.rc1 docs/modules/develop/pages/classes/queries.adoc
decidim-0.28.2 docs/modules/develop/pages/classes/queries.adoc
decidim-0.28.1 docs/modules/develop/pages/classes/queries.adoc
decidim-0.28.0 docs/modules/develop/pages/classes/queries.adoc
decidim-0.28.0.rc5 docs/modules/develop/pages/classes/queries.adoc
decidim-0.28.0.rc4 docs/modules/develop/pages/classes/queries.adoc