Class: Quo::RelationBackedQuerySpecification
- Inherits:
-
Object
- Object
- Quo::RelationBackedQuerySpecification
- Defined in:
- lib/quo/relation_backed_query_specification.rb,
sig/generated/quo/relation_backed_query_specification.rbs
Overview
RelationBackedQuerySpecification encapsulates all the options for building a SQL query This separates the storage of query options from the actual query construction and provides a cleaner interface for RelationBackedQuery
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
@rbs! @options: Hash[Symbol, untyped].
Class Method Summary collapse
-
.blank ⇒ Quo::RelationBackedQuerySpecification
Returns a blank specification.
-
.build(options = {}) ⇒ Quo::RelationBackedQuerySpecification
Builds a new specification from a hash of options.
Instance Method Summary collapse
- #[](key) ⇒ Object
-
#apply_to(relation) ⇒ ActiveRecord::Relation
Apply all the specification options to the given ActiveRecord relation.
- #distinct(enabled = true) ⇒ Quo::RelationBackedQuerySpecification
- #eager_load(*associations) ⇒ Quo::RelationBackedQuerySpecification
- #extending(*modules) ⇒ Quo::RelationBackedQuerySpecification
- #group(*columns) ⇒ Quo::RelationBackedQuerySpecification
- #has?(key) ⇒ Boolean
- #includes(*associations) ⇒ Quo::RelationBackedQuerySpecification
- #initialize(options = {}) ⇒ Object constructor
- #joins(*tables) ⇒ Quo::RelationBackedQuerySpecification
- #left_outer_joins(*tables) ⇒ Quo::RelationBackedQuerySpecification
- #limit(value) ⇒ Quo::RelationBackedQuerySpecification
-
#merge(new_options) ⇒ Quo::RelationBackedQuerySpecification
Creates a new specification with merged options.
- #offset(value) ⇒ Quo::RelationBackedQuerySpecification
- #order(order_clause) ⇒ Quo::RelationBackedQuerySpecification
- #preload(*associations) ⇒ Quo::RelationBackedQuerySpecification
- #reorder(order_clause) ⇒ Quo::RelationBackedQuerySpecification
- #select(*fields) ⇒ Quo::RelationBackedQuerySpecification
- #unscope(*args) ⇒ Quo::RelationBackedQuerySpecification
- #where(conditions) ⇒ Quo::RelationBackedQuerySpecification
Constructor Details
#initialize(options = {}) ⇒ Object
15 16 17 |
# File 'lib/quo/relation_backed_query_specification.rb', line 15 def initialize( = {}) @options = end |
Instance Attribute Details
#options ⇒ Object (readonly)
@rbs! @options: Hash[Symbol, untyped]
12 13 14 |
# File 'lib/quo/relation_backed_query_specification.rb', line 12 def @options end |
Class Method Details
.blank ⇒ Quo::RelationBackedQuerySpecification
Returns a blank specification
169 170 171 |
# File 'lib/quo/relation_backed_query_specification.rb', line 169 def self.blank @blank ||= new end |
.build(options = {}) ⇒ Quo::RelationBackedQuerySpecification
Builds a new specification from a hash of options
163 164 165 |
# File 'lib/quo/relation_backed_query_specification.rb', line 163 def self.build( = {}) new() end |
Instance Method Details
#[](key) ⇒ Object
64 65 66 |
# File 'lib/quo/relation_backed_query_specification.rb', line 64 def [](key) [key] end |
#apply_to(relation) ⇒ ActiveRecord::Relation
Apply all the specification options to the given ActiveRecord relation
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/quo/relation_backed_query_specification.rb', line 30 def apply_to(relation) rel = relation rel = rel.select(*[:select]) if [:select] rel = rel.where([:where]) if [:where] rel = rel.order([:order]) if [:order] rel = rel.group(*[:group]) if [:group] rel = rel.limit([:limit]) if [:limit] rel = rel.offset([:offset]) if [:offset] if (joins = [:joins]) rel = joins.is_a?(Array) ? rel.joins(*joins) : rel.joins(joins) end if (left_outer_joins = [:left_outer_joins]) rel = left_outer_joins.is_a?(Array) ? rel.left_outer_joins(*left_outer_joins) : rel.left_outer_joins(left_outer_joins) end rel = rel.includes(*[:includes]) if [:includes] rel = rel.preload(*[:preload]) if [:preload] rel = rel.eager_load(*[:eager_load]) if [:eager_load] rel = rel.distinct if [:distinct] rel = rel.reorder([:reorder]) if [:reorder] rel = rel.extending(*[:extending]) if [:extending] rel = rel.unscope([:unscope]) if [:unscope] rel end |
#distinct(enabled = true) ⇒ Quo::RelationBackedQuerySpecification
138 139 140 |
# File 'lib/quo/relation_backed_query_specification.rb', line 138 def distinct(enabled = true) merge(distinct: enabled) end |
#eager_load(*associations) ⇒ Quo::RelationBackedQuerySpecification
132 133 134 |
# File 'lib/quo/relation_backed_query_specification.rb', line 132 def eager_load(*associations) merge(eager_load: associations) end |
#extending(*modules) ⇒ Quo::RelationBackedQuerySpecification
150 151 152 |
# File 'lib/quo/relation_backed_query_specification.rb', line 150 def extending(*modules) merge(extending: modules) end |
#group(*columns) ⇒ Quo::RelationBackedQuerySpecification
90 91 92 |
# File 'lib/quo/relation_backed_query_specification.rb', line 90 def group(*columns) merge(group: columns) end |
#has?(key) ⇒ Boolean
58 59 60 |
# File 'lib/quo/relation_backed_query_specification.rb', line 58 def has?(key) .key?(key) end |
#includes(*associations) ⇒ Quo::RelationBackedQuerySpecification
120 121 122 |
# File 'lib/quo/relation_backed_query_specification.rb', line 120 def includes(*associations) merge(includes: associations) end |
#joins(*tables) ⇒ Quo::RelationBackedQuerySpecification
108 109 110 |
# File 'lib/quo/relation_backed_query_specification.rb', line 108 def joins(*tables) merge(joins: tables) end |
#left_outer_joins(*tables) ⇒ Quo::RelationBackedQuerySpecification
114 115 116 |
# File 'lib/quo/relation_backed_query_specification.rb', line 114 def left_outer_joins(*tables) merge(left_outer_joins: tables) end |
#limit(value) ⇒ Quo::RelationBackedQuerySpecification
96 97 98 |
# File 'lib/quo/relation_backed_query_specification.rb', line 96 def limit(value) merge(limit: value) end |
#merge(new_options) ⇒ Quo::RelationBackedQuerySpecification
Creates a new specification with merged options
22 23 24 25 |
# File 'lib/quo/relation_backed_query_specification.rb', line 22 def merge() = . if .is_a?(self.class) self.class.new(.merge()) end |
#offset(value) ⇒ Quo::RelationBackedQuerySpecification
102 103 104 |
# File 'lib/quo/relation_backed_query_specification.rb', line 102 def offset(value) merge(offset: value) end |
#order(order_clause) ⇒ Quo::RelationBackedQuerySpecification
84 85 86 |
# File 'lib/quo/relation_backed_query_specification.rb', line 84 def order(order_clause) merge(order: order_clause) end |
#preload(*associations) ⇒ Quo::RelationBackedQuerySpecification
126 127 128 |
# File 'lib/quo/relation_backed_query_specification.rb', line 126 def preload(*associations) merge(preload: associations) end |
#reorder(order_clause) ⇒ Quo::RelationBackedQuerySpecification
144 145 146 |
# File 'lib/quo/relation_backed_query_specification.rb', line 144 def reorder(order_clause) merge(reorder: order_clause) end |
#select(*fields) ⇒ Quo::RelationBackedQuerySpecification
72 73 74 |
# File 'lib/quo/relation_backed_query_specification.rb', line 72 def select(*fields) merge(select: fields) end |
#unscope(*args) ⇒ Quo::RelationBackedQuerySpecification
156 157 158 |
# File 'lib/quo/relation_backed_query_specification.rb', line 156 def unscope(*args) merge(unscope: args) end |
#where(conditions) ⇒ Quo::RelationBackedQuerySpecification
78 79 80 |
# File 'lib/quo/relation_backed_query_specification.rb', line 78 def where(conditions) merge(where: conditions) end |