emotions 0.1.6 → 0.1.7
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/.rspec +1 -0
- data/README.md +2 -2
- data/Rakefile +11 -1
- data/emotions.gemspec +10 -8
- data/lib/emotions.rb +6 -4
- data/lib/emotions/emotion.rb +16 -1
- data/lib/emotions/emotional.rb +23 -8
- data/lib/emotions/emotive.rb +3 -5
- data/lib/emotions/railtie.rb +9 -1
- data/lib/emotions/version.rb +1 -1
- data/lib/generators/emotions/install_generator.rb +2 -2
- data/lib/generators/emotions/templates/migration.rb +1 -1
- data/spec/emotions/emotion_spec.rb +87 -0
- data/spec/emotions/emotional_spec.rb +127 -0
- data/spec/emotions/emotive_spec.rb +35 -0
- data/spec/spec_helper.rb +29 -0
- data/spec/support/extensions/active_record.rb +17 -0
- data/spec/support/macros/database_macros.rb +39 -0
- data/spec/support/macros/model_macros.rb +31 -0
- metadata +58 -11
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--colour
|
data/README.md
CHANGED
@@ -62,8 +62,8 @@ user.no_longer_sad_about!(picture)
|
|
62
62
|
user.sad_about?(picture)
|
63
63
|
# => false
|
64
64
|
|
65
|
-
|
66
|
-
# => [#<User id=1>]
|
65
|
+
User.happy_about(picture)
|
66
|
+
# => #<ActiveRecord::Relation [#<User id=1>]>
|
67
67
|
|
68
68
|
user.express!(:sad, picture)
|
69
69
|
user.sad_about?(picure)
|
data/Rakefile
CHANGED
@@ -1 +1,11 @@
|
|
1
|
-
require
|
1
|
+
require 'bundler'
|
2
|
+
require 'rake'
|
3
|
+
require 'bundler/gem_tasks'
|
4
|
+
require 'rspec/core/rake_task'
|
5
|
+
|
6
|
+
task default: :spec
|
7
|
+
|
8
|
+
desc 'Run all specs'
|
9
|
+
RSpec::Core::RakeTask.new(:spec) do |task|
|
10
|
+
task.pattern = 'spec/**/*_spec.rb'
|
11
|
+
end
|
data/emotions.gemspec
CHANGED
@@ -4,22 +4,24 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
require 'emotions/version'
|
5
5
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
|
-
spec.name =
|
7
|
+
spec.name = 'emotions'
|
8
8
|
spec.version = Emotions::VERSION
|
9
|
-
spec.authors = [
|
10
|
-
spec.email = [
|
9
|
+
spec.authors = ['Rémi Prévost']
|
10
|
+
spec.email = ['rprevost@mirego.com']
|
11
11
|
spec.description = 'Emotions is a Ruby library that allows ActiveRecord records to express (and hopefully store) emotions about other records.'
|
12
12
|
spec.summary = 'Emotions is a Ruby library that allows ActiveRecord records to express (and hopefully store) emotions about other records.'
|
13
13
|
spec.homepage = 'https://github.com/mirego/emotions'
|
14
|
-
spec.license =
|
14
|
+
spec.license = 'BSD 3-Clause'
|
15
15
|
|
16
16
|
spec.files = `git ls-files`.split($/)
|
17
17
|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
-
spec.require_paths = [
|
19
|
+
spec.require_paths = ['lib']
|
20
20
|
|
21
|
-
spec.add_dependency
|
21
|
+
spec.add_dependency 'rails', '>= 3.0.0'
|
22
22
|
|
23
|
-
spec.add_development_dependency
|
24
|
-
spec.add_development_dependency
|
23
|
+
spec.add_development_dependency 'rake'
|
24
|
+
spec.add_development_dependency 'bundler', '~> 1.3'
|
25
|
+
spec.add_development_dependency 'rspec', '~> 2.13'
|
26
|
+
spec.add_development_dependency 'sqlite3', '~> 1.3'
|
25
27
|
end
|
data/lib/emotions.rb
CHANGED
@@ -1,7 +1,9 @@
|
|
1
|
-
require
|
2
|
-
|
3
|
-
require
|
4
|
-
require
|
1
|
+
require 'emotions/version'
|
2
|
+
|
3
|
+
require 'active_record'
|
4
|
+
require 'emotions/emotion'
|
5
|
+
require 'emotions/emotive'
|
6
|
+
require 'emotions/emotional'
|
5
7
|
|
6
8
|
module Emotions
|
7
9
|
def self.configure
|
data/lib/emotions/emotion.rb
CHANGED
@@ -5,8 +5,23 @@ module Emotions
|
|
5
5
|
# Validations
|
6
6
|
validates :emotional, presence: true
|
7
7
|
validates :emotive, presence: true
|
8
|
+
|
8
9
|
validates_each :emotion do |record, attr, value|
|
9
|
-
|
10
|
+
unless Emotions.emotions.include?(value.try(:to_sym))
|
11
|
+
record.errors.add attr, I18n.t(:invalid, scope: [:errors, :messages])
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
validates_each :emotional do |record, attr, value|
|
16
|
+
if value.blank? || !value.class.try(:emotional?)
|
17
|
+
record.errors.add attr, I18n.t(:invalid, scope: [:errors, :messages])
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
validates_each :emotive do |record, attr, value|
|
22
|
+
if value.blank? || !value.class.try(:emotive?)
|
23
|
+
record.errors.add attr, I18n.t(:invalid, scope: [:errors, :messages])
|
24
|
+
end
|
10
25
|
end
|
11
26
|
|
12
27
|
# Associations
|
data/lib/emotions/emotional.rb
CHANGED
@@ -15,7 +15,7 @@ module Emotions
|
|
15
15
|
end
|
16
16
|
|
17
17
|
def emotions_about(emotive)
|
18
|
-
_emotions_about(emotive).
|
18
|
+
_emotions_about(emotive).pluck(:emotion).map(&:to_sym)
|
19
19
|
end
|
20
20
|
|
21
21
|
def express!(emotion, emotive)
|
@@ -30,25 +30,40 @@ module Emotions
|
|
30
30
|
def define_emotion_methods(emotion)
|
31
31
|
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
32
32
|
def #{emotion}_about?(emotive)
|
33
|
-
|
34
|
-
end
|
35
|
-
|
36
|
-
def #{emotion}_about?(emotive)
|
37
|
-
#{emotion}_about(emotive).exists?
|
33
|
+
!!#{emotion}_about(emotive).exists?
|
38
34
|
end
|
35
|
+
alias #{emotion}? #{emotion}_about?
|
36
|
+
alias #{emotion}_with? #{emotion}_about?
|
37
|
+
alias #{emotion}_over? #{emotion}_about?
|
39
38
|
|
40
39
|
def #{emotion}_about!(emotive)
|
41
40
|
emotion = #{emotion}_about(emotive).first_or_initialize
|
42
|
-
emotion.save!
|
41
|
+
emotion.tap(&:save!)
|
43
42
|
end
|
43
|
+
alias #{emotion}_with! #{emotion}_about!
|
44
|
+
alias #{emotion}_over! #{emotion}_about!
|
44
45
|
|
45
46
|
def no_longer_#{emotion}_about!(emotive)
|
46
|
-
#{emotion}_about(emotive).
|
47
|
+
#{emotion}_about(emotive).first.tap(&:destroy)
|
47
48
|
end
|
49
|
+
alias no_longer_#{emotion}_with! no_longer_#{emotion}_about!
|
50
|
+
alias no_longer_#{emotion}_over! no_longer_#{emotion}_about!
|
48
51
|
|
49
52
|
def #{emotion}_about(emotive)
|
50
53
|
_emotions_about(emotive).where(emotion: #{emotion.to_s.inspect})
|
51
54
|
end
|
55
|
+
alias #{emotion}_with #{emotion}_about
|
56
|
+
alias #{emotion}_over #{emotion}_about
|
57
|
+
|
58
|
+
RUBY
|
59
|
+
|
60
|
+
instance_eval <<-RUBY, __FILE__, __LINE__ + 1
|
61
|
+
def #{emotion}_about(emotive)
|
62
|
+
emotional_ids = emotive.#{emotion}_about.where(emotional_type: self.name).pluck(:emotional_id)
|
63
|
+
self.where(id: emotional_ids)
|
64
|
+
end
|
65
|
+
alias #{emotion}_with #{emotion}_about
|
66
|
+
alias #{emotion}_over #{emotion}_about
|
52
67
|
RUBY
|
53
68
|
end
|
54
69
|
end
|
data/lib/emotions/emotive.rb
CHANGED
@@ -10,10 +10,6 @@ module Emotions
|
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
13
|
-
def emotional_about
|
14
|
-
self.emotions.includes(:emotional)
|
15
|
-
end
|
16
|
-
|
17
13
|
def update_emotion_counter(emotion)
|
18
14
|
attribute = :"#{emotion}_emotions_count"
|
19
15
|
|
@@ -26,8 +22,10 @@ module Emotions
|
|
26
22
|
def define_emotion_methods(emotion)
|
27
23
|
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
28
24
|
def #{emotion}_about
|
29
|
-
|
25
|
+
self.emotions.where(emotion: #{emotion.to_s.inspect})
|
30
26
|
end
|
27
|
+
alias #{emotion}_with #{emotion}_about
|
28
|
+
alias #{emotion}_over #{emotion}_about
|
31
29
|
RUBY
|
32
30
|
end
|
33
31
|
end
|
data/lib/emotions/railtie.rb
CHANGED
@@ -3,7 +3,7 @@ require 'rails'
|
|
3
3
|
|
4
4
|
module Emotions
|
5
5
|
class Railtie < Rails::Railtie
|
6
|
-
initializer
|
6
|
+
initializer 'emotions.active_record' do |app|
|
7
7
|
ActiveSupport.on_load :active_record do
|
8
8
|
def self.acts_as_emotive
|
9
9
|
self.send :include, Emotions::Emotive
|
@@ -12,6 +12,14 @@ module Emotions
|
|
12
12
|
def self.acts_as_emotional
|
13
13
|
self.send :include, Emotions::Emotional
|
14
14
|
end
|
15
|
+
|
16
|
+
def self.emotional?
|
17
|
+
!!@emotional
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.emotive?
|
21
|
+
!!@emotive
|
22
|
+
end
|
15
23
|
end
|
16
24
|
end
|
17
25
|
end
|
data/lib/emotions/version.rb
CHANGED
@@ -11,9 +11,9 @@ module Emotions
|
|
11
11
|
# taken from http://github.com/rails/rails/blob/master/activerecord/lib/generators/active_record.rb
|
12
12
|
def self.next_migration_number(dirname)
|
13
13
|
if ActiveRecord::Base.timestamped_migrations
|
14
|
-
Time.now.utc.strftime(
|
14
|
+
Time.now.utc.strftime('%Y%m%d%H%M%S')
|
15
15
|
else
|
16
|
-
|
16
|
+
'%.3d' % (current_migration_number(dirname) + 1)
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
@@ -15,7 +15,7 @@ class AddEmotions < ActiveRecord::Migration
|
|
15
15
|
|
16
16
|
add_index :emotions, [:emotional_type, :emotional_id, :emotive_type, :emotive_id, :emotion], unique: true, name: 'index_emotions_by_emotional_emotive_and_emotion'
|
17
17
|
add_index :emotions, [:emotional_type, :emotional_id, :emotive_type, :emotive_id], name: 'index_emotions_by_emotional_and_emotive'
|
18
|
-
add_index :emotions, [:emotive_type, :emotive_id, :emotion], name: '
|
18
|
+
add_index :emotions, [:emotive_type, :emotive_id, :emotion], name: 'index_emotions_by_emotive'
|
19
19
|
end
|
20
20
|
|
21
21
|
def down
|
@@ -0,0 +1,87 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Emotions::Emotion do
|
4
|
+
before do
|
5
|
+
emotions :happy, :sad
|
6
|
+
|
7
|
+
run_migration do
|
8
|
+
create_table(:users, force: true)
|
9
|
+
create_table(:pictures, force: true) do |t|
|
10
|
+
t.integer :happy_emotions_count, default: 0
|
11
|
+
t.integer :sad_emotions_count, default: 0
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
emotional 'User'
|
16
|
+
emotive 'Picture'
|
17
|
+
end
|
18
|
+
|
19
|
+
describe :Validations do
|
20
|
+
describe :validate_presence_of_emotional do
|
21
|
+
subject { described_class.new(emotion: 'happy', emotive: Picture.create) }
|
22
|
+
before { subject.valid? }
|
23
|
+
|
24
|
+
it { should_not be_valid }
|
25
|
+
it { expect(subject.errors.full_messages).to eql ["Emotional can't be blank", "Emotional is invalid"] }
|
26
|
+
end
|
27
|
+
|
28
|
+
describe :validate_presence_of_emotive do
|
29
|
+
subject { described_class.new(emotion: 'happy', emotional: User.create) }
|
30
|
+
before { subject.valid? }
|
31
|
+
|
32
|
+
it { should_not be_valid }
|
33
|
+
it { expect(subject.errors.full_messages).to eql ["Emotive can't be blank", "Emotive is invalid"] }
|
34
|
+
end
|
35
|
+
|
36
|
+
describe :validate_inclusion_of_emotion do
|
37
|
+
subject { described_class.new(emotion: 'mad', emotional: User.create, emotive: Picture.create) }
|
38
|
+
before { subject.valid? }
|
39
|
+
|
40
|
+
it { should_not be_valid }
|
41
|
+
it { expect(subject.errors.full_messages).to eql ["Emotion is invalid"] }
|
42
|
+
end
|
43
|
+
|
44
|
+
describe :validate_class_of_emotive do
|
45
|
+
subject { described_class.new(emotion: 'happy', emotional: User.create, emotive: User.create) }
|
46
|
+
before { subject.valid? }
|
47
|
+
|
48
|
+
it { should_not be_valid }
|
49
|
+
it { expect(subject.errors.full_messages).to eql ["Emotive is invalid"] }
|
50
|
+
end
|
51
|
+
|
52
|
+
describe :validate_class_of_emotional do
|
53
|
+
subject { described_class.new(emotion: 'happy', emotional: Picture.create, emotive: Picture.create) }
|
54
|
+
before { subject.valid? }
|
55
|
+
|
56
|
+
it { should_not be_valid }
|
57
|
+
it { expect(subject.errors.full_messages).to eql ["Emotional is invalid"] }
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
describe :Callbacks do
|
62
|
+
describe :update_emotion_counter_on_create do
|
63
|
+
let(:picture) { Picture.create }
|
64
|
+
let(:emotion) { described_class.new(emotion: 'happy', emotional: User.create, emotive: picture) }
|
65
|
+
|
66
|
+
before do
|
67
|
+
picture.should_receive(:update_emotion_counter).with('happy').once
|
68
|
+
emotion.should_receive(:update_emotion_counter).once.and_call_original
|
69
|
+
end
|
70
|
+
|
71
|
+
it { emotion.save! }
|
72
|
+
end
|
73
|
+
|
74
|
+
describe :update_emotion_counter_on_destroy do
|
75
|
+
let(:picture) { Picture.create }
|
76
|
+
let(:emotion) { described_class.new(emotion: 'happy', emotional: User.create, emotive: picture) }
|
77
|
+
|
78
|
+
before do
|
79
|
+
emotion.save!
|
80
|
+
picture.should_receive(:update_emotion_counter).with('happy').once
|
81
|
+
emotion.should_receive(:update_emotion_counter).once.and_call_original
|
82
|
+
end
|
83
|
+
|
84
|
+
it { emotion.destroy }
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
@@ -0,0 +1,127 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Emotions::Emotional do
|
4
|
+
before do
|
5
|
+
emotions :happy, :sad
|
6
|
+
|
7
|
+
run_migration do
|
8
|
+
create_table(:users, force: true)
|
9
|
+
create_table(:pictures, force: true)
|
10
|
+
end
|
11
|
+
|
12
|
+
emotional 'User'
|
13
|
+
emotive 'Picture'
|
14
|
+
end
|
15
|
+
|
16
|
+
let(:user) { User.create }
|
17
|
+
|
18
|
+
describe :InstanceMethods do
|
19
|
+
describe :Aliases do
|
20
|
+
subject { user }
|
21
|
+
|
22
|
+
it { should respond_to :happy? }
|
23
|
+
|
24
|
+
it { should respond_to :happy_about? }
|
25
|
+
it { should respond_to :happy_with? }
|
26
|
+
it { should respond_to :happy_over? }
|
27
|
+
|
28
|
+
it { should respond_to :happy_about! }
|
29
|
+
it { should respond_to :happy_with! }
|
30
|
+
it { should respond_to :happy_over! }
|
31
|
+
|
32
|
+
it { should respond_to :no_longer_happy_about! }
|
33
|
+
it { should respond_to :no_longer_happy_with! }
|
34
|
+
it { should respond_to :no_longer_happy_over! }
|
35
|
+
|
36
|
+
it { should respond_to :happy_about }
|
37
|
+
it { should respond_to :happy_with }
|
38
|
+
it { should respond_to :happy_over }
|
39
|
+
end
|
40
|
+
|
41
|
+
describe :emotions_about do
|
42
|
+
let(:picture) { Picture.create }
|
43
|
+
|
44
|
+
context 'for user with emotions' do
|
45
|
+
before do
|
46
|
+
user.happy_about! picture
|
47
|
+
user.sad_about! picture
|
48
|
+
end
|
49
|
+
|
50
|
+
it { expect(user.emotions_about(picture)).to eql [:happy, :sad] }
|
51
|
+
end
|
52
|
+
|
53
|
+
context 'for user without emotions' do
|
54
|
+
it { expect(user.emotions_about(picture)).to be_empty }
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
describe :express! do
|
59
|
+
let(:picture) { Picture.create }
|
60
|
+
|
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) }
|
63
|
+
end
|
64
|
+
|
65
|
+
describe :no_longer_express! do
|
66
|
+
let(:picture) { Picture.create }
|
67
|
+
before { user.happy_about!(picture) }
|
68
|
+
|
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) }
|
71
|
+
end
|
72
|
+
|
73
|
+
describe :DynamicMethods do
|
74
|
+
describe :emotion_about? do
|
75
|
+
before { user.happy_about!(picture) }
|
76
|
+
let(:picture) { Picture.create }
|
77
|
+
let(:other_picture) { Picture.create }
|
78
|
+
|
79
|
+
it { expect(user.happy_about? picture).to be_true }
|
80
|
+
it { expect(user.happy_about? other_picture).to be_false }
|
81
|
+
end
|
82
|
+
|
83
|
+
describe :emotion_about! do
|
84
|
+
let(:picture) { Picture.create }
|
85
|
+
|
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) }
|
89
|
+
end
|
90
|
+
|
91
|
+
describe :no_longer_emotion_about! do
|
92
|
+
before { user.happy_about!(picture) }
|
93
|
+
let(:picture) { Picture.create }
|
94
|
+
|
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) }
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
describe :ClassMethods do
|
103
|
+
describe :Aliases do
|
104
|
+
subject { user.class }
|
105
|
+
|
106
|
+
it { should respond_to :happy_about }
|
107
|
+
it { should respond_to :happy_with }
|
108
|
+
it { should respond_to :happy_over }
|
109
|
+
end
|
110
|
+
|
111
|
+
describe :DynamicMethods do
|
112
|
+
describe :emotion_about do
|
113
|
+
let(:first_other_user) { User.create }
|
114
|
+
let(:second_other_user) { User.create }
|
115
|
+
let(:picture) { Picture.create }
|
116
|
+
|
117
|
+
before do
|
118
|
+
user.happy_about! picture
|
119
|
+
first_other_user.happy_about! picture
|
120
|
+
end
|
121
|
+
|
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] }
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Emotions::Emotive do
|
4
|
+
before do
|
5
|
+
emotions :happy, :sad
|
6
|
+
|
7
|
+
run_migration do
|
8
|
+
create_table(:users, force: true)
|
9
|
+
create_table(:pictures, force: true) do |t|
|
10
|
+
t.integer :happy_emotions_count, default: 0
|
11
|
+
t.integer :sad_emotions_count, default: 0
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
emotional 'User'
|
16
|
+
emotive 'Picture'
|
17
|
+
end
|
18
|
+
|
19
|
+
let(:picture) { Picture.create }
|
20
|
+
|
21
|
+
describe :InstanceMethods do
|
22
|
+
describe :update_emotion_counter do
|
23
|
+
let(:relation) do
|
24
|
+
double.tap { |double| double.stub(:count).and_return(42) }
|
25
|
+
end
|
26
|
+
|
27
|
+
before do
|
28
|
+
Picture.any_instance.stub(:happy_about).and_return(relation)
|
29
|
+
picture.update_emotion_counter(:happy)
|
30
|
+
end
|
31
|
+
|
32
|
+
it { expect(picture.reload.happy_emotions_count).to eql 42 }
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
$:.unshift File.expand_path('../../lib', __FILE__)
|
2
|
+
|
3
|
+
require 'rspec'
|
4
|
+
require 'sqlite3'
|
5
|
+
|
6
|
+
require 'emotions'
|
7
|
+
|
8
|
+
# Require our macros and extensions
|
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))
|
11
|
+
|
12
|
+
RSpec.configure do |config|
|
13
|
+
# Include our macros
|
14
|
+
config.include DatabaseMacros
|
15
|
+
config.include ModelMacros
|
16
|
+
|
17
|
+
config.before(:each) do
|
18
|
+
# Create the SQLite database
|
19
|
+
setup_database
|
20
|
+
|
21
|
+
# Run our migration
|
22
|
+
run_default_migration
|
23
|
+
end
|
24
|
+
|
25
|
+
config.after(:each) do
|
26
|
+
# Make sure we remove our test database file
|
27
|
+
cleanup_database
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,17 @@
|
|
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
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module DatabaseMacros
|
2
|
+
# Run migrations in the test database
|
3
|
+
def run_migration(&block)
|
4
|
+
# Create a new migration class
|
5
|
+
klass = Class.new(ActiveRecord::Migration)
|
6
|
+
|
7
|
+
# Create a new `up` that executes the argument
|
8
|
+
klass.send(:define_method, :up) { self.instance_exec(&block) }
|
9
|
+
|
10
|
+
# Create a new instance of it and execute its `up` method
|
11
|
+
klass.new.up
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.database_file
|
15
|
+
@database_file || File.expand_path('../test.db', __FILE__)
|
16
|
+
end
|
17
|
+
|
18
|
+
def setup_database
|
19
|
+
# Make sure the test database file is gone
|
20
|
+
cleanup_database
|
21
|
+
|
22
|
+
# Establish the connection
|
23
|
+
SQLite3::Database.new FileUtils.touch(DatabaseMacros.database_file).first
|
24
|
+
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: DatabaseMacros.database_file)
|
25
|
+
|
26
|
+
# Silence everything
|
27
|
+
ActiveRecord::Base.logger = ActiveRecord::Migration.verbose = false
|
28
|
+
end
|
29
|
+
|
30
|
+
def cleanup_database
|
31
|
+
FileUtils.rm(DatabaseMacros.database_file) if File.exists?(DatabaseMacros.database_file)
|
32
|
+
end
|
33
|
+
|
34
|
+
# Run the built-in migration
|
35
|
+
def run_default_migration
|
36
|
+
load File.expand_path('../../../../lib/generators/emotions/templates/migration.rb', __FILE__)
|
37
|
+
AddEmotions.new.up
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module ModelMacros
|
2
|
+
# Create a new emotional model
|
3
|
+
def emotional(klass_name, &block)
|
4
|
+
spawn_model klass_name, ActiveRecord::Base do
|
5
|
+
acts_as_emotional
|
6
|
+
instance_exec(&block) if block
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
# Create a new emotive model
|
11
|
+
def emotive(klass_name, &block)
|
12
|
+
spawn_model klass_name, ActiveRecord::Base do
|
13
|
+
acts_as_emotive
|
14
|
+
class_eval(&block) if block
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
# Configure the emotions managed by Emotions
|
19
|
+
def emotions(*emotions)
|
20
|
+
Emotions.configure { |config| config.emotions = emotions }
|
21
|
+
end
|
22
|
+
|
23
|
+
protected
|
24
|
+
|
25
|
+
# Create a new model class
|
26
|
+
def spawn_model(klass_name, parent_klass, &block)
|
27
|
+
Object.instance_eval { remove_const klass_name } if Object.const_defined?(klass_name)
|
28
|
+
Object.const_set(klass_name, Class.new(parent_klass))
|
29
|
+
Object.const_get(klass_name).class_eval(&block) if block_given?
|
30
|
+
end
|
31
|
+
end
|
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.7
|
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-
|
12
|
+
date: 2013-06-24 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
@@ -27,6 +27,22 @@ dependencies:
|
|
27
27
|
- - ! '>='
|
28
28
|
- !ruby/object:Gem::Version
|
29
29
|
version: 3.0.0
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
30
46
|
- !ruby/object:Gem::Dependency
|
31
47
|
name: bundler
|
32
48
|
requirement: !ruby/object:Gem::Requirement
|
@@ -44,21 +60,37 @@ dependencies:
|
|
44
60
|
- !ruby/object:Gem::Version
|
45
61
|
version: '1.3'
|
46
62
|
- !ruby/object:Gem::Dependency
|
47
|
-
name:
|
63
|
+
name: rspec
|
48
64
|
requirement: !ruby/object:Gem::Requirement
|
49
65
|
none: false
|
50
66
|
requirements:
|
51
|
-
- -
|
67
|
+
- - ~>
|
52
68
|
- !ruby/object:Gem::Version
|
53
|
-
version: '
|
69
|
+
version: '2.13'
|
54
70
|
type: :development
|
55
71
|
prerelease: false
|
56
72
|
version_requirements: !ruby/object:Gem::Requirement
|
57
73
|
none: false
|
58
74
|
requirements:
|
59
|
-
- -
|
75
|
+
- - ~>
|
60
76
|
- !ruby/object:Gem::Version
|
61
|
-
version: '
|
77
|
+
version: '2.13'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: sqlite3
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ~>
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '1.3'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ~>
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '1.3'
|
62
94
|
description: Emotions is a Ruby library that allows ActiveRecord records to express
|
63
95
|
(and hopefully store) emotions about other records.
|
64
96
|
email:
|
@@ -68,6 +100,7 @@ extensions: []
|
|
68
100
|
extra_rdoc_files: []
|
69
101
|
files:
|
70
102
|
- .gitignore
|
103
|
+
- .rspec
|
71
104
|
- Gemfile
|
72
105
|
- LICENSE.md
|
73
106
|
- README.md
|
@@ -82,9 +115,16 @@ files:
|
|
82
115
|
- lib/generators/emotions/USAGE
|
83
116
|
- lib/generators/emotions/install_generator.rb
|
84
117
|
- lib/generators/emotions/templates/migration.rb
|
118
|
+
- spec/emotions/emotion_spec.rb
|
119
|
+
- spec/emotions/emotional_spec.rb
|
120
|
+
- spec/emotions/emotive_spec.rb
|
121
|
+
- spec/spec_helper.rb
|
122
|
+
- spec/support/extensions/active_record.rb
|
123
|
+
- spec/support/macros/database_macros.rb
|
124
|
+
- spec/support/macros/model_macros.rb
|
85
125
|
homepage: https://github.com/mirego/emotions
|
86
126
|
licenses:
|
87
|
-
-
|
127
|
+
- BSD 3-Clause
|
88
128
|
post_install_message:
|
89
129
|
rdoc_options: []
|
90
130
|
require_paths:
|
@@ -97,7 +137,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
97
137
|
version: '0'
|
98
138
|
segments:
|
99
139
|
- 0
|
100
|
-
hash: -
|
140
|
+
hash: -17030051057476021
|
101
141
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
142
|
none: false
|
103
143
|
requirements:
|
@@ -106,7 +146,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
106
146
|
version: '0'
|
107
147
|
segments:
|
108
148
|
- 0
|
109
|
-
hash: -
|
149
|
+
hash: -17030051057476021
|
110
150
|
requirements: []
|
111
151
|
rubyforge_project:
|
112
152
|
rubygems_version: 1.8.23
|
@@ -114,4 +154,11 @@ signing_key:
|
|
114
154
|
specification_version: 3
|
115
155
|
summary: Emotions is a Ruby library that allows ActiveRecord records to express (and
|
116
156
|
hopefully store) emotions about other records.
|
117
|
-
test_files:
|
157
|
+
test_files:
|
158
|
+
- spec/emotions/emotion_spec.rb
|
159
|
+
- spec/emotions/emotional_spec.rb
|
160
|
+
- spec/emotions/emotive_spec.rb
|
161
|
+
- spec/spec_helper.rb
|
162
|
+
- spec/support/extensions/active_record.rb
|
163
|
+
- spec/support/macros/database_macros.rb
|
164
|
+
- spec/support/macros/model_macros.rb
|