Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit d0e92130ba262f40935dbffde9528e2813cba434 @betamatt committed Apr 27, 2009
Showing with 132 additions and 0 deletions.
  1. +20 −0 MIT-LICENSE
  2. +30 −0 README
  3. +22 −0 Rakefile
  4. +18 −0 init.rb
  5. +1 −0 install.rb
  6. +28 −0 lib/passive_counter_cache.rb
  7. +4 −0 tasks/passive_counter_cache_tasks.rake
  8. +8 −0 test/passive_counter_cache_test.rb
  9. +1 −0 uninstall.rb
@@ -0,0 +1,20 @@
+Copyright (c) 2009 Matt Griffin
+
+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.
30 README
@@ -0,0 +1,30 @@
+PassiveCounterCache
+===================
+
+Adds support for simple counter cache updating that can be hooked into background processing. Rails counter caching is disabled
+and a method is provided to update caches from a background process.
+
+
+Example
+=======
+
+class Cart < ActiveRecord::Base
+ has_many :wheels
+end
+
+class Wheel < ActiveRecord::Base
+ belongs_to :cart, :passive_counter_cache => true
+end
+
+Create a cart and four wheels
+>> c = Cart.create
+>> 4.times do { c.wheels.create }
+
+As part of a BackgroundRb (or similar) scheduled task execute:
+>> PassiveCounterCache.update_all_caches!
+
+>> c.reload!
+>> c.wheel_count
+=> 4
+
+Copyright (c) 2009 Matt Griffin, released under the MIT license
@@ -0,0 +1,22 @@
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+
+desc 'Default: run unit tests.'
+task :default => :test
+
+desc 'Test the passive_counter_cache plugin.'
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+end
+
+desc 'Generate documentation for the passive_counter_cache plugin.'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'PassiveCounterCache'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
18 init.rb
@@ -0,0 +1,18 @@
+require 'passive_counter_cache'
+
+module ActiveRecord
+ class Base
+ class << self
+ def belongs_to_with_passive_counter_cache(name, opts = {}, &block)
+ if opts.delete(:passive_counter_cache)
+ PassiveCounterCache.register_association_cache self, name, opts[:counter_cache] || "#{name.to_s}_count"
+ opts[:counter_cache] = false
+ end
+
+ belongs_to_without_passive_counter_cache(name, opts, &block)
+ end
+
+ alias_method_chain :belongs_to, :passive_counter_cache
+ end
+ end
+end
@@ -0,0 +1 @@
+# Install hook code here
@@ -0,0 +1,28 @@
+class PassiveCounterCache
+ @@caches = []
+
+ class << self
+ def register_association_cache(klass, association, column_name)
+ @@caches << [klass, association, column_name]
+ end
+
+ def update_all_caches!
+ @@caches.each do |cache|
+ update_association_cache(*cache)
+ end
+ nil
+ end
+
+ def update_association_cache(klass, association, column_name)
+ reflection = klass.reflect_on_association(association)
+
+ sql = "UPDATE `#{reflection.klass.table_name}`, (" +
+ "SELECT id, COUNT(*) as `count` FROM `#{klass.table_name}` GROUP BY `#{reflection.primary_key_name}`" +
+ ") counter_values " +
+ "SET `#{column_name}` = counter_values.`count` " +
+ "WHERE counter_values.id = `#{reflection.klass.table_name}`.`#{reflection.klass.primary_key}`"
+
+ klass.connection.update(sql, "Passive Counter Cache Update")
+ end
+ end
+end
@@ -0,0 +1,4 @@
+# desc "Explaining what the task does"
+# task :passive_counter_cache do
+# # Task goes here
+# end
@@ -0,0 +1,8 @@
+require 'test/unit'
+
+class PassiveCounterCacheTest < Test::Unit::TestCase
+ # Replace this with your real tests.
+ def test_this_plugin
+ flunk
+ end
+end
@@ -0,0 +1 @@
+# Uninstall hook code here

0 comments on commit d0e9213

Please sign in to comment.