emotions 0.1.7 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,7 @@
1
1
  require 'emotions/version'
2
2
 
3
3
  require 'active_record'
4
+ require 'emotions/errors'
4
5
  require 'emotions/emotion'
5
6
  require 'emotions/emotive'
6
7
  require 'emotions/emotional'
@@ -14,6 +15,26 @@ module Emotions
14
15
  def self.emotions
15
16
  @configuration.emotions ||= []
16
17
  end
18
+
19
+ def self.inject_into_active_record
20
+ @inject_into_active_record ||= Proc.new do
21
+ def self.acts_as_emotive
22
+ self.send :include, Emotions::Emotive
23
+ end
24
+
25
+ def self.acts_as_emotional
26
+ self.send :include, Emotions::Emotional
27
+ end
28
+
29
+ def self.emotional?
30
+ @emotional ||= self.ancestors.include?(Emotions::Emotional)
31
+ end
32
+
33
+ def self.emotive?
34
+ @emotive ||= self.ancestors.include?(Emotions::Emotive)
35
+ end
36
+ end
37
+ end
17
38
  end
18
39
 
19
40
  require 'emotions/railtie' if defined?(Rails) && Rails::VERSION::MAJOR >= 3
@@ -10,58 +10,107 @@ module Emotions
10
10
  end
11
11
  end
12
12
 
13
+ # @private
13
14
  def _emotions_about(emotive)
14
15
  self.emotions.where(emotive_id: emotive.id, emotive_type: emotive.class.name)
15
16
  end
16
17
 
18
+ # Return all emotions expressed by the emotional record
19
+ # towards another emotive record
20
+ #
21
+ # @example
22
+ # user = User.first
23
+ # picture = Picture.first
24
+ # user.express! :happy, picture
25
+ # user.emotions_about(picture)
26
+ # # => [:happy]
17
27
  def emotions_about(emotive)
18
28
  _emotions_about(emotive).pluck(:emotion).map(&:to_sym)
19
29
  end
20
30
 
31
+ # Express an emotion towards another record
32
+ #
33
+ # @example
34
+ # user = User.first
35
+ # picture = Picture.first
36
+ # user.express! :happy, picture
21
37
  def express!(emotion, emotive)
22
- send("#{emotion}_about!", emotive)
38
+ emotion = _emotions_about(emotive).where(emotion: emotion).first_or_initialize
39
+
40
+ begin
41
+ emotion.tap(&:save!)
42
+ rescue ActiveRecord::RecordInvalid => e
43
+ raise InvalidEmotion.new(e.record)
44
+ end
23
45
  end
24
46
 
47
+ # No longer express an emotion towards another record
48
+ #
49
+ # @example
50
+ # user = User.first
51
+ # picture = Picture.first
52
+ # user.no_longer_express! :happy, picture
25
53
  def no_longer_express!(emotion, emotive)
26
- send("no_longer_#{emotion}_about!", emotive)
54
+ _emotions_about(emotive).where(emotion: emotion).first.tap { |e| e.try(:destroy) }
27
55
  end
28
56
 
29
57
  module ClassMethods
58
+ # Return an `ActiveRecord::Relation` containing the emotional records
59
+ # that expressed a specific emotion towards an emotive record
60
+ #
61
+ # @example
62
+ # user = User.first
63
+ # picture = Picture.first
64
+ # user.express! :happy, picture
65
+ # User.emotional_about(:happy, picture)
66
+ # # => #<ActiveRecord::Relation [#<User id=1>]>
67
+ def emotional_about(emotion, emotive)
68
+ if emotive.class.emotive?
69
+ emotional_ids = emotive.emotions.where(emotion: emotion).where(emotional_type: self.name).pluck(:emotional_id)
70
+ where(id: emotional_ids)
71
+ else
72
+ # ActiveRecord 4 supports `.none`, not ActiveRecord 3
73
+ respond_to?(:none) ? none : where('1 = 0')
74
+ end
75
+ end
76
+
30
77
  def define_emotion_methods(emotion)
31
78
  class_eval <<-RUBY, __FILE__, __LINE__ + 1
32
79
  def #{emotion}_about?(emotive)
33
80
  !!#{emotion}_about(emotive).exists?
34
81
  end
35
- alias #{emotion}? #{emotion}_about?
36
- alias #{emotion}_with? #{emotion}_about?
37
- alias #{emotion}_over? #{emotion}_about?
38
82
 
39
83
  def #{emotion}_about!(emotive)
40
- emotion = #{emotion}_about(emotive).first_or_initialize
41
- emotion.tap(&:save!)
84
+ express! #{emotion.inspect}, emotive
42
85
  end
43
- alias #{emotion}_with! #{emotion}_about!
44
- alias #{emotion}_over! #{emotion}_about!
45
86
 
46
87
  def no_longer_#{emotion}_about!(emotive)
47
- #{emotion}_about(emotive).first.tap(&:destroy)
88
+ no_longer_express! #{emotion.inspect}, emotive
48
89
  end
49
- alias no_longer_#{emotion}_with! no_longer_#{emotion}_about!
50
- alias no_longer_#{emotion}_over! no_longer_#{emotion}_about!
51
90
 
52
91
  def #{emotion}_about(emotive)
53
92
  _emotions_about(emotive).where(emotion: #{emotion.to_s.inspect})
54
93
  end
94
+
95
+ alias #{emotion}? #{emotion}_about?
96
+ alias #{emotion}_with? #{emotion}_about?
97
+ alias #{emotion}_over? #{emotion}_about?
98
+
99
+ alias #{emotion}_with! #{emotion}_about!
100
+ alias #{emotion}_over! #{emotion}_about!
101
+
102
+ alias no_longer_#{emotion}_with! no_longer_#{emotion}_about!
103
+ alias no_longer_#{emotion}_over! no_longer_#{emotion}_about!
104
+
55
105
  alias #{emotion}_with #{emotion}_about
56
106
  alias #{emotion}_over #{emotion}_about
57
-
58
107
  RUBY
59
108
 
60
109
  instance_eval <<-RUBY, __FILE__, __LINE__ + 1
61
110
  def #{emotion}_about(emotive)
62
- emotional_ids = emotive.#{emotion}_about.where(emotional_type: self.name).pluck(:emotional_id)
63
- self.where(id: emotional_ids)
111
+ emotional_about(#{emotion.inspect}, emotive)
64
112
  end
113
+
65
114
  alias #{emotion}_with #{emotion}_about
66
115
  alias #{emotion}_over #{emotion}_about
67
116
  RUBY
@@ -10,14 +10,16 @@ module Emotions
10
10
  end
11
11
  end
12
12
 
13
+ # @private
13
14
  def update_emotion_counter(emotion)
14
- attribute = :"#{emotion}_emotions_count"
15
+ attribute = "#{emotion}_emotions_count"
15
16
 
16
17
  if self.respond_to?(attribute)
17
18
  self.update_attribute(attribute, self.send("#{emotion}_about").count)
18
19
  end
19
20
  end
20
21
 
22
+ # @private
21
23
  module ClassMethods
22
24
  def define_emotion_methods(emotion)
23
25
  class_eval <<-RUBY, __FILE__, __LINE__ + 1
@@ -0,0 +1,9 @@
1
+ module Emotions
2
+ class InvalidEmotion < StandardError
3
+ attr_reader :emotion
4
+
5
+ def initialize(emotion)
6
+ @emotion = emotion
7
+ end
8
+ end
9
+ end
@@ -4,23 +4,7 @@ require 'rails'
4
4
  module Emotions
5
5
  class Railtie < Rails::Railtie
6
6
  initializer 'emotions.active_record' do |app|
7
- ActiveSupport.on_load :active_record do
8
- def self.acts_as_emotive
9
- self.send :include, Emotions::Emotive
10
- end
11
-
12
- def self.acts_as_emotional
13
- self.send :include, Emotions::Emotional
14
- end
15
-
16
- def self.emotional?
17
- !!@emotional
18
- end
19
-
20
- def self.emotive?
21
- !!@emotive
22
- end
23
- end
7
+ ActiveSupport.on_load :active_record, {}, &Emotions.inject_into_active_record
24
8
  end
25
9
  end
26
10
  end
@@ -1,3 +1,3 @@
1
1
  module Emotions
2
- VERSION = '0.1.7'
2
+ VERSION = '0.2'
3
3
  end
@@ -53,21 +53,46 @@ describe Emotions::Emotional do
53
53
  context 'for user without emotions' do
54
54
  it { expect(user.emotions_about(picture)).to be_empty }
55
55
  end
56
+
57
+ context 'for invalid emotive' do
58
+ it { expect(user.emotions_about(user)).to be_empty }
59
+ it { expect{ user.emotions_about(user) }.to_not raise_error }
60
+ end
56
61
  end
57
62
 
58
63
  describe :express! do
59
64
  let(:picture) { Picture.create }
60
65
 
61
- it { expect{ user.express! :happy, picture }.to change{ Emotions::Emotion.count }.from(0).to(1) }
62
- it { expect{ user.express! :happy, picture }.to change{ user.happy_about? picture }.from(false).to(true) }
66
+ context 'with valid emotive and emotion' do
67
+ it { expect{ user.express! :happy, picture }.to change{ Emotions::Emotion.count }.from(0).to(1) }
68
+ it { expect{ user.express! :happy, picture }.to change{ user.happy_about? picture }.from(false).to(true) }
69
+ end
70
+
71
+ context 'with invalid emotive' do
72
+ it { expect{ user.express! :happy, user }.to raise_error(Emotions::InvalidEmotion) }
73
+ end
74
+
75
+ context 'with invalid emotion' do
76
+ it { expect{ user.express! :mad, picture }.to raise_error(Emotions::InvalidEmotion) }
77
+ end
63
78
  end
64
79
 
65
80
  describe :no_longer_express! do
66
81
  let(:picture) { Picture.create }
67
82
  before { user.happy_about!(picture) }
68
83
 
69
- it { expect{ user.no_longer_express! :happy, picture }.to change{ Emotions::Emotion.count }.from(1).to(0) }
70
- it { expect{ user.no_longer_express! :happy, picture }.to change{ user.happy_about? picture }.from(true).to(false) }
84
+ context 'with valid emotive and emotion' do
85
+ it { expect{ user.no_longer_express! :happy, picture }.to change{ Emotions::Emotion.count }.from(1).to(0) }
86
+ it { expect{ user.no_longer_express! :happy, picture }.to change{ user.happy_about? picture }.from(true).to(false) }
87
+ end
88
+
89
+ context 'with invalid emotive' do
90
+ it { expect{ user.no_longer_express! :happy, user }.to_not raise_error }
91
+ end
92
+
93
+ context 'with invalid emotion' do
94
+ it { expect{ user.no_longer_express! :mad, user }.to_not raise_error }
95
+ end
71
96
  end
72
97
 
73
98
  describe :DynamicMethods do
@@ -76,25 +101,44 @@ describe Emotions::Emotional do
76
101
  let(:picture) { Picture.create }
77
102
  let(:other_picture) { Picture.create }
78
103
 
79
- it { expect(user.happy_about? picture).to be_true }
80
- it { expect(user.happy_about? other_picture).to be_false }
104
+ context 'with valid emotive' do
105
+ it { expect(user.happy_about? picture).to be_true }
106
+ it { expect(user.happy_about? other_picture).to be_false }
107
+ end
108
+
109
+ context 'with invalid emotive' do
110
+ it { expect{ user.happy_about? user }.to_not raise_error }
111
+ end
81
112
  end
82
113
 
83
114
  describe :emotion_about! do
84
115
  let(:picture) { Picture.create }
85
116
 
86
- it { expect(user.happy_about! picture).to be_instance_of(Emotions::Emotion) }
87
- it { expect{ user.happy_about! picture }.to change{ Emotions::Emotion.count }.from(0).to(1) }
88
- it { expect{ user.happy_about! picture }.to change{ user.happy_about? picture }.from(false).to(true) }
117
+ context 'with valid emotive' do
118
+ it { expect(user.happy_about! picture).to be_instance_of(Emotions::Emotion) }
119
+ it { expect{ user.happy_about! picture }.to change{ Emotions::Emotion.count }.from(0).to(1) }
120
+ it { expect{ user.happy_about! picture }.to change{ user.happy_about? picture }.from(false).to(true) }
121
+ end
122
+
123
+ context 'with invalid emotive' do
124
+ it { expect{ user.happy_about! user }.to raise_error(Emotions::InvalidEmotion) }
125
+ end
89
126
  end
90
127
 
91
128
  describe :no_longer_emotion_about! do
92
129
  before { user.happy_about!(picture) }
93
130
  let(:picture) { Picture.create }
94
131
 
95
- it { expect(user.no_longer_happy_about! picture).to be_instance_of(Emotions::Emotion) }
96
- it { expect{ user.no_longer_happy_about! picture }.to change{ Emotions::Emotion.count }.from(1).to(0) }
97
- it { expect{ user.no_longer_happy_about! picture }.to change{ user.happy_about? picture }.from(true).to(false) }
132
+ context 'with valid emotive' do
133
+ it { expect(user.no_longer_happy_about! picture).to be_instance_of(Emotions::Emotion) }
134
+ it { expect{ user.no_longer_happy_about! picture }.to change{ Emotions::Emotion.count }.from(1).to(0) }
135
+ it { expect{ user.no_longer_happy_about! picture }.to change{ user.happy_about? picture }.from(true).to(false) }
136
+ end
137
+
138
+ context 'with invalid emotive' do
139
+ it { expect(user.no_longer_happy_about! user).to be_nil }
140
+ it { expect{ user.no_longer_happy_about! user }.to_not raise_error }
141
+ end
98
142
  end
99
143
  end
100
144
  end
@@ -119,8 +163,15 @@ describe Emotions::Emotional do
119
163
  first_other_user.happy_about! picture
120
164
  end
121
165
 
122
- it { expect(User.happy_about(picture).to_a).to eql [user, first_other_user] }
123
- it { expect(User.happy_about(picture).where('id != ?', first_other_user.id).to_a).to eql [user] }
166
+ context 'with valid emotive' do
167
+ it { expect(User.happy_about(picture).to_a).to eql [user, first_other_user] }
168
+ it { expect(User.happy_about(picture).where('id != ?', first_other_user.id).to_a).to eql [user] }
169
+ end
170
+
171
+ context 'with invalid emotive' do
172
+ it { expect(User.happy_about(user).to_a).to be_empty }
173
+ it { expect{ User.happy_about(user).to_a }.to_not raise_error }
174
+ end
124
175
  end
125
176
  end
126
177
  end
@@ -7,7 +7,9 @@ require 'emotions'
7
7
 
8
8
  # Require our macros and extensions
9
9
  Dir[File.expand_path('../../spec/support/macros/*.rb', __FILE__)].map(&method(:require))
10
- Dir[File.expand_path('../../spec/support/extensions/*.rb', __FILE__)].map(&method(:require))
10
+
11
+ # Inject our methods into ActiveRecord (like our railtie does)
12
+ ActiveRecord::Base.class_eval(&Emotions.inject_into_active_record)
11
13
 
12
14
  RSpec.configure do |config|
13
15
  # Include our macros
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.7
4
+ version: '0.2'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-06-24 00:00:00.000000000 Z
12
+ date: 2013-06-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -110,6 +110,7 @@ files:
110
110
  - lib/emotions/emotion.rb
111
111
  - lib/emotions/emotional.rb
112
112
  - lib/emotions/emotive.rb
113
+ - lib/emotions/errors.rb
113
114
  - lib/emotions/railtie.rb
114
115
  - lib/emotions/version.rb
115
116
  - lib/generators/emotions/USAGE
@@ -119,7 +120,6 @@ files:
119
120
  - spec/emotions/emotional_spec.rb
120
121
  - spec/emotions/emotive_spec.rb
121
122
  - spec/spec_helper.rb
122
- - spec/support/extensions/active_record.rb
123
123
  - spec/support/macros/database_macros.rb
124
124
  - spec/support/macros/model_macros.rb
125
125
  homepage: https://github.com/mirego/emotions
@@ -137,7 +137,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
137
137
  version: '0'
138
138
  segments:
139
139
  - 0
140
- hash: -17030051057476021
140
+ hash: -451133210429222225
141
141
  required_rubygems_version: !ruby/object:Gem::Requirement
142
142
  none: false
143
143
  requirements:
@@ -146,7 +146,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
146
146
  version: '0'
147
147
  segments:
148
148
  - 0
149
- hash: -17030051057476021
149
+ hash: -451133210429222225
150
150
  requirements: []
151
151
  rubyforge_project:
152
152
  rubygems_version: 1.8.23
@@ -159,6 +159,5 @@ test_files:
159
159
  - spec/emotions/emotional_spec.rb
160
160
  - spec/emotions/emotive_spec.rb
161
161
  - spec/spec_helper.rb
162
- - spec/support/extensions/active_record.rb
163
162
  - spec/support/macros/database_macros.rb
164
163
  - spec/support/macros/model_macros.rb
@@ -1,17 +0,0 @@
1
- class ActiveRecord::Base
2
- def self.acts_as_emotive
3
- self.send :include, Emotions::Emotive
4
- end
5
-
6
- def self.acts_as_emotional
7
- self.send :include, Emotions::Emotional
8
- end
9
-
10
- def self.emotive?
11
- @emotive ||= self.ancestors.include?(Emotions::Emotive)
12
- end
13
-
14
- def self.emotional?
15
- @emotional ||= self.ancestors.include?(Emotions::Emotional)
16
- end
17
- end