Class: Inquery::Query::Chainable
- Inherits:
-
Inquery::Query
- Object
- Inquery::Query
- Inquery::Query::Chainable
- 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
-
#relation ⇒ ActiveRecord::Relation
readonly
The input ActiveRecord relation that will be transformed by this query.
Attributes inherited from Inquery::Query
Class Method Summary collapse
-
.call(*args) ⇒ ActiveRecord::Relation
Instantiates the query and executes it, allowing use as an AR scope.
Instance Method Summary collapse
-
#connection ⇒ ActiveRecord::ConnectionAdapters::AbstractAdapter
Returns the database connection from the relation.
-
#initialize(*args) ⇒ Chainable
constructor
Initializes a chainable query with a relation and optional parameters.
Methods included from Mixins::RelationValidation
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
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
#relation ⇒ ActiveRecord::Relation (readonly)
The input ActiveRecord relation that will be transformed by this query.
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
32 33 34 |
# File 'lib/inquery/query/chainable.rb', line 32 def self.call(*args) return new(*args).call end |
Instance Method Details
#connection ⇒ ActiveRecord::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.
64 65 66 |
# File 'lib/inquery/query/chainable.rb', line 64 def connection @relation.connection end |