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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5748ee44d7db6b70f9f1066be963b6b580aebc02
4
- data.tar.gz: 3c61eb3e760db7546fae7cebb8672dc1f2c2357c
3
+ metadata.gz: b3ec9dd62bd0dc88ac3f2f24c3a358cdb0a3f627
4
+ data.tar.gz: b6eb97452db346ec53035758f58eff3b0d926851
5
5
  SHA512:
6
- metadata.gz: c6e9033ebbf31b6f827790e2b645a1a96aeb46bcdf29f6e7002aebf27dd902c37a3520792158139653840a02506e81229237426645b4f9c8efd8d5e4b8a72511
7
- data.tar.gz: c3e82476466b52cad99dd727e83ac57929e0b37ae174c301043dcc48697d985aee76fb09804197ecce77451f5ad6230c37a24f04a038bfd20f1603a7cdeac22d
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 'migration.rb', 'db/migrate/create_friendships.rb'
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
@@ -0,0 +1,9 @@
1
+ class AddBlockerIdToFriendships < ActiveRecord::Migration
2
+ def self.up
3
+ add_column :friendships, :blocker_id, :integer, default: nil
4
+ end
5
+
6
+ def self.down
7
+ remove_column :friendships, :blocker_id
8
+ end
9
+ end
@@ -4,9 +4,9 @@ class CreateFriendships < ActiveRecord::Migration
4
4
  t.references :friendable, polymorphic: true
5
5
  t.integer :friend_id
6
6
  t.string :status
7
-
7
+
8
8
  t.timestamps
9
9
  end
10
10
  end
11
11
 
12
- end
12
+ 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, class_name: "HasFriendship::Friendship", dependent: :destroy
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.create(friendable_id: self.id, friendable_type: self.class.base_class.name, friend_id: friend.id, status: 'pending')
41
- HasFriendship::Friendship.create(friendable_id: friend.id, friendable_type: friend.class.base_class.name, friend_id: self.id, status: 'requested')
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
- transaction do
48
- pending_friendship = HasFriendship::Friendship.find_friendship(friend, self)
49
- pending_friendship.status = 'accepted'
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
- transaction do
60
- HasFriendship::Friendship.find_friendship(friend, self).destroy
61
- HasFriendship::Friendship.find_friendship(self, friend).destroy
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 remove_friend(friend)
80
+ def on_relation_with(friend)
66
81
  transaction do
67
- HasFriendship::Friendship.find_friendship(friend, self).destroy
68
- HasFriendship::Friendship.find_friendship(self, friend).destroy
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.where(friendable_id: self.id, friend_id: friend.id, status: 'accepted').present?
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.check_one_side(friendable, friend)
5
- find_by(friendable_id: friendable.id, friendable_type: friendable.class.base_class.name, friend_id: friend.id).present?
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
- check_one_side(friendable, friend) && check_one_side(friend, friendable)
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.find_friendship(friendable, friend)
13
- find_by(friendable_id: friendable.id, friendable_type: friendable.class.base_class.name, friend_id: friend.id)
33
+ def self.find_one_side(one, other)
34
+ find_by(relation_attributes(one, other))
14
35
  end
15
36
  end
16
37
  end
@@ -1,3 +1,3 @@
1
1
  module HasFriendship
2
- VERSION = "0.0.3"
2
+ VERSION = "0.1.0"
3
3
  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.3
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-07-29 00:00:00.000000000 Z
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.1.8
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.1.8
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/migration.rb
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