custom_counter_cache 0.0.4 → 0.0.5
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.
- data/lib/custom_counter_cache/model.rb +17 -13
- data/lib/custom_counter_cache/version.rb +1 -1
- data/test/test_helper.rb +0 -4
- metadata +11 -26
@@ -32,25 +32,29 @@ module CustomCounterCache::Model
|
|
32
32
|
end
|
33
33
|
end
|
34
34
|
|
35
|
-
def update_counter_cache(
|
36
|
-
|
37
|
-
cache_column
|
38
|
-
method_name
|
39
|
-
reflection
|
40
|
-
foreign_key
|
35
|
+
def update_counter_cache(association, cache_column, options = {})
|
36
|
+
association = association.to_sym
|
37
|
+
cache_column = cache_column.to_sym
|
38
|
+
method_name = "callback_#{cache_column}".to_sym
|
39
|
+
reflection = reflect_on_association(association)
|
40
|
+
foreign_key = reflection.options[:foreign_key] || reflection.association_foreign_key
|
41
41
|
# define callback
|
42
42
|
define_method method_name do
|
43
43
|
# update old association
|
44
|
-
if send("#{foreign_key}_changed?")
|
45
|
-
|
46
|
-
|
47
|
-
|
44
|
+
if send("#{foreign_key}_changed?") || ( !respond_to?("#{association}_type") || send("#{association}_type_changed?") )
|
45
|
+
old_id = send("#{foreign_key}_was")
|
46
|
+
klass = if reflection.options[:polymorphic]
|
47
|
+
( send("#{association}_type_was") || send("#{association}_type") ).constantize
|
48
|
+
else
|
49
|
+
reflection.klass
|
50
|
+
end
|
51
|
+
if ( old_id && record = klass.find(old_id) )
|
52
|
+
record.send("update_#{cache_column}")
|
48
53
|
end
|
49
54
|
end
|
50
55
|
# update new association
|
51
|
-
|
52
|
-
|
53
|
-
new_assoc.send("update_#{cache_column}")
|
56
|
+
if ( record = send(association) )
|
57
|
+
record.send("update_#{cache_column}")
|
54
58
|
end
|
55
59
|
end
|
56
60
|
# set callbacks
|
data/test/test_helper.rb
CHANGED
@@ -1,13 +1,9 @@
|
|
1
|
-
$: << File.join(File.dirname(__FILE__), '..', 'lib')
|
2
|
-
$: << File.join(File.dirname(__FILE__))
|
3
|
-
|
4
1
|
require 'rubygems'
|
5
2
|
require 'test/unit'
|
6
3
|
require 'sqlite3'
|
7
4
|
require 'action_view'
|
8
5
|
require 'active_record'
|
9
6
|
require 'custom_counter_cache'
|
10
|
-
require 'ruby-debug'
|
11
7
|
|
12
8
|
ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
|
13
9
|
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: custom_counter_cache
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 21
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 5
|
10
|
+
version: 0.0.5
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Cedric Howe
|
@@ -15,41 +15,25 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-
|
18
|
+
date: 2012-08-28 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: rails
|
22
|
-
|
23
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
22
|
+
version_requirements: &id001 !ruby/object:Gem::Requirement
|
24
23
|
none: false
|
25
24
|
requirements:
|
26
25
|
- - ">="
|
27
26
|
- !ruby/object:Gem::Version
|
28
27
|
hash: 5
|
29
28
|
segments:
|
30
|
-
- 2
|
31
29
|
- 3
|
32
|
-
version: "
|
30
|
+
version: "3"
|
33
31
|
type: :runtime
|
34
|
-
|
35
|
-
- !ruby/object:Gem::Dependency
|
36
|
-
name: sqlite3-ruby
|
32
|
+
requirement: *id001
|
37
33
|
prerelease: false
|
38
|
-
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
-
none: false
|
40
|
-
requirements:
|
41
|
-
- - ">="
|
42
|
-
- !ruby/object:Gem::Version
|
43
|
-
hash: 3
|
44
|
-
segments:
|
45
|
-
- 0
|
46
|
-
version: "0"
|
47
|
-
type: :development
|
48
|
-
version_requirements: *id002
|
49
34
|
- !ruby/object:Gem::Dependency
|
50
|
-
name:
|
51
|
-
|
52
|
-
requirement: &id003 !ruby/object:Gem::Requirement
|
35
|
+
name: sqlite3
|
36
|
+
version_requirements: &id002 !ruby/object:Gem::Requirement
|
53
37
|
none: false
|
54
38
|
requirements:
|
55
39
|
- - ">="
|
@@ -59,7 +43,8 @@ dependencies:
|
|
59
43
|
- 0
|
60
44
|
version: "0"
|
61
45
|
type: :development
|
62
|
-
|
46
|
+
requirement: *id002
|
47
|
+
prerelease: false
|
63
48
|
description: ""
|
64
49
|
email: cedric@howe.net
|
65
50
|
executables: []
|