Module: Squeel::Adapters::ActiveRecord::Relation
- Defined in:
- lib/squeel/adapters/active_record/relation.rb
Constant Summary collapse
- JoinAssociation =
::ActiveRecord::Associations::JoinDependency::JoinAssociation
- JoinDependency =
::ActiveRecord::Associations::JoinDependency
Instance Attribute Summary collapse
- #join_dependency ⇒ Object readonly
Class Method Summary collapse
-
.included(base) ⇒ Object
ZOMG ALIAS_METHOD_CHAIN IS BELOW.
Instance Method Summary collapse
- #build_arel ⇒ Object
- #build_join_dependency(manager, joins) ⇒ Object
- #build_where(opts, other = []) ⇒ Object
- #collapse_wheres(arel, wheres) ⇒ Object
-
#debug_sql ⇒ Object
Simulate the logic that occurs in #to_a.
- #find_equality_predicates(nodes) ⇒ Object
- #having(*args) ⇒ Object
- #infer_association_for_relation_merge(r) ⇒ Object
- #joins(*args) ⇒ Object
- #merge(r, association_name = nil) ⇒ Object
- #order(*args) ⇒ Object
- #order_visitor ⇒ Object
- #predicate_visitor ⇒ Object
- #prepare_relation_for_association_merge!(r, association_name) ⇒ Object
- #relation_with_different_base?(r) ⇒ Boolean
- #select(value = Proc.new) ⇒ Object
- #select_visitor ⇒ Object
- #where(opts = Proc.new, *rest) ⇒ Object
- #where_values_hash_with_squeel ⇒ Object
Instance Attribute Details
#join_dependency ⇒ Object
14 15 16 |
# File 'lib/squeel/adapters/active_record/relation.rb', line 14 def join_dependency @join_dependency ||= (build_join_dependency(table.from(table), @joins_values) && @join_dependency) end |
Class Method Details
.included(base) ⇒ Object
ZOMG ALIAS_METHOD_CHAIN IS BELOW. HIDE YOUR EYES! … … … Since you’re still looking, let me explain this horrible transgression you see before you. You see, Relation#where_values_hash is defined on the ActiveRecord::Relation class. Since it’s defined there, but I would very much like to modify its behavior, I have three choices.
-
Inherit from ActiveRecord::Relation in a Squeel::Relation class, and make an attempt to usurp all of the various calls to methods on ActiveRecord::Relation by doing some really evil stuff with constant reassignment, all for the sake of being able to use super().
-
Submit a patch to Rails core, breaking this method off into another module, all for my own selfish desire to use super() while mucking about in Rails internals.
-
Use alias_method_chain, and say 10 hail Hanssons as penance.
I opted to go with #3. Except for the hail Hansson thing. Unless you’re DHH, in which case, I totally said them.
277 278 279 280 281 |
# File 'lib/squeel/adapters/active_record/relation.rb', line 277 def self.included(base) base.class_eval do alias_method_chain :where_values_hash, :squeel end end |
Instance Method Details
#build_arel ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/squeel/adapters/active_record/relation.rb', line 63 def build_arel arel = table.from table build_join_dependency(arel, @joins_values) unless @joins_values.empty? predicate_viz = predicate_visitor collapse_wheres(arel, predicate_viz.accept((@where_values - ['']).uniq)) arel.having(*predicate_viz.accept(@having_values.uniq.reject{|h| h.blank?})) unless @having_values.empty? arel.take(connection.sanitize_limit(@limit_value)) if @limit_value arel.skip(@offset_value) if @offset_value arel.group(*@group_values.uniq.reject{|g| g.blank?}) unless @group_values.empty? unless @order_values.empty? order_viz = order_visitor arel.order(*order_viz.accept(@order_values.uniq.reject{|o| o.blank?})) end build_select(arel, select_visitor.accept(@select_values.uniq)) arel.from(@from_value) if @from_value arel.lock(@lock_value) if @lock_value arel end |
#build_join_dependency(manager, joins) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/squeel/adapters/active_record/relation.rb', line 92 def build_join_dependency(manager, joins) buckets = joins.group_by do |join| case join when String 'string_join' when Hash, Symbol, Array, Nodes::Stub, Nodes::Join, Nodes::KeyPath 'association_join' when JoinAssociation 'stashed_join' when Arel::Nodes::Join 'join_node' else raise 'unknown class: %s' % join.class.name end end association_joins = buckets['association_join'] || [] stashed_association_joins = buckets['stashed_join'] || [] join_nodes = buckets['join_node'] || [] string_joins = (buckets['string_join'] || []).map { |x| x.strip }.uniq join_list = custom_join_ast(manager, string_joins) # All of this duplication just to add self.join_dependency = JoinDependency.new( @klass, association_joins, join_list ) join_nodes.each do |join| join_dependency.alias_tracker.aliased_name_for(join.left.name.downcase) end join_dependency.graft(*stashed_association_joins) @implicit_readonly = true unless association_joins.empty? && stashed_association_joins.empty? join_dependency.join_associations.each do |association| association.join_to(manager) end manager.join_sources.concat join_nodes.uniq manager.join_sources.concat join_list manager end |
#build_where(opts, other = []) ⇒ Object
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/squeel/adapters/active_record/relation.rb', line 164 def build_where(opts, other = []) case opts when String, Array super else # Let's prevent PredicateBuilder from doing its thing [opts, *other].map do |arg| case arg when Array # Just in case there's an array in there somewhere @klass.send(:sanitize_sql, arg) when Hash @klass.send(:expand_hash_conditions_for_aggregates, arg) else arg end end end end |
#collapse_wheres(arel, wheres) ⇒ Object
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/squeel/adapters/active_record/relation.rb', line 206 def collapse_wheres(arel, wheres) wheres = [wheres] unless Array === wheres binaries = wheres.grep(Arel::Nodes::Binary) groups = binaries.group_by {|b| [b.class, b.left]} groups.each do |_, bins| arel.where(Arel::Nodes::And.new(bins)) end (wheres - binaries).each do |where| where = Arel.sql(where) if String === where arel.where(Arel::Nodes::Grouping.new(where)) end end |
#debug_sql ⇒ Object
Simulate the logic that occurs in #to_a
This will let us get a dump of the SQL that will be run against the DB for debug purposes without actually running the query.
241 242 243 244 245 246 247 248 249 |
# File 'lib/squeel/adapters/active_record/relation.rb', line 241 def debug_sql if eager_loading? including = (@eager_load_values + @includes_values).uniq join_dependency = JoinDependency.new(@klass, including, []) construct_relation_for_association_find(join_dependency).to_sql else arel.to_sql end end |
#find_equality_predicates(nodes) ⇒ Object
222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/squeel/adapters/active_record/relation.rb', line 222 def find_equality_predicates(nodes) nodes.map { |node| case node when Arel::Nodes::Equality node when Arel::Nodes::Grouping find_equality_predicates([node.expr]) when Arel::Nodes::And find_equality_predicates(node.children) else nil end }.compact.flatten end |
#having(*args) ⇒ Object
198 199 200 201 202 203 204 |
# File 'lib/squeel/adapters/active_record/relation.rb', line 198 def having(*args) if block_given? && args.empty? super(DSL.evaluate &Proc.new) else super end end |
#infer_association_for_relation_merge(r) ⇒ Object
52 53 54 55 |
# File 'lib/squeel/adapters/active_record/relation.rb', line 52 def infer_association_for_relation_merge(r) default_association = reflect_on_all_associations.detect {|a| a.class_name == r.klass.name} default_association ? default_association.name : r.table_name.to_sym end |
#joins(*args) ⇒ Object
190 191 192 193 194 195 196 |
# File 'lib/squeel/adapters/active_record/relation.rb', line 190 def joins(*args) if block_given? && args.empty? super(DSL.evaluate &Proc.new) else super end end |
#merge(r, association_name = nil) ⇒ Object
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/squeel/adapters/active_record/relation.rb', line 36 def merge(r, association_name = nil) if association_name || relation_with_different_base?(r) r = r.clone association_name ||= infer_association_for_relation_merge(r) prepare_relation_for_association_merge!(r, association_name) self.joins_values += [association_name] if reflect_on_association(association_name) end super(r) end |
#order(*args) ⇒ Object
182 183 184 185 186 187 188 |
# File 'lib/squeel/adapters/active_record/relation.rb', line 182 def order(*args) if block_given? && args.empty? super(DSL.evaluate &Proc.new) else super end end |
#order_visitor ⇒ Object
30 31 32 33 34 |
# File 'lib/squeel/adapters/active_record/relation.rb', line 30 def order_visitor Visitors::OrderVisitor.new( Contexts::JoinDependencyContext.new(join_dependency) ) end |
#predicate_visitor ⇒ Object
24 25 26 27 28 |
# File 'lib/squeel/adapters/active_record/relation.rb', line 24 def predicate_visitor Visitors::PredicateVisitor.new( Contexts::JoinDependencyContext.new(join_dependency) ) end |
#prepare_relation_for_association_merge!(r, association_name) ⇒ Object
57 58 59 60 61 |
# File 'lib/squeel/adapters/active_record/relation.rb', line 57 def prepare_relation_for_association_merge!(r, association_name) r.where_values.map! {|w| Squeel::Visitors::PredicateVisitor.can_accept?(w) ? {association_name => w} : w} r.having_values.map! {|h| Squeel::Visitors::PredicateVisitor.can_accept?(h) ? {association_name => h} : h} r.joins_values.map! {|j| [Symbol, Hash, Nodes::Stub, Nodes::Join].include?(j.class) ? {association_name => j} : j} end |
#relation_with_different_base?(r) ⇒ Boolean
47 48 49 50 |
# File 'lib/squeel/adapters/active_record/relation.rb', line 47 def relation_with_different_base?(r) ::ActiveRecord::Relation === r && base_class.name != r.klass.base_class.name end |
#select(value = Proc.new) ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/squeel/adapters/active_record/relation.rb', line 142 def select(value = Proc.new) if block_given? && Proc === value if value.arity > 0 to_a.select {|*block_args| value.call(*block_args)} else relation = clone relation.select_values += Array.wrap(DSL.evaluate &value) relation end else super end end |
#select_visitor ⇒ Object
18 19 20 21 22 |
# File 'lib/squeel/adapters/active_record/relation.rb', line 18 def select_visitor Visitors::SelectVisitor.new( Contexts::JoinDependencyContext.new(join_dependency) ) end |
#where(opts = Proc.new, *rest) ⇒ Object
156 157 158 159 160 161 162 |
# File 'lib/squeel/adapters/active_record/relation.rb', line 156 def where(opts = Proc.new, *rest) if block_given? && Proc === opts super(DSL.evaluate &opts) else super end end |
#where_values_hash_with_squeel ⇒ Object
283 284 285 286 287 |
# File 'lib/squeel/adapters/active_record/relation.rb', line 283 def where_values_hash_with_squeel equalities = find_equality_predicates(predicate_visitor.accept(@where_values)) Hash[equalities.map { |where| [where.left.name, where.right] }] end |