Class: Inquery::Query::Chainable

Inherits:
Inquery::Query show all
Includes:
Mixins::RelationValidation
Defined in:
lib/inquery/query/chainable.rb

Overview

Chainable query class for queries that input and output ActiveRecord relations.

Use this class when you want to build queries that can be chained together or used as ActiveRecord scopes. The query receives a relation, transforms it, and returns a new relation.

Example:

class FetchActive < Inquery::Query::Chainable
  relation class: 'User'

  def call
    relation.where(active: true)
  end
end

User.all.then { |rel| FetchActive.run(rel) }
# Or as a scope:
class User < ActiveRecord::Base
  scope :active, FetchActive
end

Constant Summary

Constants included from Mixins::RelationValidation

Mixins::RelationValidation::DEFAULT_OPTIONS, Mixins::RelationValidation::OPTIONS_SCHEMA

Instance Attribute Summary collapse

Attributes inherited from Inquery::Query

#params

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mixins::RelationValidation

#validate_relation!

Methods inherited from Inquery::Query

#call, #osparams, #process, #run, run

Constructor Details

#initialize(*args) ⇒ Chainable

Initializes a chainable query with a relation and optional parameters.

Supports multiple call signatures:

new()                    - Uses default relation from 'relation' DSL
new(relation)            - Uses provided relation
new(params)              - Uses default relation with params
new(relation, params)    - Uses provided relation and params

Parameters:

  • args (Array)

    Variable arguments for relation and/or params

Raises:



52
53
54
55
56
# File 'lib/inquery/query/chainable.rb', line 52

def initialize(*args)
  relation, params = parse_init_args(*args)
  @relation = validate_relation!(relation)
  super(params)
end

Instance Attribute Details

#relationActiveRecord::Relation (readonly)

The input ActiveRecord relation that will be transformed by this query.

Returns:

  • (ActiveRecord::Relation)


39
40
41
# File 'lib/inquery/query/chainable.rb', line 39

def relation
  @relation
end

Class Method Details

.call(*args) ⇒ ActiveRecord::Relation

Instantiates the query and executes it, allowing use as an AR scope.

This enables chainable queries to be used directly as ActiveRecord scopes:

scope :active, FetchActive

Parameters:

  • args (Array)

    Arguments passed to initialize (relation and/or params)

Returns:

  • (ActiveRecord::Relation)

    The transformed relation



32
33
34
# File 'lib/inquery/query/chainable.rb', line 32

def self.call(*args)
  return new(*args).call
end

Instance Method Details

#connectionActiveRecord::ConnectionAdapters::AbstractAdapter

Returns the database connection from the relation.

This ensures that the query uses the same connection as the input relation, which is important for connection pooling and transactions.

Returns:

  • (ActiveRecord::ConnectionAdapters::AbstractAdapter)


64
65
66
# File 'lib/inquery/query/chainable.rb', line 64

def connection
  @relation.connection
end