Permalink
Browse files

restructure

  • Loading branch information...
1 parent 1d7fd15 commit 5d770e6eeb23cd92cf3c5d3ab82d61e1394d2dcc @flyerhzm flyerhzm committed Aug 15, 2009
Showing with 126 additions and 119 deletions.
  1. +2 −0 init.rb
  2. +1 −115 lib/bullet.rb
  3. +62 −0 lib/bullet/association.rb
  4. +4 −3 lib/bulletware.rb
  5. +54 −0 lib/hack/active_record.rb
  6. +3 −1 spec/spec_helper.rb
View
@@ -1,2 +1,4 @@
# Include hook code here
+require 'bullet'
+require 'hack/active_record'
ActionController::Dispatcher.middleware.use Bulletware
View
@@ -1,117 +1,3 @@
module Bullet
- class Association
- class <<self
- def start_request
- @@object_associations ||= {}
- @@unpreload_associations ||= {}
- @@possible_objects ||= {}
- end
-
- def end_request
- @@object_associations = nil
- @@unpreload_associations = nil
- @@possible_objects = nil
- end
-
- def unpreload_associations
- @@unpreload_associations
- end
-
- def has_unpreload_associations?
- !@@unpreload_associations.empty?
- end
-
- def unpreload_associations_str
- @@unpreload_associations.to_a.collect{|klazz, associations| "model: #{klazz} => assocations: #{associations}"}.join('\\n')
- end
-
- def has_klazz_association(klazz)
- !@@klazz_associations[klazz].nil? and @@klazz_associations.keys.include?(klazz)
- end
-
- def klazz_association(klazz)
- @@klazz_associations[klazz] || []
- end
-
- def define_association(klazz, associations)
- @@klazz_associations ||= {}
- @@klazz_associations[klazz] ||= []
- @@klazz_associations[klazz] << associations
- end
-
- def add_possible_objects(objects)
- klazz= objects.first.class
- @@possible_objects[klazz] ||= []
- @@possible_objects[klazz] << objects
- @@possible_objects[klazz].flatten!.uniq!
- end
-
- def add_association(object, associations)
- @@object_associations[object] ||= []
- @@object_associations[object] << associations
- end
-
- def call_association(object, associations)
- klazz = object.class
- if !@@possible_objects[klazz].nil? and@@possible_objects[klazz].include?(object) and (@@object_associations[object].nil? or !@@object_associations[object].include?(associations))
- @@unpreload_associations[klazz] = associations
- end
- end
- end
- end
-end
-
-ActiveRecord::ActiveRecordError # An ActiveRecord bug
-
-module ActiveRecord
- class Base
- class <<self
- alias_method :origin_find_every, :find_every
-
- def find_every(options)
- records = origin_find_every(options)
-
- if records and records.size > 1
- Bullet::Association.add_possible_objects(records)
- end
-
- records
- end
- end
- end
-
- module AssociationPreload
- module ClassMethods
- alias_method :origin_preload_associations, :preload_associations
-
- def preload_associations(records, associations, preload_options={})
- records = [records].flatten.compact.uniq
- return if records.empty?
- records.each do |record|
- Bullet::Association.add_association(record, associations)
- end
- origin_preload_associations(records, associations, preload_options={})
- end
- end
- end
-
- module Associations
- module ClassMethods
- alias_method :origin_collection_reader_method, :collection_reader_method
-
- def collection_reader_method(reflection, association_proxy_class)
- Bullet::Association.define_association(self, reflection.name)
- origin_collection_reader_method(reflection, association_proxy_class)
- end
- end
-
- class AssociationCollection
- alias_method :origin_load_target, :load_target
-
- def load_target
- Bullet::Association.call_association(@owner, @reflection.name)
- origin_load_target
- end
- end
- end
+ autoload :Association, 'bullet/association'
end
View
@@ -0,0 +1,62 @@
+module Bullet
+ class Association
+ class <<self
+ def start_request
+ @@object_associations ||= {}
+ @@unpreload_associations ||= {}
+ @@possible_objects ||= {}
+ end
+
+ def end_request
+ @@object_associations = nil
+ @@unpreload_associations = nil
+ @@possible_objects = nil
+ end
+
+ def unpreload_associations
+ @@unpreload_associations
+ end
+
+ def has_unpreload_associations?
+ !@@unpreload_associations.empty?
+ end
+
+ def unpreload_associations_str
+ @@unpreload_associations.to_a.collect{|klazz, associations| "model: #{klazz} => assocations: #{associations}"}.join('\\n')
+ end
+
+ def has_klazz_association(klazz)
+ !@@klazz_associations[klazz].nil? and @@klazz_associations.keys.include?(klazz)
+ end
+
+ def klazz_association(klazz)
+ @@klazz_associations[klazz] || []
+ end
+
+ def define_association(klazz, associations)
+ @@klazz_associations ||= {}
+ @@klazz_associations[klazz] ||= []
+ @@klazz_associations[klazz] << associations
+ end
+
+ def add_possible_objects(objects)
+ klazz= objects.first.class
+ @@possible_objects[klazz] ||= []
+ @@possible_objects[klazz] << objects
+ @@possible_objects[klazz].flatten!.uniq!
+ end
+
+ def add_association(object, associations)
+ @@object_associations[object] ||= []
+ @@object_associations[object] << associations
+ end
+
+ def call_association(object, associations)
+ klazz = object.class
+ if !@@possible_objects[klazz].nil? and@@possible_objects[klazz].include?(object) and (@@object_associations[object].nil? or !@@object_associations[object].include?(associations))
+ @@unpreload_associations[klazz] = associations
+ end
+ end
+ end
+ end
+end
View
@@ -6,17 +6,18 @@ def initialize(app)
def call(env)
Bullet::Association.start_request
status, headers, response = @app.call(env)
+ return [status, headers, response] if response.empty?
+
if Bullet::Association.has_unpreload_associations?
- if headers['Content-Type'].include? 'text/html'
+ if !headers['Content-Type'].nil? and headers['Content-Type'].include? 'text/html'
inserted_value = "<script type='text/javascript'>"
inserted_value << "alert('The request has N+1 queries as follows:\\n#{Bullet::Association.unpreload_associations_str}')"
inserted_value << "</script>\n"
response_body = response.body[0..-17] + inserted_value + response.body[-16..-1]
headers['Content-Length'] = response_body.length.to_s
end
- else
- response_body = response.body
end
+ response_body ||= response.body
Bullet::Association.end_request
[status, headers, response_body]
end
View
@@ -0,0 +1,54 @@
+ActiveRecord::ActiveRecordError # An ActiveRecord bug
+
+module ActiveRecord
+ class Base
+ class <<self
+ alias_method :origin_find_every, :find_every
+
+ def find_every(options)
+ records = origin_find_every(options)
+
+ if records and records.size > 1
+ Bullet::Association.add_possible_objects(records)
+ end
+
+ records
+ end
+ end
+ end
+
+ module AssociationPreload
+ module ClassMethods
+ alias_method :origin_preload_associations, :preload_associations
+
+ def preload_associations(records, associations, preload_options={})
+ records = [records].flatten.compact.uniq
+ return if records.empty?
+ records.each do |record|
+ Bullet::Association.add_association(record, associations)
+ end
+ origin_preload_associations(records, associations, preload_options={})
+ end
+ end
+ end
+
+ module Associations
+ module ClassMethods
+ alias_method :origin_collection_reader_method, :collection_reader_method
+
+ def collection_reader_method(reflection, association_proxy_class)
+ Bullet::Association.define_association(self, reflection.name)
+ origin_collection_reader_method(reflection, association_proxy_class)
+ end
+ end
+
+ class AssociationCollection
+ alias_method :origin_load_target, :load_target
+
+ def load_target
+ Bullet::Association.call_association(@owner, @reflection.name)
+ origin_load_target
+ end
+ end
+ end
+end
View
@@ -1,4 +1,6 @@
require 'rubygems'
require 'spec/autorun'
require 'active_record'
-require File.join(File.dirname(__FILE__), '../lib/bullet.rb')
+require File.join(File.dirname(__FILE__), '../lib/bullet/association')
+require File.join(File.dirname(__FILE__), '../lib/bullet')
+require File.join(File.dirname(__FILE__), '../lib/hack/active_record')

0 comments on commit 5d770e6

Please sign in to comment.