emotions 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +14 -0
- data/lib/emotions/emotion.rb +10 -0
- data/lib/emotions/emotive.rb +8 -0
- data/lib/emotions/version.rb +1 -1
- metadata +3 -3
data/README.md
CHANGED
@@ -69,3 +69,17 @@ user.express!(:sad, picture)
|
|
69
69
|
user.sad_about?(picure)
|
70
70
|
# => true
|
71
71
|
```
|
72
|
+
|
73
|
+
Most of the times, you would want to get a quick look at about how many users expressed a certain emotion towards a certain picture. That could be an expensive operation.
|
74
|
+
|
75
|
+
However, if the *emotive* record has an `<emotion>_emotions_count` column, Emotions will populate its value with how many users expressed that emotion towards it.
|
76
|
+
|
77
|
+
```ruby
|
78
|
+
user.happy_about!(picture)
|
79
|
+
|
80
|
+
picture.happy_about.count
|
81
|
+
# SQL query that counts records and returns `1`
|
82
|
+
|
83
|
+
picture.happy_emotions_count
|
84
|
+
# Quick lookup into the column and returns `1`
|
85
|
+
```
|
data/lib/emotions/emotion.rb
CHANGED
@@ -12,5 +12,15 @@ module Emotions
|
|
12
12
|
# Associations
|
13
13
|
belongs_to :emotional, polymorphic: true
|
14
14
|
belongs_to :emotive, polymorphic: true
|
15
|
+
|
16
|
+
# Callbacks
|
17
|
+
after_create :update_emotion_counter
|
18
|
+
after_destroy :update_emotion_counter
|
19
|
+
|
20
|
+
protected
|
21
|
+
|
22
|
+
def update_emotion_counter
|
23
|
+
self.emotive.update_emotion_counter(self.emotion)
|
24
|
+
end
|
15
25
|
end
|
16
26
|
end
|
data/lib/emotions/emotive.rb
CHANGED
@@ -14,6 +14,14 @@ module Emotions
|
|
14
14
|
self.emotions.includes(:emotional)
|
15
15
|
end
|
16
16
|
|
17
|
+
def update_emotion_counter(emotion)
|
18
|
+
attribute = :"#{emotion}_emotions_count"
|
19
|
+
|
20
|
+
if self.respond_to?(attribute)
|
21
|
+
self.update_attribute(attribute, self.send("#{emotion}_about").count)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
17
25
|
module ClassMethods
|
18
26
|
def define_emotion_methods(emotion)
|
19
27
|
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
data/lib/emotions/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: emotions
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -97,7 +97,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
97
97
|
version: '0'
|
98
98
|
segments:
|
99
99
|
- 0
|
100
|
-
hash: -
|
100
|
+
hash: -3833006688586079833
|
101
101
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
102
|
none: false
|
103
103
|
requirements:
|
@@ -106,7 +106,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
106
106
|
version: '0'
|
107
107
|
segments:
|
108
108
|
- 0
|
109
|
-
hash: -
|
109
|
+
hash: -3833006688586079833
|
110
110
|
requirements: []
|
111
111
|
rubyforge_project:
|
112
112
|
rubygems_version: 1.8.23
|