has_friendship 0.0.3 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/generators/has_friendship/has_friendship_generator.rb +5 -2
- data/lib/generators/has_friendship/templates/add_blocker_id_to_friendships.rb +9 -0
- data/lib/generators/has_friendship/templates/{migration.rb → create_friendships.rb} +2 -2
- data/lib/has_friendship/friendable.rb +39 -19
- data/lib/has_friendship/friendship.rb +26 -5
- data/lib/has_friendship/version.rb +1 -1
- metadata +20 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b3ec9dd62bd0dc88ac3f2f24c3a358cdb0a3f627
|
4
|
+
data.tar.gz: b6eb97452db346ec53035758f58eff3b0d926851
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1a89cb131c5523a31dcce5b211c63d0dbe5e46d09f78c562caf08201e1c81513ee5d6c8df1dd4ffef145fa22bdac7972aede1428d9cbfab1b1978fc9561be58c
|
7
|
+
data.tar.gz: 858c9683fee0765f24a8eb51ae6bc04b5d4e0b053856687c1ad5b874d8a0ea050591751d81260319bb11f8c6013613cae279651675e1090b52155bf2ac9b18dd
|
@@ -13,6 +13,9 @@ class HasFriendshipGenerator < Rails::Generators::Base
|
|
13
13
|
end
|
14
14
|
|
15
15
|
def create_migration_file
|
16
|
-
migration_template '
|
16
|
+
migration_template 'create_friendships.rb',
|
17
|
+
'db/migrate/create_friendships.rb'
|
18
|
+
migration_template 'add_blocker_id_to_friendships.rb',
|
19
|
+
'db/migrate/add_blocker_id_to_friendships.rb'
|
17
20
|
end
|
18
|
-
end
|
21
|
+
end
|
@@ -6,9 +6,14 @@ module HasFriendship
|
|
6
6
|
end
|
7
7
|
|
8
8
|
def has_friendship
|
9
|
-
|
10
9
|
class_eval do
|
11
|
-
has_many :friendships, as: :friendable,
|
10
|
+
has_many :friendships, as: :friendable,
|
11
|
+
class_name: "HasFriendship::Friendship", dependent: :destroy
|
12
|
+
|
13
|
+
has_many :blocked_friends,
|
14
|
+
-> { where friendships: { status: 'blocked' } },
|
15
|
+
through: :friendships
|
16
|
+
|
12
17
|
has_many :friends,
|
13
18
|
-> { where friendships: { status: 'accepted' } },
|
14
19
|
through: :friendships
|
@@ -37,42 +42,57 @@ module HasFriendship
|
|
37
42
|
def friend_request(friend)
|
38
43
|
unless self == friend || HasFriendship::Friendship.exist?(self, friend)
|
39
44
|
transaction do
|
40
|
-
HasFriendship::Friendship.
|
41
|
-
HasFriendship::Friendship.
|
45
|
+
HasFriendship::Friendship.create_relation(self, friend, status: 'pending')
|
46
|
+
HasFriendship::Friendship.create_relation(friend, self, status: 'requested')
|
42
47
|
end
|
43
48
|
end
|
44
49
|
end
|
45
50
|
|
46
51
|
def accept_request(friend)
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
pending_friendship.save
|
51
|
-
|
52
|
-
requeseted_friendship = HasFriendship::Friendship.find_friendship(self, friend)
|
53
|
-
requeseted_friendship.status = 'accepted'
|
54
|
-
requeseted_friendship.save
|
52
|
+
on_relation_with(friend) do |one, other|
|
53
|
+
HasFriendship::Friendship.find_unblocked_friendship(one, other)
|
54
|
+
.update(status: 'accepted')
|
55
55
|
end
|
56
56
|
end
|
57
57
|
|
58
58
|
def decline_request(friend)
|
59
|
-
|
60
|
-
HasFriendship::Friendship.
|
61
|
-
|
59
|
+
on_relation_with(friend) do |one, other|
|
60
|
+
HasFriendship::Friendship.find_unblocked_friendship(one, other).destroy
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
alias_method :remove_friend, :decline_request
|
65
|
+
|
66
|
+
def block_friend(friend)
|
67
|
+
on_relation_with(friend) do |one, other|
|
68
|
+
HasFriendship::Friendship.find_unblocked_friendship(one, other)
|
69
|
+
.update(status: 'blocked', blocker_id: self.id)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def unblock_friend(friend)
|
74
|
+
return unless has_blocked(friend)
|
75
|
+
on_relation_with(friend) do |one, other|
|
76
|
+
HasFriendship::Friendship.find_blocked_friendship(one, other).destroy
|
62
77
|
end
|
63
78
|
end
|
64
79
|
|
65
|
-
def
|
80
|
+
def on_relation_with(friend)
|
66
81
|
transaction do
|
67
|
-
|
68
|
-
|
82
|
+
yield(self, friend)
|
83
|
+
yield(friend, self)
|
69
84
|
end
|
70
85
|
end
|
71
86
|
|
72
87
|
def friends_with?(friend)
|
73
|
-
HasFriendship::Friendship.
|
88
|
+
HasFriendship::Friendship.find_relation(self, friend).any?
|
74
89
|
end
|
75
90
|
|
91
|
+
private
|
92
|
+
|
93
|
+
def has_blocked(friend)
|
94
|
+
HasFriendship::Friendship.find_one_side(self, friend).blocker_id == self.id
|
95
|
+
end
|
76
96
|
end
|
77
97
|
end
|
78
98
|
end
|
@@ -1,16 +1,37 @@
|
|
1
1
|
module HasFriendship
|
2
2
|
class Friendship < ActiveRecord::Base
|
3
|
+
def self.relation_attributes(one, other)
|
4
|
+
{
|
5
|
+
friendable_id: one.id,
|
6
|
+
friendable_type: one.class.base_class.name,
|
7
|
+
friend_id: other.id
|
8
|
+
}
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.create_relation(one, other, options)
|
12
|
+
relation = new relation_attributes(one, other)
|
13
|
+
relation.attributes = options
|
14
|
+
relation.save
|
15
|
+
end
|
3
16
|
|
4
|
-
def self.
|
5
|
-
|
17
|
+
def self.find_relation(friendable, friend)
|
18
|
+
where relation_attributes(friendable, friend)
|
6
19
|
end
|
7
20
|
|
8
21
|
def self.exist?(friendable, friend)
|
9
|
-
|
22
|
+
find_relation(friendable, friend).any? && find_relation(friend, friendable).any?
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.find_unblocked_friendship(friendable, friend)
|
26
|
+
find_relation(friendable, friend).where.not(status: "blocked").first
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.find_blocked_friendship(friendable, friend)
|
30
|
+
find_relation(friendable, friend).where(status: "blocked").first
|
10
31
|
end
|
11
32
|
|
12
|
-
def self.
|
13
|
-
find_by(
|
33
|
+
def self.find_one_side(one, other)
|
34
|
+
find_by(relation_attributes(one, other))
|
14
35
|
end
|
15
36
|
end
|
16
37
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: has_friendship
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sung Won Cho
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-12-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 4.
|
19
|
+
version: '4.0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: 4.
|
26
|
+
version: '4.0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: sqlite3
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -122,6 +122,20 @@ dependencies:
|
|
122
122
|
- - ">="
|
123
123
|
- !ruby/object:Gem::Version
|
124
124
|
version: '0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: tzinfo-data
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
125
139
|
description:
|
126
140
|
email:
|
127
141
|
- mikeswcho@gmail.com
|
@@ -132,7 +146,8 @@ files:
|
|
132
146
|
- MIT-LICENSE
|
133
147
|
- Rakefile
|
134
148
|
- lib/generators/has_friendship/has_friendship_generator.rb
|
135
|
-
- lib/generators/has_friendship/templates/
|
149
|
+
- lib/generators/has_friendship/templates/add_blocker_id_to_friendships.rb
|
150
|
+
- lib/generators/has_friendship/templates/create_friendships.rb
|
136
151
|
- lib/has_friendship.rb
|
137
152
|
- lib/has_friendship/extender.rb
|
138
153
|
- lib/has_friendship/friendable.rb
|