Commit f48117190e801a90a7c1047e19ee142074e8212b

Authored by Rafael Chacon
1 parent 4bd761e1
Exists in 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])
... ... @@ -61,17 +61,21 @@ module ActsAsFollower #:nodoc:
61 61  
62 62 # Returns the following records.
63 63 def followers(options={})
64   - self.followings.unblocked.includes(:follower).all(options).collect{|f| f.follower}
  64 + self.followings.unblocked.includes(:follower).
  65 + apply_finder_options(options,true).
  66 + to_a.collect{|f| f.follower}
65 67 end
66 68  
67 69 def blocks(options={})
68   - self.followings.blocked.includes(:follower).all(options).collect{|f| f.follower}
  70 + self.followings.blocked.includes(:follower).
  71 + apply_finder_options(options, true).
  72 + to_a.collect{|f| f.follower}
69 73 end
70 74  
71 75 # Returns true if the current instance is followed by the passed record
72 76 # Returns false if the current instance is blocked by the passed record or no follow is found
73 77 def followed_by?(follower)
74   - self.followings.unblocked.for_follower(follower).exists?
  78 + self.followings.unblocked.for_follower(follower).first.present?
75 79 end
76 80  
77 81 def block(follower)
... ... @@ -89,7 +93,7 @@ module ActsAsFollower #:nodoc:
89 93 private
90 94  
91 95 def block_future_follow(follower)
92   - follows.create(:followable => self, :follower => follower, :blocked => true)
  96 + Follow.create(:followable => self, :follower => follower, :blocked => true)
93 97 end
94 98  
95 99 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  
... ... @@ -42,12 +42,12 @@ module ActsAsFollower #:nodoc:
42 42  
43 43 # Returns the follow records related to this instance by type.
44 44 def follows_by_type(followable_type, options={})
45   - self.follows.unblocked.includes(:followable).for_followable_type(followable_type).all(options)
  45 + self.follows.unblocked.includes(:followable).for_followable_type(followable_type).apply_finder_options(options, true)
46 46 end
47 47  
48 48 # Returns the follow records related to this instance with the followable included.
49 49 def all_follows(options={})
50   - self.follows.unblocked.includes(:followable).all(options)
  50 + self.follows.unblocked.includes(:followable).apply_finder_options(options, true)
51 51 end
52 52  
53 53 # Returns the actual records which this instance is following.
... ... @@ -60,8 +60,8 @@ module ActsAsFollower #:nodoc:
60 60 followables = followable_type.constantize.
61 61 joins(:followings).
62 62 where('follows.blocked' => false,
63   - 'follows.follower_id' => self.id,
64   - 'follows.follower_type' => parent_class_name(self),
  63 + 'follows.follower_id' => self.id,
  64 + 'follows.follower_type' => parent_class_name(self),
65 65 'follows.followable_type' => followable_type)
66 66 if options.has_key?(:limit)
67 67 followables = followables.limit(options[:limit])
... ...
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
... ... @@ -158,6 +158,7 @@ class ActsAsFollowableTest < ActiveSupport::TestCase
158 158 end
159 159  
160 160 should "be in the list of blocks" do
  161 + @sam.block(@jon)
161 162 assert_equal [@jon], @sam.blocks
162 163 end
163 164 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   -
... ...