mongoid_counter_cache 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in mongoid_counter_cache.gemspec
4
+ gemspec
data/README.markdown ADDED
@@ -0,0 +1,17 @@
1
+ ##Support:
2
+
3
+ **Rails3.0+/Mongoid2.0.2+**
4
+
5
+ ##Usage:
6
+
7
+ class Forum
8
+ references_many :topics
9
+ references_many :posts
10
+ end
11
+
12
+ class Topic
13
+ referenced_in :forum
14
+ include Mongoid::CounterCache
15
+ counter_cache name: :forum, inverse_of: :posts
16
+ end
17
+
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,3 @@
1
+ module MongoidCounterCache
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,52 @@
1
+ require 'rails'
2
+ require "mongoid_counter_cache/version"
3
+
4
+ #
5
+ # lib/mongoid/counter_cache.rb
6
+ # ruby
7
+ #
8
+ # Created by Zhang Alex on 2011-06-17.
9
+ # Copyright 2011 __KittyPad.com__. All rights reserved.
10
+ #
11
+ # ===================================
12
+ # class Forum
13
+ # references_many :topics
14
+ # references_many :posts
15
+ # end
16
+ #
17
+ #
18
+ # class Topic
19
+ # referenced_in :forum
20
+ # include Mongoid::CounterCache
21
+ # counter_cache name: :forum, inverse_of: :posts
22
+ # end
23
+ # ===================================
24
+
25
+ module Mongoid
26
+ module CounterCache
27
+ extend ActiveSupport::Concern
28
+
29
+ module ClassMethods
30
+ def counter_cache(metadata)
31
+ counter_name = "#{metadata[:inverse_of]}_count"
32
+
33
+ set_callback(:create, :after) do |document|
34
+ relation = document.send(metadata[:name])
35
+ if relation
36
+ relation.inc(counter_name.to_sym, 1) if relation.class.fields.keys.include?(counter_name)
37
+ end
38
+ end
39
+
40
+ set_callback(:destroy, :after) do |document|
41
+ relation = document.send(metadata[:name])
42
+ if relation && relation.class.fields.keys.include?(counter_name)
43
+ relation.inc(counter_name.to_sym, -1)
44
+ end
45
+ end
46
+
47
+ end
48
+
49
+ end #ClassMethods
50
+
51
+ end #CounterCache
52
+ end #Mongoid
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid_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
+ - AlexZhang
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-07-01 00:00:00 +08: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: 23
30
+ segments:
31
+ - 3
32
+ - 0
33
+ - 8
34
+ version: 3.0.8
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: mongoid
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 3
46
+ segments:
47
+ - 0
48
+ version: "0"
49
+ type: :runtime
50
+ version_requirements: *id002
51
+ - !ruby/object:Gem::Dependency
52
+ name: bson_ext
53
+ prerelease: false
54
+ requirement: &id003 !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ hash: 3
60
+ segments:
61
+ - 0
62
+ version: "0"
63
+ type: :runtime
64
+ version_requirements: *id003
65
+ description: Mongoid Counter Cache
66
+ email:
67
+ - blackanger.z@gmail.com
68
+ executables: []
69
+
70
+ extensions: []
71
+
72
+ extra_rdoc_files: []
73
+
74
+ files:
75
+ - Gemfile
76
+ - Rakefile
77
+ - README.markdown
78
+ - lib/mongoid_counter_cache/version.rb
79
+ - lib/mongoid_counter_cache.rb
80
+ has_rdoc: true
81
+ homepage: ""
82
+ licenses: []
83
+
84
+ post_install_message:
85
+ rdoc_options: []
86
+
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ hash: 3
95
+ segments:
96
+ - 0
97
+ version: "0"
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ hash: 3
104
+ segments:
105
+ - 0
106
+ version: "0"
107
+ requirements: []
108
+
109
+ rubyforge_project: mongoid_counter_cache
110
+ rubygems_version: 1.6.2
111
+ signing_key:
112
+ specification_version: 3
113
+ summary: Mongoid Counter Cache
114
+ test_files: []
115
+