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 CHANGED
@@ -5,7 +5,7 @@ rvm:
5
5
  - 1.9.3
6
6
 
7
7
  gemfile:
8
- - gemfiles/Gemfile.rails-4.0
9
- - gemfiles/Gemfile.rails-3.2.x
8
+ - gemfiles/Gemfile.activerecord-4.0
9
+ - gemfiles/Gemfile.activerecord-3.2.x
10
10
 
11
11
  script: "echo 'DO IT' && bundle exec rake spec"
data/README.md CHANGED
@@ -1,9 +1,16 @@
1
- # Partisan
2
-
3
- [![Gem Version](https://badge.fury.io/rb/partisan.png)](https://rubygems.org/gems/partisan)
4
- [![Build Status](https://travis-ci.org/mirego/partisan.png?branch=master)](https://travis-ci.org/mirego/partisan)
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
 
@@ -2,4 +2,4 @@ source 'https://rubygems.org'
2
2
 
3
3
  gemspec path: '../'
4
4
 
5
- gem 'rails', '~> 3.2.0'
5
+ gem 'activerecord', '~> 3.2.0'
@@ -2,4 +2,4 @@ source 'https://rubygems.org'
2
2
 
3
3
  gemspec path: '../'
4
4
 
5
- gem 'rails', '~> 4.0.0'
5
+ gem 'activerecord', '~> 4.0.0'
data/lib/partisan.rb CHANGED
@@ -23,4 +23,4 @@ module Partisan
23
23
  end
24
24
  end
25
25
 
26
- require 'partisan/railtie' if defined?(Rails) && Rails::VERSION::MAJOR >= 3
26
+ ActiveRecord::Base.class_eval(&Partisan.inject_into_active_record)
@@ -14,39 +14,62 @@ module Partisan
14
14
  after_create :update_follow_counter
15
15
  after_destroy :update_follow_counter
16
16
 
17
- # Follower's :follow callbacks
18
- around_create do |follow, blk|
19
- self.follower.about_to_follow = self.follower.just_followed = self.followable
20
- self.follower.run_callbacks :follow, &blk
21
- self.follower.about_to_follow = self.follower.just_followed = nil
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
- # Followable's :follow callbacks
25
- around_create do |follow, blk|
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
- # Follower's :unfollow callbacks
32
- around_destroy do |follow, blk|
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
- # Followable's :unfollow callbacks
39
- around_destroy do |follow, blk|
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
- protected
47
+ def around_destroy_followable(&blk)
48
+ execute_callback :followable, :being_unfollowed, &blk
49
+ end
46
50
 
47
- def update_follow_counter
48
- self.follower.update_follower_counter
49
- self.followable.update_followable_counter
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
@@ -1,3 +1,3 @@
1
1
  module Partisan
2
- VERSION = "0.2.3"
2
+ VERSION = "0.3"
3
3
  end
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 'rails', '>= 3.0.0'
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 emotional model
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 emotive model
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
- class_eval(&block) if block
14
+ instance_exec(&block) if block
15
15
  end
16
16
  end
17
17
 
18
- # Create a new emotive and emotionnal model
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
- class_eval(&block) if block
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.2.3
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-09 00:00:00.000000000 Z
13
+ date: 2013-08-28 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
- name: rails
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.rails-3.2.x
113
- - gemfiles/Gemfile.rails-4.0
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: 598192539115200308
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: 598192539115200308
154
+ hash: 83070319270688629
156
155
  requirements: []
157
156
  rubyforge_project:
158
157
  rubygems_version: 1.8.23
@@ -1,10 +0,0 @@
1
- require 'partisan'
2
- require 'rails'
3
-
4
- module Partisan
5
- class Railtie < Rails::Railtie
6
- initializer "follows.active_record" do |app|
7
- ActiveSupport.on_load :active_record, {}, &Partisan.inject_into_active_record
8
- end
9
- end
10
- end