partisan 0.2.3 → 0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +2 -2
- data/README.md +13 -6
- data/gemfiles/{Gemfile.rails-3.2.x → Gemfile.activerecord-3.2.x} +1 -1
- data/gemfiles/{Gemfile.rails-4.0 → Gemfile.activerecord-4.0} +1 -1
- data/lib/partisan.rb +1 -1
- data/lib/partisan/follow.rb +47 -24
- data/lib/partisan/version.rb +1 -1
- data/partisan.gemspec +1 -1
- data/spec/spec_helper.rb +0 -3
- data/spec/support/macros/model_macros.rb +5 -5
- metadata +7 -8
- data/lib/partisan/railtie.rb +0 -10
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -1,9 +1,16 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
Partisan is a Ruby library that allows ActiveRecord records to follow other records.
|
1
|
+
<p align="center">
|
2
|
+
<a href="https://github.com/mirego/partisan">
|
3
|
+
<img src="http://i.imgur.com/npOjnF2.png" alt="Partisan" />
|
4
|
+
</a>
|
5
|
+
<br />
|
6
|
+
Partisan is a Ruby library that allows ActiveRecord records to follow other records.
|
7
|
+
<br /><br />
|
8
|
+
<a href="https://rubygems.org/gems/partisan"><img src="https://badge.fury.io/rb/partisan.png" /></a>
|
9
|
+
<a href="https://codeclimate.com/github/mirego/partisan"><img src="https://codeclimate.com/github/mirego/partisan.png" /></a>
|
10
|
+
<a href="https://travis-ci.org/mirego/partisan"><img src="https://travis-ci.org/mirego/partisan.png?branch=master" /></a>
|
11
|
+
</p>
|
12
|
+
|
13
|
+
---
|
7
14
|
|
8
15
|
It’s heavily inspired by `acts_as_follower`. However, it’s not 100% compatible with `acts_as_follower` as I removed some “features”:
|
9
16
|
|
data/lib/partisan.rb
CHANGED
data/lib/partisan/follow.rb
CHANGED
@@ -14,39 +14,62 @@ module Partisan
|
|
14
14
|
after_create :update_follow_counter
|
15
15
|
after_destroy :update_follow_counter
|
16
16
|
|
17
|
-
|
18
|
-
around_create
|
19
|
-
|
20
|
-
|
21
|
-
|
17
|
+
around_create :around_create_follower
|
18
|
+
around_create :around_create_followable
|
19
|
+
around_destroy :around_destroy_follower
|
20
|
+
around_destroy :around_destroy_followable
|
21
|
+
|
22
|
+
# Constants
|
23
|
+
FOLLOWER_FOLLOW_ACCESSORS = [:about_to_follow, :just_followed]
|
24
|
+
FOLLOWER_UNFOLLOW_ACCESSORS = [:about_to_unfollow, :just_unfollowed]
|
25
|
+
FOLLOWABLE_BEING_FOLLOWED_ACCESSORS = [:about_to_be_followed_by, :just_followed_by]
|
26
|
+
FOLLOWABLE_BEING_UNFOLLOWED_ACCESSORS = [:about_to_be_unfollowed_by, :just_unfollowed_by]
|
27
|
+
|
28
|
+
protected
|
29
|
+
|
30
|
+
def update_follow_counter
|
31
|
+
follower.update_follower_counter
|
32
|
+
followable.update_followable_counter
|
22
33
|
end
|
23
34
|
|
24
|
-
|
25
|
-
|
26
|
-
self.followable.about_to_be_followed_by = self.followable.just_followed_by = self.follower
|
27
|
-
self.followable.run_callbacks :being_followed, &blk
|
28
|
-
self.followable.about_to_be_followed_by = self.followable.just_followed_by = nil
|
35
|
+
def around_create_follower(&blk)
|
36
|
+
execute_callback :follower, :follow, &blk
|
29
37
|
end
|
30
38
|
|
31
|
-
|
32
|
-
|
33
|
-
self.follower.about_to_unfollow = self.follower.just_unfollowed = self.followable
|
34
|
-
self.follower.run_callbacks :unfollow, &blk
|
35
|
-
self.follower.about_to_unfollow = self.follower.just_unfollowed = nil
|
39
|
+
def around_create_followable(&blk)
|
40
|
+
execute_callback :followable, :being_followed, &blk
|
36
41
|
end
|
37
42
|
|
38
|
-
|
39
|
-
|
40
|
-
self.followable.about_to_be_unfollowed_by = self.followable.just_unfollowed_by = self.follower
|
41
|
-
self.followable.run_callbacks :being_unfollowed, &blk
|
42
|
-
self.followable.about_to_be_unfollowed_by = self.followable.just_unfollowed_by = nil
|
43
|
+
def around_destroy_follower(&blk)
|
44
|
+
execute_callback :follower, :unfollow, &blk
|
43
45
|
end
|
44
46
|
|
45
|
-
|
47
|
+
def around_destroy_followable(&blk)
|
48
|
+
execute_callback :followable, :being_unfollowed, &blk
|
49
|
+
end
|
46
50
|
|
47
|
-
def
|
48
|
-
|
49
|
-
|
51
|
+
def self.accessors_for_follow_callback(association, callback)
|
52
|
+
const_get "#{association}_#{callback}_accessors".upcase
|
53
|
+
end
|
54
|
+
|
55
|
+
private
|
56
|
+
|
57
|
+
def execute_callback(association, callback, &blk)
|
58
|
+
# Fetch our associated objects
|
59
|
+
object = send(association)
|
60
|
+
reverse_object = association == :follower ? send(:followable) : send(:follower)
|
61
|
+
|
62
|
+
# Associate each given accessor to the reverse object
|
63
|
+
accessors = self.class.accessors_for_follow_callback(association, callback)
|
64
|
+
accessors.map { |accessor| object.send "#{accessor}=", reverse_object }
|
65
|
+
|
66
|
+
# Run the callbacks
|
67
|
+
object.run_callbacks(callback, &blk)
|
68
|
+
|
69
|
+
# Reset each accessor value
|
70
|
+
accessors.map { |accessor| object.send "#{accessor}=", nil }
|
71
|
+
|
72
|
+
true
|
50
73
|
end
|
51
74
|
end
|
52
75
|
end
|
data/lib/partisan/version.rb
CHANGED
data/partisan.gemspec
CHANGED
@@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(spec)/})
|
19
19
|
spec.require_paths = ['lib']
|
20
20
|
|
21
|
-
spec.add_dependency '
|
21
|
+
spec.add_dependency 'activerecord', '>= 3.0.0'
|
22
22
|
|
23
23
|
spec.add_development_dependency 'bundler', '~> 1.3'
|
24
24
|
spec.add_development_dependency 'rake'
|
data/spec/spec_helper.rb
CHANGED
@@ -8,9 +8,6 @@ require 'partisan'
|
|
8
8
|
# Require our macros and extensions
|
9
9
|
Dir[File.expand_path('../../spec/support/macros/*.rb', __FILE__)].map(&method(:require))
|
10
10
|
|
11
|
-
# Inject our methods into ActiveRecord (like our railtie does)
|
12
|
-
ActiveRecord::Base.class_eval(&Partisan.inject_into_active_record)
|
13
|
-
|
14
11
|
RSpec.configure do |config|
|
15
12
|
# Include our macros
|
16
13
|
config.include DatabaseMacros
|
@@ -1,5 +1,5 @@
|
|
1
1
|
module ModelMacros
|
2
|
-
# Create a new
|
2
|
+
# Create a new followable model
|
3
3
|
def followable(klass_name, &block)
|
4
4
|
spawn_model klass_name, ActiveRecord::Base do
|
5
5
|
acts_as_followable
|
@@ -7,20 +7,20 @@ module ModelMacros
|
|
7
7
|
end
|
8
8
|
end
|
9
9
|
|
10
|
-
# Create a new
|
10
|
+
# Create a new follower model
|
11
11
|
def follower(klass_name, &block)
|
12
12
|
spawn_model klass_name, ActiveRecord::Base do
|
13
13
|
acts_as_follower
|
14
|
-
|
14
|
+
instance_exec(&block) if block
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
18
|
-
# Create a new
|
18
|
+
# Create a new followable and follower model
|
19
19
|
def followable_and_follower(klass_name, &block)
|
20
20
|
spawn_model klass_name, ActiveRecord::Base do
|
21
21
|
acts_as_followable
|
22
22
|
acts_as_follower
|
23
|
-
|
23
|
+
instance_exec(&block) if block
|
24
24
|
end
|
25
25
|
end
|
26
26
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: partisan
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: '0.3'
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,10 +10,10 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2013-08-
|
13
|
+
date: 2013-08-28 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
|
-
name:
|
16
|
+
name: activerecord
|
17
17
|
requirement: !ruby/object:Gem::Requirement
|
18
18
|
none: false
|
19
19
|
requirements:
|
@@ -109,8 +109,8 @@ files:
|
|
109
109
|
- LICENSE.md
|
110
110
|
- README.md
|
111
111
|
- Rakefile
|
112
|
-
- gemfiles/Gemfile.
|
113
|
-
- gemfiles/Gemfile.
|
112
|
+
- gemfiles/Gemfile.activerecord-3.2.x
|
113
|
+
- gemfiles/Gemfile.activerecord-4.0
|
114
114
|
- lib/generators/partisan/USAGE
|
115
115
|
- lib/generators/partisan/install_generator.rb
|
116
116
|
- lib/generators/partisan/templates/migration.rb
|
@@ -119,7 +119,6 @@ files:
|
|
119
119
|
- lib/partisan/follow_helper.rb
|
120
120
|
- lib/partisan/followable.rb
|
121
121
|
- lib/partisan/follower.rb
|
122
|
-
- lib/partisan/railtie.rb
|
123
122
|
- lib/partisan/version.rb
|
124
123
|
- partisan.gemspec
|
125
124
|
- spec/partisan/followable_spec.rb
|
@@ -143,7 +142,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
143
142
|
version: '0'
|
144
143
|
segments:
|
145
144
|
- 0
|
146
|
-
hash:
|
145
|
+
hash: 83070319270688629
|
147
146
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
148
147
|
none: false
|
149
148
|
requirements:
|
@@ -152,7 +151,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
152
151
|
version: '0'
|
153
152
|
segments:
|
154
153
|
- 0
|
155
|
-
hash:
|
154
|
+
hash: 83070319270688629
|
156
155
|
requirements: []
|
157
156
|
rubyforge_project:
|
158
157
|
rubygems_version: 1.8.23
|
data/lib/partisan/railtie.rb
DELETED