Guest User

Untitled

a guest
May 26th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. # TODO: add a check to make sure a relationship doens't already exist before insert
  2.  
  3. module ActiveRecord
  4. module Associations # :nodoc:
  5.  
  6. module ClassMethods
  7.  
  8. def has_many_polymorphs(association_id, options={})
  9.  
  10. pjoin = options[:polymorphic_join_table]
  11.  
  12. #from polymorphic column field names
  13. pfrom = {:id_label => (options[:polymorphic_from].to_s + '_id').to_sym,:type_label => (options[:polymorphic_from].to_s + '_type').to_sym, :type => self}
  14.  
  15. #to polymorphic column field names
  16. pto = {:id_label => (options[:polymorphic_to].to_s + '_id').to_sym,:type_label => (options[:polymorphic_to].to_s + '_type').to_sym,:type => association_id.to_s.classify.to_s, :table => association_id}
  17.  
  18. single_direction_finder = "SELECT #{pto[:table]}.* " +
  19. "FROM #{pto[:table]} INNER JOIN #{pjoin} ON #{pto[:table]}.id = #{pjoin}.#{pto[:id_label]} " +
  20. "WHERE #{pjoin}.#{pfrom[:id_label]} =" + ' #{id} ' +
  21. "AND #{pjoin}.#{pfrom[:type_label]} = '#{pfrom[:type]}' AND #{pjoin}.#{pto[:type_label]} = '#{pto[:type]}'"
  22.  
  23. bidirectional_finder = "SELECT #{pto[:table]}.* " +
  24. "FROM #{pto[:table]} INNER JOIN #{pjoin} ON " +
  25. "(#{pto[:table]}.id = #{pjoin}.#{pto[:id_label]} AND #{pjoin}.#{pfrom[:type_label]} = '#{pfrom[:type]}') OR " +
  26. "(#{pto[:table]}.id = #{pjoin}.#{pfrom[:id_label]} AND #{pjoin}.#{pto[:type_label]} = '#{pfrom[:type]}') " +
  27. "WHERE " +
  28. "(#{pjoin}.#{pfrom[:id_label]} =" + ' #{id} ' + " AND #{pjoin}.#{pfrom[:type_label]} = '#{pfrom[:type]}' AND #{pjoin}.#{pto[:type_label]} = '#{pto[:type]}') " +
  29. "OR " +
  30. "(#{pjoin}.#{pto[:id_label]} =" + ' #{id} ' + " AND #{pjoin}.#{pto[:type_label]} = '#{pfrom[:type]}' AND #{pjoin}.#{pfrom[:type_label]} = '#{pto[:type]}') "
  31.  
  32. class_eval do
  33.  
  34. has_and_belongs_to_many pto[:table],
  35. {:join_table => pjoin,
  36. :foreign_key => pfrom[:id_label],
  37. :association_foreign_key => pto[:id_label],
  38. :finder_sql => (options[:bidirectional].blank? ? single_direction_finder : bidirectional_finder),
  39. :insert_sql =>
  40. "INSERT INTO #{pjoin} (`#{pfrom[:id_label]}`, `#{pfrom[:type_label]}`, `#{pto[:id_label]}`, `#{pto[:type_label]}`) " +
  41. 'VALUES (#{id}, ' + "'#{pfrom[:type]}', " + '#{record.id}, ' + "'#{pto[:type]}')",
  42. :delete_sql =>
  43. "DELETE FROM #{pjoin} WHERE #{pfrom[:id_label]} = " +
  44. '#{id}' + " AND #{pfrom[:type_label]} = '#{pfrom[:type]}' AND #{pto[:id_label]} = " +
  45. '#{record.id}' + " AND #{pto[:type_label]} = '#{pto[:type]}'"
  46. }
  47. end #end clas_eval
  48.  
  49. end #end def
  50.  
  51.  
  52. end #end classmethods
  53. end #associatioins
  54.  
  55. end #end active record
Add Comment
Please, Sign In to add comment