lib/bullet/association.rb in flyerhzm-bullet-1.5.8 vs lib/bullet/association.rb in flyerhzm-bullet-1.5.9

- old
+ new

@@ -17,10 +17,11 @@ @@callers = nil @@possible_objects = nil @@impossible_objects = nil @@call_object_associations = nil @@eager_loadings = nil + @@klazz_associations = nil end def notification? check_unused_preload_associations has_unpreload_associations? or has_unused_preload_associations? @@ -71,12 +72,30 @@ end def add_eager_loadings(objects, associations) objects = Array(objects) eager_loadings[objects] ||= [] - eager_loadings[objects] << associations - unique(eager_loadings[objects]) + eager_loadings.each do |k, v| + unless (k & objects).empty? + if (k & objects) == k + eager_loadings[k] = (eager_loadings[k] + Array(associations)) + unique(eager_loadings[k]) + break + else + eager_loadings.merge!({(k & objects) => (eager_loadings[k] + Array(associations))}) + unique(eager_loadings[(k & objects)]) + eager_loadings.merge!({(k - objects) => eager_loadings[k]}) unless (k - objects).empty? + unique(eager_loadings[(k - objects)]) + eager_loadings.delete(k) + objects = objects - k + end + end + end + unless objects.empty? + eager_loadings[objects] << Array(associations) + unique(eager_loadings[objects]) + end end def define_association(klazz, associations) add_klazz_associations(klazz, associations) end @@ -106,13 +125,32 @@ !unpreload_associations.empty? end private def unpreload_associations?(object, associations) + possible?(object) and !impossible?(object) and !association?(object, associations) + end + + def possible?(object) klazz = object.class - (!possible_objects[klazz].nil? and possible_objects[klazz].include?(object)) and - (impossible_objects[klazz].nil? or !impossible_objects[klazz].include?(object)) and - (object_associations[object].nil? or !object_associations[object].include?(associations)) + possible_objects[klazz] and possible_objects[klazz].include?(object) + end + + def impossible?(object) + klazz = object.class + impossible_objects[klazz] and impossible_objects[klazz].include?(object) + end + + def association?(object, associations) + object_associations.each do |key, value| + if key == object + value.each do |v| + result = v.is_a?(Hash) ? v.has_key?(associations) : v == associations + return true if result + end + end + end + return false end def notification_response response = [] if has_unused_preload_associations?