has_friendship 0.1.3 → 0.1.4

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: 705e193cc6e95b8661e5897d2e2cda94fe5fe26b
4
- data.tar.gz: 3833bf8514f56070397c975101ac50a57fe21615
3
+ metadata.gz: 498832a304c1e5234ed5ed576727d2f98a236b50
4
+ data.tar.gz: 4cf1a787e0e8034df4351707dd086c5409dde83c
5
5
  SHA512:
6
- metadata.gz: d548a1dc0cf9619ce0cc9deb827894d94d27a9b4b1df78a4db1f3e9fc559061c52960942b69948086d8324fb596514345507338ab29382851374b1b4fe3a0a24
7
- data.tar.gz: 9afea893eb09c57e734324e3ed7b845ca26bf4658ab6b1d563344a67b3d842033f32d80671a93ba2a7ae0e002f42b10f0cca1de07b53a2fc4b16d7139fae2a31
6
+ metadata.gz: b9319fd2d7592610e5e2c040c84f8083d2526d848ddc2ac84fea1e286e62b13a593922526c676d71963a69b065e4b6d5c089e2415d28c38ffddd4f162f8910fb
7
+ data.tar.gz: 889c71eb0c6ae727428041b430c1eb3a61ddc7f3082efc5158587fab5ba1994aa328b0a749c28a47ac9bcf9ef3c57cba8f94f35fa5e1715bb4fc2d390717bb4a
@@ -3,7 +3,7 @@ class CreateFriendships < ActiveRecord::Migration
3
3
  create_table :friendships do |t|
4
4
  t.references :friendable, polymorphic: true
5
5
  t.integer :friend_id
6
- t.string :status
6
+ t.integer :status, index: true
7
7
 
8
8
  t.timestamps
9
9
  end
@@ -11,21 +11,21 @@ module HasFriendship
11
11
  class_name: "HasFriendship::Friendship", dependent: :destroy
12
12
 
13
13
  has_many :blocked_friends,
14
- -> { where friendships: { status: 'blocked' } },
14
+ -> { where friendships: { status: 3 } },
15
15
  through: :friendships,
16
16
  source: :friend
17
17
 
18
18
  has_many :friends,
19
- -> { where friendships: { status: 'accepted' } },
19
+ -> { where friendships: { status: 2 } },
20
20
  through: :friendships
21
21
 
22
22
  has_many :requested_friends,
23
- -> { where friendships: { status: 'requested' } },
23
+ -> { where friendships: { status: 1 } },
24
24
  through: :friendships,
25
25
  source: :friend
26
26
 
27
27
  has_many :pending_friends,
28
- -> { where friendships: { status: 'pending' } },
28
+ -> { where friendships: { status: 0 } },
29
29
  through: :friendships,
30
30
  source: :friend
31
31
 
@@ -43,16 +43,15 @@ module HasFriendship
43
43
  def friend_request(friend)
44
44
  unless self == friend || HasFriendship::Friendship.exist?(self, friend)
45
45
  transaction do
46
- HasFriendship::Friendship.create_relation(self, friend, status: 'pending')
47
- HasFriendship::Friendship.create_relation(friend, self, status: 'requested')
46
+ HasFriendship::Friendship.create_relation(self, friend, status: 0)
47
+ HasFriendship::Friendship.create_relation(friend, self, status: 1)
48
48
  end
49
49
  end
50
50
  end
51
51
 
52
52
  def accept_request(friend)
53
53
  on_relation_with(friend) do |one, other|
54
- HasFriendship::Friendship.find_unblocked_friendship(one, other)
55
- .update(status: 'accepted')
54
+ HasFriendship::Friendship.find_unblocked_friendship(one, other).accept!
56
55
  end
57
56
  end
58
57
 
@@ -66,8 +65,7 @@ module HasFriendship
66
65
 
67
66
  def block_friend(friend)
68
67
  on_relation_with(friend) do |one, other|
69
- HasFriendship::Friendship.find_unblocked_friendship(one, other)
70
- .update(status: 'blocked', blocker_id: self.id)
68
+ HasFriendship::Friendship.find_unblocked_friendship(one, other).block!
71
69
  end
72
70
  end
73
71
 
@@ -86,7 +84,7 @@ module HasFriendship
86
84
  end
87
85
 
88
86
  def friends_with?(friend)
89
- HasFriendship::Friendship.find_relation(self, friend, status: 'accepted').any?
87
+ HasFriendship::Friendship.find_relation(self, friend, status: 2).any?
90
88
  end
91
89
 
92
90
  private
@@ -1,5 +1,20 @@
1
1
  module HasFriendship
2
2
  class Friendship < ActiveRecord::Base
3
+
4
+ enum status: { pending: 0, requested: 1, accepted: 2, blocked: 3 } do
5
+ event :accept do
6
+ transition [:pending, :requested] => :accepted
7
+ end
8
+
9
+ event :block do
10
+ before do
11
+ self.blocker_id = self.friendable.id
12
+ end
13
+
14
+ transition all - [:blocked] => :blocked
15
+ end
16
+ end
17
+
3
18
  def self.relation_attributes(one, other, status: nil)
4
19
  attr = {
5
20
  friendable_id: one.id,
@@ -29,11 +44,11 @@ module HasFriendship
29
44
  end
30
45
 
31
46
  def self.find_unblocked_friendship(friendable, friend)
32
- find_relation(friendable, friend).where.not(status: "blocked").first
47
+ find_relation(friendable, friend).where.not(status: 3).first
33
48
  end
34
49
 
35
50
  def self.find_blocked_friendship(friendable, friend)
36
- find_relation(friendable, friend).where(status: "blocked").first
51
+ find_relation(friendable, friend).where(status: 3).first
37
52
  end
38
53
 
39
54
  def self.find_one_side(one, other)
@@ -1,3 +1,3 @@
1
1
  module HasFriendship
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
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.1.3
4
+ version: 0.1.4
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: 2016-04-04 00:00:00.000000000 Z
11
+ date: 2016-04-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: stateful_enum
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: sqlite3
29
43
  requirement: !ruby/object:Gem::Requirement