# File lib/thinking_sphinx/active_record/associations.rb, line 6 def initialize(model) @model = model @joins = ActiveSupport::OrderedHash.new end
# File lib/thinking_sphinx/active_record/associations.rb, line 11 def add_join_to(stack) join_for(stack) end
# File lib/thinking_sphinx/active_record/associations.rb, line 15 def aggregate_for?(stack) return false if stack.empty? joins_for(stack).compact.any? { |join| [:has_many, :has_and_belongs_to_many].include?( join.reflection.macro ) } end
# File lib/thinking_sphinx/active_record/associations.rb, line 25 def alias_for(stack) return model.quoted_table_name if stack.empty? join_for(stack).aliased_table_name end
# File lib/thinking_sphinx/active_record/associations.rb, line 31 def join_values @joins.values.compact end
# File lib/thinking_sphinx/active_record/associations.rb, line 35 def model_for(stack) return model if stack.empty? join = join_for(stack) join.nil? ? nil : join.reflection.klass end
# File lib/thinking_sphinx/active_record/associations.rb, line 44 def base @base ||= JoinDependency.new model, [], [] end
# File lib/thinking_sphinx/active_record/associations.rb, line 48 def join_for(stack) @joins[stack] ||= begin reflection = reflection_for stack reflection.nil? ? nil : JoinDependency::JoinAssociation.new( reflection, base, parent_join_for(stack) ).tap { |join| join.join_type = Arel::OuterJoin rewrite_conditions_for join } end end
# File lib/thinking_sphinx/active_record/associations.rb, line 61 def joins_for(stack) if stack.length == 1 [join_for(stack)] else [joins_for(stack[0..-2]), join_for(stack)].flatten end end
# File lib/thinking_sphinx/active_record/associations.rb, line 69 def parent_for(stack) stack.length == 1 ? base : join_for(stack[0..-2]) end
# File lib/thinking_sphinx/active_record/associations.rb, line 73 def parent_join_for(stack) stack.length == 1 ? base.join_base : parent_for(stack) end
# File lib/thinking_sphinx/active_record/associations.rb, line 77 def reflection_for(stack) parent = parent_for(stack) klass = parent.respond_to?(:base_klass) ? parent.base_klass : parent.active_record klass.reflections[stack.last] end
# File lib/thinking_sphinx/active_record/associations.rb, line 84 def rewrite_conditions_for(join) if join.respond_to?(:scope_chain) conditions = Array(join.scope_chain).flatten else conditions = Array(join.conditions).flatten end conditions.each do |condition| next unless condition.is_a?(String) condition.gsub! /::ts_join_alias::/, model.connection.quote_table_name(join.parent.aliased_table_name) end end