Guest User

Untitled

a guest
Apr 23rd, 2018
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. class ActiveRecord::Associations::HasManyThroughAssociation < ActiveRecord::Associations::AssociationProxy
  2.  
  3. def find_target
  4. records = @reflection.klass.find(:all,
  5. :select => construct_select,
  6. :conditions => construct_conditions,
  7. :from => construct_from,
  8. :joins => construct_joins,
  9. :order => @reflection.options[:order],
  10. :limit => @reflection.options[:limit],
  11. :group => @reflection.options[:group],
  12. :include => @reflection.options[:include] || @reflection.source_reflection.options[:include]
  13. )
  14. @reflection.options[:uniq] ? records.to_set.to_a : records
  15. end
  16.  
  17.  
  18. def construct_joins(custom_joins = nil)
  19. reflection = @reflection
  20. joins = []
  21. while reflection.through_reflection
  22. joins << construct_one_join(reflection)
  23. reflection = reflection.through_reflection
  24. end
  25. "#{joins.join(' ')} #{custom_joins}"
  26. end
  27.  
  28. def construct_one_join(reflection)
  29. polymorphic_join = nil
  30. if reflection.through_reflection.options[:as] || reflection.source_reflection.macro == :belongs_to
  31. reflection_primary_key = reflection.klass.primary_key
  32. source_primary_key = reflection.source_reflection.primary_key_name
  33. if reflection.options[:source_type]
  34. polymorphic_join = "AND %s.%s = %s" % [
  35. reflection.through_reflection.table_name, "#{reflection.source_reflection.options[:foreign_type]}",
  36. @owner.class.quote_value(reflection.options[:source_type])
  37. ]
  38. end
  39. else
  40. reflection_primary_key = reflection.source_reflection.primary_key_name
  41. source_primary_key = reflection.klass.primary_key
  42. if reflection.source_reflection.options[:as]
  43. polymorphic_join = "AND %s.%s = %s" % [
  44. reflection.table_name, "#{reflection.source_reflection.options[:as]}_type",
  45. @owner.class.quote_value(reflection.through_reflection.klass.name)
  46. ]
  47. end
  48. end
  49.  
  50. "INNER JOIN %s ON %s.%s = %s.%s %s #{reflection.options[:joins]}" % [
  51. reflection.through_reflection.table_name,
  52. reflection.table_name, reflection_primary_key,
  53. reflection.through_reflection.table_name, source_primary_key,
  54. polymorphic_join
  55. ]
  56. end
  57.  
  58. def last_through_reflection
  59. reflection = @reflection.through_reflection
  60. while reflection.through_reflection
  61. reflection = reflection.through_reflection
  62. end
  63. reflection
  64. end
  65.  
  66. def construct_conditions
  67. table_name = last_through_reflection.table_name
  68. conditions = construct_quoted_owner_attributes(last_through_reflection).map do |attr, value|
  69. "#{table_name}.#{attr} = #{value}"
  70. end
  71. conditions << @reflection.source_reflection.options[:conditions] if @reflection.source_reflection.options[:conditions]
  72. conditions << sql_conditions if sql_conditions
  73. "(" + conditions.join(') AND (') + ")"
  74. end
  75.  
  76.  
  77. def one_condition(reflection)
  78. [
  79. conditions_of_reflection(reflection),
  80. ("#{reflection.table_name}.#{reflection.klass.inheritance_column} = #{reflection.klass.quote_value(reflection.klass.name.demodulize)}" unless reflection.klass.descends_from_active_record?),
  81. ("#{reflection.table_name}.#{reflection.klass.deleted_attribute} IS NULL" if reflection.klass.paranoid?)
  82. ]
  83. end
  84.  
  85. def conditions_of_reflection(reflection)
  86. (interpolate_sql(reflection.klass.send(:sanitize_sql, reflection.options[:conditions])) if reflection.options[:conditions])
  87. end
  88.  
  89. def conditions
  90. reflection = @reflection
  91. conditions = [conditions_of_reflection(reflection)]
  92. while reflection = reflection.through_reflection
  93. conditions += one_condition(reflection)
  94. end
  95. conditions.compact!
  96. @conditions ||= conditions.join(' AND ') unless conditions.empty?
  97. end
  98. alias_method :sql_conditions, :conditions
  99.  
  100. end
Add Comment
Please, Sign In to add comment