Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ActiveRecord::Associations::HasManyThroughAssociation < ActiveRecord::Associations::AssociationProxy
- def find_target
- records = @reflection.klass.find(:all,
- :select => construct_select,
- :conditions => construct_conditions,
- :from => construct_from,
- :joins => construct_joins,
- :order => @reflection.options[:order],
- :limit => @reflection.options[:limit],
- :group => @reflection.options[:group],
- :include => @reflection.options[:include] || @reflection.source_reflection.options[:include]
- )
- @reflection.options[:uniq] ? records.to_set.to_a : records
- end
- def construct_joins(custom_joins = nil)
- reflection = @reflection
- joins = []
- while reflection.through_reflection
- joins << construct_one_join(reflection)
- reflection = reflection.through_reflection
- end
- "#{joins.join(' ')} #{custom_joins}"
- end
- def construct_one_join(reflection)
- polymorphic_join = nil
- if reflection.through_reflection.options[:as] || reflection.source_reflection.macro == :belongs_to
- reflection_primary_key = reflection.klass.primary_key
- source_primary_key = reflection.source_reflection.primary_key_name
- if reflection.options[:source_type]
- polymorphic_join = "AND %s.%s = %s" % [
- reflection.through_reflection.table_name, "#{reflection.source_reflection.options[:foreign_type]}",
- @owner.class.quote_value(reflection.options[:source_type])
- ]
- end
- else
- reflection_primary_key = reflection.source_reflection.primary_key_name
- source_primary_key = reflection.klass.primary_key
- if reflection.source_reflection.options[:as]
- polymorphic_join = "AND %s.%s = %s" % [
- reflection.table_name, "#{reflection.source_reflection.options[:as]}_type",
- @owner.class.quote_value(reflection.through_reflection.klass.name)
- ]
- end
- end
- "INNER JOIN %s ON %s.%s = %s.%s %s #{reflection.options[:joins]}" % [
- reflection.through_reflection.table_name,
- reflection.table_name, reflection_primary_key,
- reflection.through_reflection.table_name, source_primary_key,
- polymorphic_join
- ]
- end
- def last_through_reflection
- reflection = @reflection.through_reflection
- while reflection.through_reflection
- reflection = reflection.through_reflection
- end
- reflection
- end
- def construct_conditions
- table_name = last_through_reflection.table_name
- conditions = construct_quoted_owner_attributes(last_through_reflection).map do |attr, value|
- "#{table_name}.#{attr} = #{value}"
- end
- conditions << @reflection.source_reflection.options[:conditions] if @reflection.source_reflection.options[:conditions]
- conditions << sql_conditions if sql_conditions
- "(" + conditions.join(') AND (') + ")"
- end
- def one_condition(reflection)
- [
- conditions_of_reflection(reflection),
- ("#{reflection.table_name}.#{reflection.klass.inheritance_column} = #{reflection.klass.quote_value(reflection.klass.name.demodulize)}" unless reflection.klass.descends_from_active_record?),
- ("#{reflection.table_name}.#{reflection.klass.deleted_attribute} IS NULL" if reflection.klass.paranoid?)
- ]
- end
- def conditions_of_reflection(reflection)
- (interpolate_sql(reflection.klass.send(:sanitize_sql, reflection.options[:conditions])) if reflection.options[:conditions])
- end
- def conditions
- reflection = @reflection
- conditions = [conditions_of_reflection(reflection)]
- while reflection = reflection.through_reflection
- conditions += one_condition(reflection)
- end
- conditions.compact!
- @conditions ||= conditions.join(' AND ') unless conditions.empty?
- end
- alias_method :sql_conditions, :conditions
- end
Add Comment
Please, Sign In to add comment