Commit 0f3a095d3c659d91f509040cf2eda1ad35ddd0e3

Authored by Tom Cocca
2 parents 4bd761e1 4f9c4cc3
Exists in master

Merge pull request #32 from rafael/master

Update the code to work with Rails 4.
Rakefile
... ... @@ -3,7 +3,7 @@ Bundler::GemHelper.install_tasks
3 3  
4 4 require 'rake'
5 5 require 'rake/testtask'
6   -require 'rake/rdoctask'
  6 +require 'rdoc/task'
7 7  
8 8 desc 'Default: run unit tests.'
9 9 task :default => :test
... ...
acts_as_follower.gemspec
... ... @@ -19,7 +19,8 @@ Gem::Specification.new do |s|
19 19 s.require_paths = ["lib"]
20 20  
21 21 s.add_development_dependency "sqlite3"
22   - s.add_development_dependency "shoulda"
23   - s.add_development_dependency "factory_girl"
24   - s.add_development_dependency "rails", "~>3.0.10"
  22 + s.add_development_dependency "shoulda_create"
  23 + s.add_development_dependency "shoulda", "3.5.0"
  24 + s.add_development_dependency "factory_girl", "4.2.0"
  25 + s.add_development_dependency "rails", "~>4.0.0"
25 26 end
... ...
lib/acts_as_follower/follow_scopes.rb
... ... @@ -2,7 +2,8 @@ module ActsAsFollower #:nodoc:
2 2 module FollowScopes
3 3  
4 4 def for_follower(follower)
5   - where(:follower_id => follower.id, :follower_type => parent_class_name(follower))
  5 + where(:follower_id => follower.id,
  6 + :follower_type => parent_class_name(follower))
6 7 end
7 8  
8 9 def for_followable(followable)
... ...
lib/acts_as_follower/followable.rb
... ... @@ -25,8 +25,8 @@ module ActsAsFollower #:nodoc:
25 25 follows = follower_type.constantize.
26 26 joins(:follows).
27 27 where('follows.blocked' => false,
28   - 'follows.followable_id' => self.id,
29   - 'follows.followable_type' => parent_class_name(self),
  28 + 'follows.followable_id' => self.id,
  29 + 'follows.followable_type' => parent_class_name(self),
30 30 'follows.follower_type' => follower_type)
31 31 if options.has_key?(:limit)
32 32 follows = follows.limit(options[:limit])
... ... @@ -59,19 +59,27 @@ module ActsAsFollower #:nodoc:
59 59 self.followings.blocked.count
60 60 end
61 61  
62   - # Returns the following records.
  62 + # Returns the followings records scoped
  63 + def followers_scoped
  64 + self.followings.includes(:follower)
  65 + end
  66 +
63 67 def followers(options={})
64   - self.followings.unblocked.includes(:follower).all(options).collect{|f| f.follower}
  68 + followers_scope = followers_scoped.unblocked
  69 + followers_scope = apply_options_to_scope(followers_scope, options)
  70 + followers_scope.to_a.collect{|f| f.follower}
65 71 end
66 72  
67 73 def blocks(options={})
68   - self.followings.blocked.includes(:follower).all(options).collect{|f| f.follower}
  74 + blocked_followers_scope = followers_scoped.blocked
  75 + blocked_followers_scope = apply_options_to_scope(blocked_followers_scope, options)
  76 + blocked_followers_scope.to_a.collect{|f| f.follower}
69 77 end
70 78  
71 79 # Returns true if the current instance is followed by the passed record
72 80 # Returns false if the current instance is blocked by the passed record or no follow is found
73 81 def followed_by?(follower)
74   - self.followings.unblocked.for_follower(follower).exists?
  82 + self.followings.unblocked.for_follower(follower).first.present?
75 83 end
76 84  
77 85 def block(follower)
... ... @@ -89,7 +97,7 @@ module ActsAsFollower #:nodoc:
89 97 private
90 98  
91 99 def block_future_follow(follower)
92   - follows.create(:followable => self, :follower => follower, :blocked => true)
  100 + Follow.create(:followable => self, :follower => follower, :blocked => true)
93 101 end
94 102  
95 103 def block_existing_follow(follower)
... ...
lib/acts_as_follower/follower.rb
... ... @@ -29,7 +29,7 @@ module ActsAsFollower #:nodoc:
29 29 # Does not allow duplicate records to be created.
30 30 def follow(followable)
31 31 if self != followable
32   - self.follows.find_or_create_by_followable_id_and_followable_type(followable.id, parent_class_name(followable))
  32 + self.follows.find_or_create_by(followable_id: followable.id, followable_type: parent_class_name(followable))
33 33 end
34 34 end
35 35  
... ... @@ -40,14 +40,21 @@ module ActsAsFollower #:nodoc:
40 40 end
41 41 end
42 42  
  43 + # returns the follows records to the current instance
  44 + def follows_scoped
  45 + self.follows.unblocked.includes(:followable)
  46 + end
  47 +
43 48 # Returns the follow records related to this instance by type.
44 49 def follows_by_type(followable_type, options={})
45   - self.follows.unblocked.includes(:followable).for_followable_type(followable_type).all(options)
  50 + follows_scope = follows_scoped.for_followable_type(followable_type)
  51 + follows_scope = apply_options_to_scope(follows_scope, options)
46 52 end
47 53  
48 54 # Returns the follow records related to this instance with the followable included.
49 55 def all_follows(options={})
50   - self.follows.unblocked.includes(:followable).all(options)
  56 + follows_scope = follows_scoped
  57 + follows_scope = apply_options_to_scope(follows_scope, options)
51 58 end
52 59  
53 60 # Returns the actual records which this instance is following.
... ... @@ -60,8 +67,8 @@ module ActsAsFollower #:nodoc:
60 67 followables = followable_type.constantize.
61 68 joins(:followings).
62 69 where('follows.blocked' => false,
63   - 'follows.follower_id' => self.id,
64   - 'follows.follower_type' => parent_class_name(self),
  70 + 'follows.follower_id' => self.id,
  71 + 'follows.follower_type' => parent_class_name(self),
65 72 'follows.followable_type' => followable_type)
66 73 if options.has_key?(:limit)
67 74 followables = followables.limit(options[:limit])
... ...
lib/acts_as_follower/follower_lib.rb
... ... @@ -11,5 +11,23 @@ module ActsAsFollower
11 11 return obj.class.name
12 12 end
13 13  
  14 + def apply_options_to_scope(scope, options = {})
  15 + if options.has_key?(:limit)
  16 + scope = scope.limit(options[:limit])
  17 + end
  18 + if options.has_key?(:includes)
  19 + scope = scope.includes(options[:includes])
  20 + end
  21 + if options.has_key?(:joins)
  22 + scope = scope.joins(options[:joins])
  23 + end
  24 + if options.has_key?(:where)
  25 + scope = scope.order(options[:where])
  26 + end
  27 + if options.has_key?(:order)
  28 + scope = scope.order(options[:order])
  29 + end
  30 + scope
  31 + end
14 32 end
15 33 end
... ...
test/acts_as_followable_test.rb
... ... @@ -4,7 +4,7 @@ class ActsAsFollowableTest < ActiveSupport::TestCase
4 4  
5 5 context "instance methods" do
6 6 setup do
7   - @sam = Factory(:sam)
  7 + @sam = FactoryGirl.create(:sam)
8 8 end
9 9  
10 10 should "be defined" do
... ... @@ -16,10 +16,10 @@ class ActsAsFollowableTest < ActiveSupport::TestCase
16 16  
17 17 context "acts_as_followable" do
18 18 setup do
19   - @sam = Factory(:sam)
20   - @jon = Factory(:jon)
21   - @oasis = Factory(:oasis)
22   - @metallica = Factory(:metallica)
  19 + @sam = FactoryGirl.create(:sam)
  20 + @jon = FactoryGirl.create(:jon)
  21 + @oasis = FactoryGirl.create(:oasis)
  22 + @metallica = FactoryGirl.create(:metallica)
23 23 @sam.follow(@jon)
24 24 end
25 25  
... ... @@ -30,7 +30,7 @@ class ActsAsFollowableTest < ActiveSupport::TestCase
30 30 end
31 31  
32 32 should "return the proper number of multiple followers" do
33   - @bob = Factory(:bob)
  33 + @bob = FactoryGirl.create(:bob)
34 34 @sam.follow(@bob)
35 35 assert_equal 0, @sam.followers_count
36 36 assert_equal 1, @jon.followers_count
... ... @@ -45,7 +45,7 @@ class ActsAsFollowableTest < ActiveSupport::TestCase
45 45 end
46 46  
47 47 should "return users (multiple followers)" do
48   - @bob = Factory(:bob)
  48 + @bob = FactoryGirl.create(:bob)
49 49 @sam.follow(@bob)
50 50 assert_equal [], @sam.followers
51 51 assert_equal [@sam], @jon.followers
... ... @@ -53,7 +53,7 @@ class ActsAsFollowableTest < ActiveSupport::TestCase
53 53 end
54 54  
55 55 should "return users (multiple followers, complex)" do
56   - @bob = Factory(:bob)
  56 + @bob = FactoryGirl.create(:bob)
57 57 @sam.follow(@bob)
58 58 @jon.follow(@bob)
59 59 assert_equal [], @sam.followers
... ... @@ -62,7 +62,7 @@ class ActsAsFollowableTest < ActiveSupport::TestCase
62 62 end
63 63  
64 64 should "accept AR options" do
65   - @bob = Factory(:bob)
  65 + @bob = FactoryGirl.create(:bob)
66 66 @bob.follow(@jon)
67 67 assert_equal 1, @jon.followers(:limit => 1).count
68 68 end
... ... @@ -86,7 +86,7 @@ class ActsAsFollowableTest < ActiveSupport::TestCase
86 86  
87 87 context "get follow record" do
88 88 setup do
89   - @bob = Factory(:bob)
  89 + @bob = FactoryGirl.create(:bob)
90 90 @follow = @bob.follow(@sam)
91 91 end
92 92  
... ... @@ -101,7 +101,7 @@ class ActsAsFollowableTest < ActiveSupport::TestCase
101 101  
102 102 context "blocks" do
103 103 setup do
104   - @bob = Factory(:bob)
  104 + @bob = FactoryGirl.create(:bob)
105 105 @jon.block(@sam)
106 106 @jon.block(@bob)
107 107 end
... ...
test/acts_as_follower_test.rb
... ... @@ -4,7 +4,7 @@ class ActsAsFollowerTest < ActiveSupport::TestCase
4 4  
5 5 context "instance methods" do
6 6 setup do
7   - @sam = Factory(:sam)
  7 + @sam = FactoryGirl.create(:sam)
8 8 end
9 9  
10 10 should "be defined" do
... ... @@ -19,9 +19,9 @@ class ActsAsFollowerTest < ActiveSupport::TestCase
19 19  
20 20 context "acts_as_follower" do
21 21 setup do
22   - @sam = Factory(:sam)
23   - @jon = Factory(:jon)
24   - @oasis = Factory(:oasis)
  22 + @sam = FactoryGirl.create(:sam)
  23 + @jon = FactoryGirl.create(:jon)
  24 + @oasis = FactoryGirl.create(:oasis)
25 25 @sam.follow(@jon)
26 26 @sam.follow(@oasis)
27 27 end
... ... @@ -83,8 +83,8 @@ class ActsAsFollowerTest < ActiveSupport::TestCase
83 83  
84 84 context "follows" do
85 85 setup do
86   - @band_follow = Follow.find(:first, :conditions => ["follower_id = ? and follower_type = 'User' and followable_id = ? and followable_type = 'Band'", @sam.id, @oasis.id])
87   - @user_follow = Follow.find(:first, :conditions => ["follower_id = ? and follower_type = 'User' and followable_id = ? and followable_type = 'User'", @sam.id, @jon.id])
  86 + @band_follow = Follow.where("follower_id = ? and follower_type = 'User' and followable_id = ? and followable_type = 'Band'", @sam.id, @oasis.id).first
  87 + @user_follow = Follow.where("follower_id = ? and follower_type = 'User' and followable_id = ? and followable_type = 'User'", @sam.id, @jon.id).first
88 88 end
89 89  
90 90 context "follows_by_type" do
... ... @@ -94,7 +94,7 @@ class ActsAsFollowerTest < ActiveSupport::TestCase
94 94 end
95 95  
96 96 should "accept AR options" do
97   - @metallica = Factory(:metallica)
  97 + @metallica = FactoryGirl.create(:metallica)
98 98 @sam.follow(@metallica)
99 99 assert_equal 1, @sam.follows_by_type('Band', :limit => 1).count
100 100 end
... ... @@ -102,7 +102,7 @@ class ActsAsFollowerTest < ActiveSupport::TestCase
102 102  
103 103 context "following_by_type_count" do
104 104 should "return the count of the requested type" do
105   - @metallica = Factory(:metallica)
  105 + @metallica = FactoryGirl.create(:metallica)
106 106 @sam.follow(@metallica)
107 107 assert_equal 2, @sam.following_by_type_count('Band')
108 108 assert_equal 1, @sam.following_by_type_count('User')
... ... @@ -146,7 +146,7 @@ class ActsAsFollowerTest < ActiveSupport::TestCase
146 146 end
147 147  
148 148 should "accept AR options" do
149   - @metallica = Factory(:metallica)
  149 + @metallica = FactoryGirl.create(:metallica)
150 150 @sam.follow(@metallica)
151 151 assert_equal 1, @sam.following_by_type('Band', :limit => 1).to_a.size
152 152 end
... ... @@ -159,7 +159,7 @@ class ActsAsFollowerTest < ActiveSupport::TestCase
159 159 end
160 160  
161 161 should "call following_by_type_count" do
162   - @metallica = Factory(:metallica)
  162 + @metallica = FactoryGirl.create(:metallica)
163 163 @sam.follow(@metallica)
164 164 assert_equal 2, @sam.following_bands_count
165 165 assert_equal 1, @sam.following_users_count
... ...
test/dummy30/config/environments/test.rb
... ... @@ -7,9 +7,6 @@ Dummy::Application.configure do
7 7 # and recreated between test runs. Don't rely on the data there!
8 8 config.cache_classes = true
9 9  
10   - # Log error messages when you accidentally call methods on nil.
11   - config.whiny_nils = true
12   -
13 10 # Show full error reports and disable caching
14 11 config.consider_all_requests_local = true
15 12  
... ...
test/factories/bands.rb
1   -Factory.define :oasis, :class => Band do |b|
2   - b.name 'Oasis'
3   -end
  1 +FactoryGirl.define do
  2 + factory :oasis, :class => Band do |b|
  3 + b.name 'Oasis'
  4 + end
4 5  
5   -Factory.define :metallica, :class => Band do |b|
6   - b.name 'Metallica'
  6 + factory :metallica, :class => Band do |b|
  7 + b.name 'Metallica'
  8 + end
7 9 end
... ...
test/factories/users.rb
1   -Factory.define :jon, :class => User do |u|
2   - u.name 'Jon'
3   -end
  1 +FactoryGirl.define do
  2 + factory :jon, class: User do |u|
  3 + u.name 'Jon'
  4 + end
4 5  
5   -Factory.define :sam, :class => User do |u|
6   - u.name 'Sam'
7   -end
  6 + factory :sam, :class => User do |u|
  7 + u.name 'Sam'
  8 + end
8 9  
9   -Factory.define :bob, :class => User do |u|
10   - u.name 'Bob'
  10 + factory :bob, :class => User do |u|
  11 + u.name 'Bob'
  12 + end
11 13 end
... ...
test/test_helper.rb
... ... @@ -12,6 +12,7 @@ load(File.dirname(__FILE__) + '/schema.rb')
12 12 require File.dirname(__FILE__) + '/../lib/generators/templates/model.rb'
13 13  
14 14 require 'shoulda'
  15 +require 'shoulda_create'
15 16 require 'factory_girl'
  17 +ActiveSupport::TestCase.extend(ShouldaCreate)
16 18 FactoryGirl.find_definitions
17   -
... ...