like_system 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.empty +0 -0
- data/.gitignore +15 -0
- data/.keep +0 -0
- data/.rspec +2 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +23 -0
- data/Appraisals +7 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +93 -0
- data/Rakefile +8 -0
- data/gemfiles/.empty +0 -0
- data/gemfiles/.gitignore +0 -0
- data/gemfiles/.keep +0 -0
- data/gemfiles/rails4.1.gemfile +7 -0
- data/gemfiles/rails4.2.gemfile +7 -0
- data/lib/.empty +0 -0
- data/lib/.gitignore +0 -0
- data/lib/.keep +0 -0
- data/lib/generators/.empty +0 -0
- data/lib/generators/.gitignore +0 -0
- data/lib/generators/.keep +0 -0
- data/lib/generators/like_system/.empty +0 -0
- data/lib/generators/like_system/.gitignore +0 -0
- data/lib/generators/like_system/.keep +0 -0
- data/lib/generators/like_system/like_system_generator.rb +46 -0
- data/lib/generators/like_system/templates/.empty +0 -0
- data/lib/generators/like_system/templates/.gitignore +0 -0
- data/lib/generators/like_system/templates/.keep +0 -0
- data/lib/generators/like_system/templates/migration.rb +47 -0
- data/lib/like_system.rb +45 -0
- data/lib/like_system/.empty +0 -0
- data/lib/like_system/.gitignore +0 -0
- data/lib/like_system/.keep +0 -0
- data/lib/like_system/like.rb +155 -0
- data/lib/like_system/likee.rb +58 -0
- data/lib/like_system/liker.rb +88 -0
- data/lib/like_system/version.rb +12 -0
- data/like_system.gemspec +31 -0
- data/spec/.empty +0 -0
- data/spec/.gitignore +0 -0
- data/spec/.keep +0 -0
- data/spec/db/.empty +0 -0
- data/spec/db/.gitignore +0 -0
- data/spec/db/.keep +0 -0
- data/spec/db/migrate/.empty +0 -0
- data/spec/db/migrate/.gitignore +0 -0
- data/spec/db/migrate/.keep +0 -0
- data/spec/db/migrate/20140926000000_create_likes.rb +47 -0
- data/spec/db/migrate/20140926000005_create_dummy_likers.rb +22 -0
- data/spec/db/migrate/20140926000010_create_dummy_likees.rb +22 -0
- data/spec/like_system/.empty +0 -0
- data/spec/like_system/.gitignore +0 -0
- data/spec/like_system/.keep +0 -0
- data/spec/like_system/like_spec.rb +177 -0
- data/spec/like_system/likee_spec.rb +69 -0
- data/spec/like_system/liker_spec.rb +96 -0
- data/spec/spec_helper.rb +116 -0
- data/spec/support/.empty +0 -0
- data/spec/support/.gitignore +0 -0
- data/spec/support/.keep +0 -0
- data/spec/support/active_record.rb +12 -0
- data/spec/support/shoulda_matchers.rb +2 -0
- metadata +237 -0
@@ -0,0 +1,58 @@
|
|
1
|
+
###
|
2
|
+
# LikeSystem module
|
3
|
+
#
|
4
|
+
# This module defines common behavior in like system
|
5
|
+
###
|
6
|
+
module LikeSystem
|
7
|
+
###
|
8
|
+
# Likee module
|
9
|
+
#
|
10
|
+
# This module defines likee behavior in like system
|
11
|
+
###
|
12
|
+
module Likee
|
13
|
+
###
|
14
|
+
# Extends ActiveSupport::Concern
|
15
|
+
###
|
16
|
+
extend ActiveSupport::Concern
|
17
|
+
|
18
|
+
###
|
19
|
+
# Included configuration
|
20
|
+
###
|
21
|
+
included do
|
22
|
+
###
|
23
|
+
# Has many likers association configuration
|
24
|
+
###
|
25
|
+
has_many :likers, class_name: "LikeSystem::Like", as: :likee, dependent: :destroy
|
26
|
+
end
|
27
|
+
|
28
|
+
###
|
29
|
+
# Specifies if self can be liked by {Liker} objects
|
30
|
+
#
|
31
|
+
# @return [Boolean]
|
32
|
+
###
|
33
|
+
def is_likee?
|
34
|
+
true
|
35
|
+
end
|
36
|
+
|
37
|
+
###
|
38
|
+
# Specifies if self is liked by a {Liker} object
|
39
|
+
#
|
40
|
+
# @param [Liker] liker - the {Liker} object to test against
|
41
|
+
# @return [Boolean]
|
42
|
+
###
|
43
|
+
def liked_by?(liker)
|
44
|
+
Like.likes?(liker, self)
|
45
|
+
end
|
46
|
+
|
47
|
+
###
|
48
|
+
# Retrieves a scope of {Like} objects that likes self filtered {Liker} type
|
49
|
+
#
|
50
|
+
# @param [Class] klass - the {Class} to filter
|
51
|
+
# @return [ActiveRecord::Relation]
|
52
|
+
###
|
53
|
+
def likers_by(klass)
|
54
|
+
Like.scope_by_likee(self).scope_by_liker_type(klass)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
@@ -0,0 +1,88 @@
|
|
1
|
+
###
|
2
|
+
# LikeSystem module
|
3
|
+
#
|
4
|
+
# This module defines common behavior in like system
|
5
|
+
###
|
6
|
+
module LikeSystem
|
7
|
+
###
|
8
|
+
# Liker module
|
9
|
+
#
|
10
|
+
# This module defines liker behavior in like system
|
11
|
+
###
|
12
|
+
module Liker
|
13
|
+
###
|
14
|
+
# Extends ActiveSupport::Concern
|
15
|
+
###
|
16
|
+
extend ActiveSupport::Concern
|
17
|
+
|
18
|
+
###
|
19
|
+
# Included configuration
|
20
|
+
###
|
21
|
+
included do
|
22
|
+
###
|
23
|
+
# Has many likees association configuration
|
24
|
+
###
|
25
|
+
has_many :likees, class_name: "LikeSystem::Like", as: :liker, dependent: :destroy
|
26
|
+
end
|
27
|
+
|
28
|
+
###
|
29
|
+
# Specifies if self can like {Likee} objects
|
30
|
+
#
|
31
|
+
# @return [Boolean]
|
32
|
+
###
|
33
|
+
def is_liker?
|
34
|
+
true
|
35
|
+
end
|
36
|
+
|
37
|
+
###
|
38
|
+
# Creates a {Like} relationship between self and a {Likee} object
|
39
|
+
#
|
40
|
+
# @param [Likee] likee - the likee of the {Like} relationship
|
41
|
+
# @return [Boolean]
|
42
|
+
###
|
43
|
+
def like(likee)
|
44
|
+
Like.like(self, likee)
|
45
|
+
end
|
46
|
+
|
47
|
+
###
|
48
|
+
# Destroys a {Like} relationship between self and a {Likee} object
|
49
|
+
#
|
50
|
+
# @param [Likee] likee - the likee of the {Like} relationship
|
51
|
+
# @return [Boolean]
|
52
|
+
###
|
53
|
+
def unlike(likee)
|
54
|
+
Like.unlike(self, likee)
|
55
|
+
end
|
56
|
+
|
57
|
+
###
|
58
|
+
# Toggles a {Like} relationship between self and a {Likee} object
|
59
|
+
#
|
60
|
+
# @param [Likee] likee - the likee of the {Like} relationship
|
61
|
+
# @return [Boolean]
|
62
|
+
###
|
63
|
+
def toggle_like(likee)
|
64
|
+
Like.toggle_like(self, likee)
|
65
|
+
end
|
66
|
+
|
67
|
+
###
|
68
|
+
# Specifies if self likes a {Liker} object
|
69
|
+
#
|
70
|
+
# @param [Likee] likee - the {Likee} object to test against
|
71
|
+
# @return [Boolean]
|
72
|
+
###
|
73
|
+
def likes?(likee)
|
74
|
+
Like.likes?(self, likee)
|
75
|
+
end
|
76
|
+
|
77
|
+
###
|
78
|
+
# Retrieves a scope of {Like} objects that are liked by self
|
79
|
+
#
|
80
|
+
# @param [Class] klass - the {Class} to include
|
81
|
+
# @return [ActiveRecord::Relation]
|
82
|
+
###
|
83
|
+
def likees_by(klass)
|
84
|
+
Like.scope_by_liker(self).scope_by_likee_type(klass)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
data/like_system.gemspec
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'like_system/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "like_system"
|
8
|
+
spec.version = LikeSystem::VERSION
|
9
|
+
spec.authors = ["Pablo Martin Viva"]
|
10
|
+
spec.email = ["pmviva@gmail.com"]
|
11
|
+
spec.summary = %q{An active record like system.}
|
12
|
+
spec.description = %q{An active record like system developed using ruby on rails 4.1 applying domain driven design and test driven development principles.}
|
13
|
+
spec.homepage = "http://github.com/pmviva/like_system"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.0.0")
|
21
|
+
|
22
|
+
spec.add_dependency "rails", [ ">= 4.1", "< 5" ]
|
23
|
+
|
24
|
+
spec.add_development_dependency "appraisal", "~> 2.0"
|
25
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
26
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
27
|
+
spec.add_development_dependency "rspec", "~> 3.1"
|
28
|
+
spec.add_development_dependency "shoulda-matchers", "~> 2.7"
|
29
|
+
spec.add_development_dependency "sqlite3", "~> 1.3"
|
30
|
+
end
|
31
|
+
|
data/spec/.empty
ADDED
File without changes
|
data/spec/.gitignore
ADDED
File without changes
|
data/spec/.keep
ADDED
File without changes
|
data/spec/db/.empty
ADDED
File without changes
|
data/spec/db/.gitignore
ADDED
File without changes
|
data/spec/db/.keep
ADDED
File without changes
|
File without changes
|
File without changes
|
File without changes
|
@@ -0,0 +1,47 @@
|
|
1
|
+
###
|
2
|
+
# CreateLikes class
|
3
|
+
#
|
4
|
+
# This class defines the test create likes migration in like system
|
5
|
+
###
|
6
|
+
class CreateLikes < ActiveRecord::Migration
|
7
|
+
###
|
8
|
+
# Changes the database
|
9
|
+
###
|
10
|
+
def change
|
11
|
+
###
|
12
|
+
# Likes table creation
|
13
|
+
###
|
14
|
+
create_table :likes do |t|
|
15
|
+
###
|
16
|
+
# Likee id field and likee type field definition
|
17
|
+
###
|
18
|
+
t.references :likee, polymorphic: true
|
19
|
+
|
20
|
+
###
|
21
|
+
# Liker id fiel and liker type field definition
|
22
|
+
###
|
23
|
+
t.references :liker, polymorphic: true
|
24
|
+
|
25
|
+
###
|
26
|
+
# Timestamps fields definition
|
27
|
+
###
|
28
|
+
t.timestamps null: false
|
29
|
+
end
|
30
|
+
|
31
|
+
###
|
32
|
+
# Likes table likee id field and likee type field index addition
|
33
|
+
###
|
34
|
+
add_index :likes, [:likee_id, :likee_type], name: "likes_likee_idx"
|
35
|
+
|
36
|
+
###
|
37
|
+
# Likes table liker id field and liker type field index addition
|
38
|
+
###
|
39
|
+
add_index :likes, [:liker_id, :liker_type], name: "likes_liker_idx"
|
40
|
+
|
41
|
+
###
|
42
|
+
# Likes table likee id field and likee type field and liker id field and liker type field unique index addition
|
43
|
+
###
|
44
|
+
add_index :likes, [:likee_id, :likee_type, :liker_id, :liker_type], name: "likes_likee_liker_idx", unique: true
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
@@ -0,0 +1,22 @@
|
|
1
|
+
###
|
2
|
+
# CreateDummyLikers class
|
3
|
+
#
|
4
|
+
# This class defines the create dummy likers migration in like system
|
5
|
+
###
|
6
|
+
class CreateDummyLikers < ActiveRecord::Migration
|
7
|
+
###
|
8
|
+
# Changes the database
|
9
|
+
###
|
10
|
+
def change
|
11
|
+
###
|
12
|
+
# Dummy likers table creation
|
13
|
+
###
|
14
|
+
create_table :dummy_likers do |t|
|
15
|
+
###
|
16
|
+
# Timestamps fields definition
|
17
|
+
###
|
18
|
+
t.timestamps null: false
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
@@ -0,0 +1,22 @@
|
|
1
|
+
###
|
2
|
+
# CreateDummyLikees class
|
3
|
+
#
|
4
|
+
# This class defines the create dummy likees migration in like system
|
5
|
+
###
|
6
|
+
class CreateDummyLikees < ActiveRecord::Migration
|
7
|
+
###
|
8
|
+
# Changes the database
|
9
|
+
###
|
10
|
+
def change
|
11
|
+
###
|
12
|
+
# Dummy likees table creation
|
13
|
+
###
|
14
|
+
create_table :dummy_likees do |t|
|
15
|
+
###
|
16
|
+
# Timestamps fields definition
|
17
|
+
###
|
18
|
+
t.timestamps null: false
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
File without changes
|
File without changes
|
File without changes
|
@@ -0,0 +1,177 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
###
|
4
|
+
# Describes LikeSystem::Like
|
5
|
+
###
|
6
|
+
describe LikeSystem::Like do
|
7
|
+
###
|
8
|
+
# Let likee be DummyLikee.create
|
9
|
+
###
|
10
|
+
let(:likee) { DummyLikee.create }
|
11
|
+
|
12
|
+
###
|
13
|
+
# Let liker be DummyLiker.create
|
14
|
+
###
|
15
|
+
let(:liker) { DummyLiker.create }
|
16
|
+
|
17
|
+
###
|
18
|
+
# Describes associations
|
19
|
+
###
|
20
|
+
describe "associations" do
|
21
|
+
###
|
22
|
+
# Should belong to likee
|
23
|
+
###
|
24
|
+
it "should belong to likee" do
|
25
|
+
should belong_to(:likee)
|
26
|
+
end
|
27
|
+
|
28
|
+
###
|
29
|
+
# Should belong to liker
|
30
|
+
###
|
31
|
+
it "should belong to liker" do
|
32
|
+
should belong_to(:liker)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
###
|
37
|
+
# Describes class methods
|
38
|
+
###
|
39
|
+
describe "class methods" do
|
40
|
+
###
|
41
|
+
# Should raise argument error on invalid likee when likes
|
42
|
+
###
|
43
|
+
it "should raise argument error on invalid likee when likes" do
|
44
|
+
expect { LikeSystem::Like.like(liker, liker) }.to raise_error ArgumentError
|
45
|
+
end
|
46
|
+
|
47
|
+
###
|
48
|
+
# Should raise argument error on invalid liker when likes
|
49
|
+
###
|
50
|
+
it "should raise argument error on invalid liker when likes " do
|
51
|
+
expect { LikeSystem::Like.like(likee, likee) }.to raise_error ArgumentError
|
52
|
+
end
|
53
|
+
|
54
|
+
###
|
55
|
+
# Should like
|
56
|
+
###
|
57
|
+
it "should like" do
|
58
|
+
expect(LikeSystem::Like.like(liker, likee)).to equal(true)
|
59
|
+
end
|
60
|
+
|
61
|
+
###
|
62
|
+
# Should not like
|
63
|
+
###
|
64
|
+
it "should not like" do
|
65
|
+
LikeSystem::Like.like(liker, likee)
|
66
|
+
|
67
|
+
expect(LikeSystem::Like.like(liker, likee)).to equal(false)
|
68
|
+
end
|
69
|
+
|
70
|
+
###
|
71
|
+
# Should raise argument error on invalid likee when unlikes
|
72
|
+
###
|
73
|
+
it "should raise argument error on invalid likee when unlikes" do
|
74
|
+
expect { LikeSystem::Like.unlike(liker, liker) }.to raise_error ArgumentError
|
75
|
+
end
|
76
|
+
|
77
|
+
###
|
78
|
+
# Should raise argument error on invalid liker when unlikes
|
79
|
+
###
|
80
|
+
it "should raise argument error on invalid liker when unlikes" do
|
81
|
+
expect { LikeSystem::Like.unlike(likee, likee) }.to raise_error ArgumentError
|
82
|
+
end
|
83
|
+
|
84
|
+
###
|
85
|
+
# Should unlike
|
86
|
+
###
|
87
|
+
it "should unlike" do
|
88
|
+
LikeSystem::Like.like(liker, likee)
|
89
|
+
|
90
|
+
expect(LikeSystem::Like.unlike(liker, likee)).to equal(true)
|
91
|
+
end
|
92
|
+
|
93
|
+
###
|
94
|
+
# Should not unlike
|
95
|
+
###
|
96
|
+
it "should not unlike" do
|
97
|
+
expect(LikeSystem::Like.unlike(liker, likee)).to equal(false)
|
98
|
+
end
|
99
|
+
|
100
|
+
###
|
101
|
+
# Should raise argument error on invalid likee when toggle like
|
102
|
+
###
|
103
|
+
it "should raise argument error on invalid likee when toggle like" do
|
104
|
+
expect { LikeSystem::Like.toggle_like(liker, liker) }.to raise_error ArgumentError
|
105
|
+
end
|
106
|
+
|
107
|
+
###
|
108
|
+
# Should raise argument error on invalid liker when toggle like
|
109
|
+
###
|
110
|
+
it "should raise argument error on invalid liker when toggle like" do
|
111
|
+
expect { LikeSystem::Like.toggle_like(likee, likee) }.to raise_error ArgumentError
|
112
|
+
end
|
113
|
+
|
114
|
+
###
|
115
|
+
# Should toggle like
|
116
|
+
###
|
117
|
+
it "should toggle like" do
|
118
|
+
expect(LikeSystem::Like.likes?(liker, likee)).to equal(false)
|
119
|
+
|
120
|
+
LikeSystem::Like.toggle_like(liker, likee)
|
121
|
+
|
122
|
+
expect(LikeSystem::Like.likes?(liker, likee)).to equal(true)
|
123
|
+
|
124
|
+
LikeSystem::Like.toggle_like(liker, likee)
|
125
|
+
|
126
|
+
expect(LikeSystem::Like.likes?(liker, likee)).to equal(false)
|
127
|
+
end
|
128
|
+
|
129
|
+
###
|
130
|
+
# Should specify if likes
|
131
|
+
###
|
132
|
+
it "should specify if likes" do
|
133
|
+
expect(LikeSystem::Like.likes?(liker, likee)).to equal(false)
|
134
|
+
|
135
|
+
LikeSystem::Like.like(liker, likee)
|
136
|
+
|
137
|
+
expect(LikeSystem::Like.likes?(liker, likee)).to equal(true)
|
138
|
+
end
|
139
|
+
|
140
|
+
###
|
141
|
+
# Should scope likes by likee
|
142
|
+
###
|
143
|
+
it "should scope likes by likee" do
|
144
|
+
scope = LikeSystem::Like.where(likee: likee)
|
145
|
+
|
146
|
+
expect(LikeSystem::Like.scope_by_likee(likee)).to eq(scope)
|
147
|
+
end
|
148
|
+
|
149
|
+
###
|
150
|
+
# Should scope likes by likee type
|
151
|
+
####
|
152
|
+
it "should scope likes by likee type" do
|
153
|
+
scope = LikeSystem::Like.where(likee_type: DummyLikee)
|
154
|
+
|
155
|
+
expect(LikeSystem::Like.scope_by_likee_type(DummyLikee)).to eq(scope)
|
156
|
+
end
|
157
|
+
|
158
|
+
###
|
159
|
+
# Should scope likes by liker
|
160
|
+
###
|
161
|
+
it "should scope likes by liker" do
|
162
|
+
scope = LikeSystem::Like.where(liker: liker)
|
163
|
+
|
164
|
+
expect(LikeSystem::Like.scope_by_liker(liker)).to eq(scope)
|
165
|
+
end
|
166
|
+
|
167
|
+
###
|
168
|
+
# Should scope likes by liker type
|
169
|
+
####
|
170
|
+
it "should scope likes by liker type" do
|
171
|
+
scope = LikeSystem::Like.where(liker_type: DummyLiker)
|
172
|
+
|
173
|
+
expect(LikeSystem::Like.scope_by_liker_type(DummyLiker)).to eq(scope)
|
174
|
+
end
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|