module ArelHelpers::JoinAssociation
Public Class Methods
join_association(table, association, join_type = Arel::Nodes::InnerJoin, &block)
click to toggle source
activerecord uses JoinDependency to automagically generate inner join statements for any type of association (belongs_to, has_many, and has_and_belongs_to_many). For example, for HABTM associations, two join statements are required. This method encapsulates that functionality and yields an intermediate object for chaining. It also allows you to use an outer join instead of the default inner via the join_type arg.
# File lib/arel-helpers/join_association.rb, line 19 def join_association(table, association, join_type = Arel::Nodes::InnerJoin, &block) if ActiveRecord::VERSION::STRING >= '5.0.0' join_association_5_0(table, association, join_type, &block) elsif ActiveRecord::VERSION::STRING >= '4.2.0' join_association_4_2(table, association, join_type, &block) elsif ActiveRecord::VERSION::STRING >= '4.1.0' join_association_4_1(table, association, join_type, &block) else join_association_3_1(table, association, join_type, &block) end end
Private Class Methods
join_association_3_1(table, association, join_type) { |left.to_sym, right| ... }
click to toggle source
# File lib/arel-helpers/join_association.rb, line 33 def join_association_3_1(table, association, join_type) associations = association.is_a?(Array) ? association : [association] join_dependency = ActiveRecord::Associations::JoinDependency.new(table, associations, []) manager = Arel::SelectManager.new(table) join_dependency.join_associations.each do |assoc| assoc.join_type = join_type assoc.join_to(manager) end manager.join_sources.map do |assoc| if block_given? # yield |assoc_name, join_conditions| right = yield assoc.left.name.to_sym, assoc.right assoc.class.new(assoc.left, right) else assoc end end end
join_association_4_1(table, association, join_type) { |left.to_sym, right| ... }
click to toggle source
# File lib/arel-helpers/join_association.rb, line 54 def join_association_4_1(table, association, join_type) associations = association.is_a?(Array) ? association : [association] join_dependency = ActiveRecord::Associations::JoinDependency.new(table, associations, []) join_dependency.join_constraints([]).map do |constraint| right = if block_given? yield constraint.left.name.to_sym, constraint.right else constraint.right end join_type.new(constraint.left, right) end end
join_association_4_2(table, association, join_type) { |left.to_sym, right| ... }
click to toggle source
ActiveRecord 4.2 moves bind variables out of the join classes and into the relation. For this reason, a method like ::join_association isn't able to add to the list of bind variables dynamically. To get around the problem, this method must return a string.
# File lib/arel-helpers/join_association.rb, line 74 def join_association_4_2(table, association, join_type) associations = association.is_a?(Array) ? association : [association] join_dependency = ActiveRecord::Associations::JoinDependency.new(table, associations, []) constraints = join_dependency.join_constraints([]) binds = constraints.flat_map do |info| info.binds.map { |bv| table.connection.quote(*bv.reverse) } end joins = constraints.flat_map do |constraint| constraint.joins.map do |join| right = if block_given? yield join.left.name.to_sym, join.right else join.right end join_type.new(join.left, right) end end join_strings = joins.map do |join| to_sql(join, table, binds) end join_strings.join(' ') end
join_association_5_0(table, association, join_type) { |left.to_sym, right| ... }
click to toggle source
# File lib/arel-helpers/join_association.rb, line 103 def join_association_5_0(table, association, join_type) associations = association.is_a?(Array) ? association : [association] join_dependency = ActiveRecord::Associations::JoinDependency.new(table, associations, []) constraints = join_dependency.join_constraints([], join_type) binds = constraints.flat_map do |info| prepared_binds = table.connection.prepare_binds_for_database(info.binds) prepared_binds.map { |value| table.connection.quote(value) } end joins = constraints.flat_map do |constraint| constraint.joins.map do |join| right = if block_given? yield join.left.name.to_sym, join.right else join.right end join_type.new(join.left, right) end end join_strings = joins.map do |join| to_sql(join, table, binds) end join_strings.join(' ') end
to_sql(node, table, binds)
click to toggle source
# File lib/arel-helpers/join_association.rb, line 135 def to_sql(node, table, binds) visitor = table.connection.visitor collect = visitor.accept(node, Arel::Collectors::Bind.new) collect.substitute_binds(binds).join end