Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit c74065514de6b5804af8a1246cabf693a0b82ee4 @flyerhzm flyerhzm committed Aug 13, 2009
Showing with 191 additions and 0 deletions.
  1. +20 −0 MIT-LICENSE
  2. +13 −0 README
  3. +23 −0 Rakefile
  4. +1 −0 init.rb
  5. +1 −0 install.rb
  6. +80 −0 lib/bullet.rb
  7. +4 −0 tasks/bullet_tasks.rake
  8. +48 −0 test/bullet_test.rb
  9. +1 −0 uninstall.rb
@@ -0,0 +1,20 @@
+Copyright (c) 2009 [name of plugin creator]
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,13 @@
+Bullet
+======
+
+Introduction goes here.
+
+
+Example
+=======
+
+Example goes here.
+
+
+Copyright (c) 2009 [name of plugin creator], released under the MIT license
@@ -0,0 +1,23 @@
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+
+desc 'Default: run unit tests.'
+task :default => :test
+
+desc 'Test the bullet plugin.'
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.libs << 'test'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+end
+
+desc 'Generate documentation for the bullet plugin.'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'Bullet'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
@@ -0,0 +1 @@
+# Include hook code here
@@ -0,0 +1 @@
+# Install hook code here
@@ -0,0 +1,80 @@
+module Bullet
+ class Association
+ class <<self
+ def start_request
+ @@associations = {}
+ end
+
+ def add_association(object, associations)
+ @@associations[object] = associations
+ end
+
+ def call_association(object, associations)
+ end
+
+ def end_request
+ end
+ end
+ end
+end
+
+module ActiveRecord
+ module AssociationPreload
+ module ClassMethods
+ def preload_associations(records, associations, preload_options={})
+ records = [records].flatten.compact.uniq
+ return if records.empty?
+ records.each do |record|
+ puts "add association"
+ Bullet::Association.add_association(record, associations)
+ end
+ case associations
+ when Array then associations.each {|association| preload_associations(records, association, preload_options)}
+ when Symbol, String then preload_one_association(records, associations.to_sym, preload_options)
+ when Hash then
+ associations.each do |parent, child|
+ raise "parent must be an association name" unless parent.is_a?(String) || parent.is_a?(Symbol)
+ preload_associations(records, parent, preload_options)
+ reflection = reflections[parent]
+ parents = records.map {|record| record.send(reflection.name)}.flatten.compact
+ unless parents.empty?
+ parents.first.class.preload_associations(parents, child)
+ end
+ end
+ end
+ end
+ end
+ end
+end
+
+module ActiveRecord
+ module Associations
+ module ClassMethods
+ def collection_reader_method(reflection, association_proxy_class)
+ define_method(reflection.name) do |*params|
+ puts "call association"
+ Bullet::Association.call_association(self, reflection.name)
+ force_reload = params.first unless params.empty?
+ association = association_instance_get(reflection.name)
+
+ unless association
+ association = association_proxy_class.new(self, reflection)
+ association_instance_set(reflection.name, association)
+ end
+
+ association.reload if force_reload
+
+ association
+ end
+
+ define_method("#{reflection.name.to_s.singularize}_ids") do
+ if send(reflection.name).loaded? || reflection.options[:finder_sql]
+ send(reflection.name).map(&:id)
+ else
+ send(reflection.name).all(:select => "#{reflection.quoted_table_name}.#{reflection.klass.primary_key}").map(&:id)
+ end
+ end
+ end
+ end
+ end
+end
@@ -0,0 +1,4 @@
+# desc "Explaining what the task does"
+# task :bullet do
+# # Task goes here
+# end
@@ -0,0 +1,48 @@
+require 'rubygems'
+require 'test/unit'
+require 'active_record'
+require 'active_record/associations'
+require File.join(File.dirname(__FILE__), '../lib/bullet')
+
+ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :dbfile => ':memory:')
+
+def setup_db
+ ActiveRecord::Schema.define(:version => 1) do
+ create_table :posts do |t|
+ t.column :name, :string
+ end
+
+ create_table :comments do |t|
+ t.column :name, :string
+ t.column :post_id, :string
+ end
+ end
+end
+
+def teardown_db
+ ActiveRecord::Base.connection.tables.each do |table|
+ ActiveRecord::Base.connection.drop_table(table)
+ end
+end
+
+class Post < ActiveRecord::Base
+ has_many :comments
+end
+
+class Comment < ActiveRecord::Base
+ belongs_to :post
+end
+
+class BulletTest < Test::Unit::Testcase
+ def setup
+ setup_db
+ end
+
+ def teardown
+ teardown_db
+ end
+
+ def test_detect_preload
+ assert true
+ end
+end
@@ -0,0 +1 @@
+# Uninstall hook code here

0 comments on commit c740655

Please sign in to comment.