Class: ActiveRecord::Associations::JoinDependency::JoinAssociation
- Inherits:
-
JoinPart
- Object
- JoinPart
- ActiveRecord::Associations::JoinDependency::JoinAssociation
- Defined in:
- lib/store_base_sti_class_for_4_0.rb,
lib/store_base_sti_class_for_4_1.rb,
lib/store_base_sti_class_for_4_2.rb,
lib/store_base_sti_class_for_5_0.rb
Overview
:nodoc:
Instance Method Summary collapse
- #join_constraints(foreign_table, foreign_klass, node, join_type, tables, scope_chain, chain) ⇒ Object
- #join_to(manager) ⇒ Object
Instance Method Details
#join_constraints(foreign_table, foreign_klass, node, join_type, tables, scope_chain, chain) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 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 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/store_base_sti_class_for_4_1.rb', line 35 def join_constraints(foreign_table, foreign_klass, node, join_type, tables, scope_chain, chain) joins = [] tables = tables.reverse scope_chain_index = 0 scope_chain = scope_chain.reverse # The chain starts with the target table, but we want to end with it here (makes # more sense in this context), so we reverse chain.reverse_each do |reflection| table = tables.shift klass = reflection.klass case reflection.source_macro when :belongs_to key = reflection.association_primary_key foreign_key = reflection.foreign_key else key = reflection.foreign_key foreign_key = reflection.active_record_primary_key end constraint = build_constraint(klass, table, key, foreign_table, foreign_key) scope_chain_items = scope_chain[scope_chain_index].map do |item| if item.is_a?(Relation) item else ActiveRecord::Relation.create(klass, table).instance_exec(node, &item) end end scope_chain_index += 1 default_scope = if klass.method(:build_default_scope).arity == 0 klass.send(:build_default_scope) else klass.send(:build_default_scope, ActiveRecord::Relation.create(klass, table)) end scope_chain_items.concat [default_scope].compact rel = scope_chain_items.inject(scope_chain_items.shift) do |left, right| left.merge right end if reflection.type # START PATCH # original: # constraint = constraint.and table[reflection.type].eq foreign_klass.base_class.name sti_class_name = ActiveRecord::Base.store_base_sti_class ? foreign_klass.base_class.name : foreign_klass.name constraint = constraint.and table[reflection.type].eq sti_class_name # END PATCH end if rel && !rel.arel.constraints.empty? constraint = constraint.and rel.arel.constraints end joins << table.create_join(table, table.create_on(constraint), join_type) # The current table in this iteration becomes the foreign table in the next foreign_table, foreign_klass = table, klass end joins end |
#join_to(manager) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/store_base_sti_class_for_4_0.rb', line 39 def join_to(manager) tables = @tables.dup foreign_table = parent_table foreign_klass = parent.base_klass # The chain starts with the target table, but we want to end with it here (makes # more sense in this context), so we reverse chain.reverse.each_with_index do |reflection, i| table = tables.shift case reflection.source_macro when :belongs_to key = reflection.association_primary_key foreign_key = reflection.foreign_key when :has_and_belongs_to_many # Join the join table first... manager.from(join( table, table[reflection.foreign_key]. eq(foreign_table[reflection.active_record_primary_key]) )) foreign_table, table = table, tables.shift key = reflection.association_primary_key foreign_key = reflection.association_foreign_key else key = reflection.foreign_key foreign_key = reflection.active_record_primary_key end constraint = build_constraint(reflection, table, key, foreign_table, foreign_key) scope_chain_items = scope_chain[i] if reflection.type # START PATCH # original: # scope_chain_items += [ # ActiveRecord::Relation.new(reflection.klass, table) # .where(reflection.type => foreign_klass.base_class.name) # ] if ActiveRecord::Base.store_base_sti_class scope_chain_items += [ ActiveRecord::Relation.new(reflection.klass, table) .where(reflection.type => foreign_klass.base_class.name) ] else scope_chain_items += [ ActiveRecord::Relation.new(reflection.klass, table) .where(reflection.type => ([foreign_klass] + foreign_klass.descendants).map(&:name)) ] end # END PATCH end scope_chain_items += [reflection.klass.send(:build_default_scope)].compact scope_chain_items.each do |item| unless item.is_a?(Relation) item = ActiveRecord::Relation.new(reflection.klass, table).instance_exec(self, &item) end constraint = constraint.and(item.arel.constraints) unless item.arel.constraints.empty? end manager.from(join(table, constraint)) # The current table in this iteration becomes the foreign table in the next foreign_table, foreign_klass = table, reflection.klass end manager end |