custom_counter_cache 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,3 @@
1
+ module CustomCounterCache
2
+ require 'custom_counter_cache/model'
3
+ end
@@ -0,0 +1,59 @@
1
+ module CustomCounterCache::Model
2
+
3
+ def self.included(base)
4
+ base.extend ActsAsMethods
5
+ end
6
+
7
+ module ActsAsMethods
8
+
9
+ def define_counter_cache(cache_column, &block)
10
+ # counter accessors
11
+ unless column_names.include?(cache_column) # Object.const_defined?(:Counter)
12
+ has_many :counters, :as => :countable, :dependent => :destroy
13
+ define_method "#{cache_column}" do
14
+ counters.find(:first, :conditions => { :key => cache_column.to_s }).value rescue 0
15
+ end
16
+ define_method "#{cache_column}=" do |count|
17
+ if ( counter = counters.find(:first, :conditions => { :key => cache_column.to_s }) )
18
+ counter.update_attribute :value, count.to_i
19
+ else
20
+ counters.create :key => cache_column.to_s, :value => count.to_i
21
+ end
22
+ end
23
+ end
24
+ # counter update method
25
+ define_method "update_#{cache_column}" do
26
+ update_attribute cache_column, block.call(self)
27
+ end
28
+ end
29
+
30
+ def update_counter_cache(association_id, cache_column, options = {})
31
+ association_id = association_id.to_sym
32
+ cache_column = cache_column.to_sym
33
+ method_name = "callback_#{cache_column}".to_sym
34
+ reflection = reflect_on_association(association_id)
35
+ # define callback
36
+ define_method method_name do
37
+ # update old association
38
+ if send("#{reflection.association_foreign_key}_changed?")
39
+ old_assoc_id = send("#{reflection.association_foreign_key}_was")
40
+ if ( old_assoc_id && old_assoc = reflection.klass.find(old_assoc_id))
41
+ old_assoc.send("update_#{cache_column}")
42
+ end
43
+ end
44
+ # update new association
45
+ new_assoc_id = send(reflection.association_foreign_key)
46
+ if ( new_assoc_id && new_assoc = reflection.klass.find(new_assoc_id) )
47
+ new_assoc.send("update_#{cache_column}")
48
+ end
49
+ end
50
+ # set callbacks
51
+ after_save method_name, :if => options.delete(:if)
52
+ after_destroy method_name, :if => options.delete(:if)
53
+ end
54
+
55
+ end
56
+
57
+ end
58
+
59
+ ActiveRecord::Base.send :include, CustomCounterCache::Model
@@ -0,0 +1,5 @@
1
+ module CustomCounterCache
2
+
3
+ VERSION = '0.0.1'
4
+
5
+ end
@@ -0,0 +1,15 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper')
2
+
3
+ class ControllerTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ end
7
+
8
+ def teardown
9
+ end
10
+
11
+ def test_foobar
12
+ assert true
13
+ end
14
+
15
+ end
@@ -0,0 +1,12 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper')
2
+
3
+ class HelperTest < ActionView::TestCase
4
+
5
+ def setup
6
+ end
7
+
8
+ def test_foobar
9
+ assert true
10
+ end
11
+
12
+ end
@@ -0,0 +1,11 @@
1
+ $: << File.join(File.dirname(__FILE__), '..', 'lib')
2
+ $: << File.join(File.dirname(__FILE__))
3
+
4
+ require 'rubygems'
5
+ require 'test/unit'
6
+ require 'active_support'
7
+ require 'action_controller'
8
+ require 'action_controller/test_case'
9
+ require 'action_view'
10
+ require 'active_record'
11
+ require 'custom_counter_cache'
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: custom_counter_cache
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Cedric Howe
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-05-04 00:00:00 -04:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rails
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 5
30
+ segments:
31
+ - 2
32
+ - 3
33
+ version: "2.3"
34
+ type: :runtime
35
+ version_requirements: *id001
36
+ description: ""
37
+ email: cedric@freezerbox.com
38
+ executables: []
39
+
40
+ extensions: []
41
+
42
+ extra_rdoc_files: []
43
+
44
+ files:
45
+ - lib/custom_counter_cache/model.rb
46
+ - lib/custom_counter_cache/version.rb
47
+ - lib/custom_counter_cache.rb
48
+ - test/controller_test.rb
49
+ - test/helper_test.rb
50
+ - test/test_helper.rb
51
+ has_rdoc: true
52
+ homepage: http://github.com/cedric/custom_counter_cache/
53
+ licenses: []
54
+
55
+ post_install_message:
56
+ rdoc_options: []
57
+
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ hash: 3
66
+ segments:
67
+ - 0
68
+ version: "0"
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ hash: 23
75
+ segments:
76
+ - 1
77
+ - 3
78
+ - 6
79
+ version: 1.3.6
80
+ requirements: []
81
+
82
+ rubyforge_project: custom_counter_cache
83
+ rubygems_version: 1.4.2
84
+ signing_key:
85
+ specification_version: 3
86
+ summary: Custom counter_cache functionality that supports conditions and multiple models.
87
+ test_files:
88
+ - test/controller_test.rb
89
+ - test/helper_test.rb
90
+ - test/test_helper.rb