diff --git a/Rakefile b/Rakefile index e33a737..ab56276 100644 --- a/Rakefile +++ b/Rakefile @@ -3,7 +3,7 @@ Bundler::GemHelper.install_tasks require 'rake' require 'rake/testtask' -require 'rake/rdoctask' +require 'rdoc/task' desc 'Default: run unit tests.' task :default => :test diff --git a/acts_as_follower.gemspec b/acts_as_follower.gemspec index 7975494..f6d2e69 100644 --- a/acts_as_follower.gemspec +++ b/acts_as_follower.gemspec @@ -19,7 +19,8 @@ Gem::Specification.new do |s| s.require_paths = ["lib"] s.add_development_dependency "sqlite3" - s.add_development_dependency "shoulda" - s.add_development_dependency "factory_girl" - s.add_development_dependency "rails", "~>3.0.10" + s.add_development_dependency "shoulda_create" + s.add_development_dependency "shoulda", "3.5.0" + s.add_development_dependency "factory_girl", "4.2.0" + s.add_development_dependency "rails", "~>4.0.0" end diff --git a/lib/acts_as_follower/follow_scopes.rb b/lib/acts_as_follower/follow_scopes.rb index 6917132..17a01eb 100644 --- a/lib/acts_as_follower/follow_scopes.rb +++ b/lib/acts_as_follower/follow_scopes.rb @@ -2,7 +2,8 @@ module ActsAsFollower #:nodoc: module FollowScopes def for_follower(follower) - where(:follower_id => follower.id, :follower_type => parent_class_name(follower)) + where(:follower_id => follower.id, + :follower_type => parent_class_name(follower)) end def for_followable(followable) diff --git a/lib/acts_as_follower/followable.rb b/lib/acts_as_follower/followable.rb index 7582859..9b53407 100644 --- a/lib/acts_as_follower/followable.rb +++ b/lib/acts_as_follower/followable.rb @@ -25,8 +25,8 @@ def followers_by_type(follower_type, options={}) follows = follower_type.constantize. joins(:follows). where('follows.blocked' => false, - 'follows.followable_id' => self.id, - 'follows.followable_type' => parent_class_name(self), + 'follows.followable_id' => self.id, + 'follows.followable_type' => parent_class_name(self), 'follows.follower_type' => follower_type) if options.has_key?(:limit) follows = follows.limit(options[:limit]) @@ -61,17 +61,21 @@ def blocked_followers_count # Returns the following records. def followers(options={}) - self.followings.unblocked.includes(:follower).all(options).collect{|f| f.follower} + self.followings.unblocked.includes(:follower). + apply_finder_options(options,true). + to_a.collect{|f| f.follower} end def blocks(options={}) - self.followings.blocked.includes(:follower).all(options).collect{|f| f.follower} + self.followings.blocked.includes(:follower). + apply_finder_options(options, true). + to_a.collect{|f| f.follower} end # Returns true if the current instance is followed by the passed record # Returns false if the current instance is blocked by the passed record or no follow is found def followed_by?(follower) - self.followings.unblocked.for_follower(follower).exists? + self.followings.unblocked.for_follower(follower).first.present? end def block(follower) @@ -89,7 +93,7 @@ def get_follow_for(follower) private def block_future_follow(follower) - follows.create(:followable => self, :follower => follower, :blocked => true) + Follow.create(:followable => self, :follower => follower, :blocked => true) end def block_existing_follow(follower) diff --git a/lib/acts_as_follower/follower.rb b/lib/acts_as_follower/follower.rb index 2bef3bb..cce364a 100644 --- a/lib/acts_as_follower/follower.rb +++ b/lib/acts_as_follower/follower.rb @@ -29,7 +29,7 @@ def follow_count # Does not allow duplicate records to be created. def follow(followable) if self != followable - self.follows.find_or_create_by_followable_id_and_followable_type(followable.id, parent_class_name(followable)) + self.follows.find_or_create_by(followable_id: followable.id, followable_type: parent_class_name(followable)) end end @@ -42,12 +42,12 @@ def stop_following(followable) # Returns the follow records related to this instance by type. def follows_by_type(followable_type, options={}) - self.follows.unblocked.includes(:followable).for_followable_type(followable_type).all(options) + self.follows.unblocked.includes(:followable).for_followable_type(followable_type).apply_finder_options(options, true) end # Returns the follow records related to this instance with the followable included. def all_follows(options={}) - self.follows.unblocked.includes(:followable).all(options) + self.follows.unblocked.includes(:followable).apply_finder_options(options, true) end # Returns the actual records which this instance is following. @@ -60,8 +60,8 @@ def following_by_type(followable_type, options={}) followables = followable_type.constantize. joins(:followings). where('follows.blocked' => false, - 'follows.follower_id' => self.id, - 'follows.follower_type' => parent_class_name(self), + 'follows.follower_id' => self.id, + 'follows.follower_type' => parent_class_name(self), 'follows.followable_type' => followable_type) if options.has_key?(:limit) followables = followables.limit(options[:limit]) diff --git a/test/acts_as_followable_test.rb b/test/acts_as_followable_test.rb index 8b9364d..5f713d5 100644 --- a/test/acts_as_followable_test.rb +++ b/test/acts_as_followable_test.rb @@ -4,7 +4,7 @@ class ActsAsFollowableTest < ActiveSupport::TestCase context "instance methods" do setup do - @sam = Factory(:sam) + @sam = FactoryGirl.create(:sam) end should "be defined" do @@ -16,10 +16,10 @@ class ActsAsFollowableTest < ActiveSupport::TestCase context "acts_as_followable" do setup do - @sam = Factory(:sam) - @jon = Factory(:jon) - @oasis = Factory(:oasis) - @metallica = Factory(:metallica) + @sam = FactoryGirl.create(:sam) + @jon = FactoryGirl.create(:jon) + @oasis = FactoryGirl.create(:oasis) + @metallica = FactoryGirl.create(:metallica) @sam.follow(@jon) end @@ -30,7 +30,7 @@ class ActsAsFollowableTest < ActiveSupport::TestCase end should "return the proper number of multiple followers" do - @bob = Factory(:bob) + @bob = FactoryGirl.create(:bob) @sam.follow(@bob) assert_equal 0, @sam.followers_count assert_equal 1, @jon.followers_count @@ -45,7 +45,7 @@ class ActsAsFollowableTest < ActiveSupport::TestCase end should "return users (multiple followers)" do - @bob = Factory(:bob) + @bob = FactoryGirl.create(:bob) @sam.follow(@bob) assert_equal [], @sam.followers assert_equal [@sam], @jon.followers @@ -53,7 +53,7 @@ class ActsAsFollowableTest < ActiveSupport::TestCase end should "return users (multiple followers, complex)" do - @bob = Factory(:bob) + @bob = FactoryGirl.create(:bob) @sam.follow(@bob) @jon.follow(@bob) assert_equal [], @sam.followers @@ -62,7 +62,7 @@ class ActsAsFollowableTest < ActiveSupport::TestCase end should "accept AR options" do - @bob = Factory(:bob) + @bob = FactoryGirl.create(:bob) @bob.follow(@jon) assert_equal 1, @jon.followers(:limit => 1).count end @@ -86,7 +86,7 @@ class ActsAsFollowableTest < ActiveSupport::TestCase context "get follow record" do setup do - @bob = Factory(:bob) + @bob = FactoryGirl.create(:bob) @follow = @bob.follow(@sam) end @@ -101,7 +101,7 @@ class ActsAsFollowableTest < ActiveSupport::TestCase context "blocks" do setup do - @bob = Factory(:bob) + @bob = FactoryGirl.create(:bob) @jon.block(@sam) @jon.block(@bob) end @@ -158,6 +158,7 @@ class ActsAsFollowableTest < ActiveSupport::TestCase end should "be in the list of blocks" do + @sam.block(@jon) assert_equal [@jon], @sam.blocks end end diff --git a/test/acts_as_follower_test.rb b/test/acts_as_follower_test.rb index 01eed6d..d62ac3f 100644 --- a/test/acts_as_follower_test.rb +++ b/test/acts_as_follower_test.rb @@ -4,7 +4,7 @@ class ActsAsFollowerTest < ActiveSupport::TestCase context "instance methods" do setup do - @sam = Factory(:sam) + @sam = FactoryGirl.create(:sam) end should "be defined" do @@ -19,9 +19,9 @@ class ActsAsFollowerTest < ActiveSupport::TestCase context "acts_as_follower" do setup do - @sam = Factory(:sam) - @jon = Factory(:jon) - @oasis = Factory(:oasis) + @sam = FactoryGirl.create(:sam) + @jon = FactoryGirl.create(:jon) + @oasis = FactoryGirl.create(:oasis) @sam.follow(@jon) @sam.follow(@oasis) end @@ -83,8 +83,8 @@ class ActsAsFollowerTest < ActiveSupport::TestCase context "follows" do setup do - @band_follow = Follow.find(:first, :conditions => ["follower_id = ? and follower_type = 'User' and followable_id = ? and followable_type = 'Band'", @sam.id, @oasis.id]) - @user_follow = Follow.find(:first, :conditions => ["follower_id = ? and follower_type = 'User' and followable_id = ? and followable_type = 'User'", @sam.id, @jon.id]) + @band_follow = Follow.where("follower_id = ? and follower_type = 'User' and followable_id = ? and followable_type = 'Band'", @sam.id, @oasis.id).first + @user_follow = Follow.where("follower_id = ? and follower_type = 'User' and followable_id = ? and followable_type = 'User'", @sam.id, @jon.id).first end context "follows_by_type" do @@ -94,7 +94,7 @@ class ActsAsFollowerTest < ActiveSupport::TestCase end should "accept AR options" do - @metallica = Factory(:metallica) + @metallica = FactoryGirl.create(:metallica) @sam.follow(@metallica) assert_equal 1, @sam.follows_by_type('Band', :limit => 1).count end @@ -102,7 +102,7 @@ class ActsAsFollowerTest < ActiveSupport::TestCase context "following_by_type_count" do should "return the count of the requested type" do - @metallica = Factory(:metallica) + @metallica = FactoryGirl.create(:metallica) @sam.follow(@metallica) assert_equal 2, @sam.following_by_type_count('Band') assert_equal 1, @sam.following_by_type_count('User') @@ -146,7 +146,7 @@ class ActsAsFollowerTest < ActiveSupport::TestCase end should "accept AR options" do - @metallica = Factory(:metallica) + @metallica = FactoryGirl.create(:metallica) @sam.follow(@metallica) assert_equal 1, @sam.following_by_type('Band', :limit => 1).to_a.size end @@ -159,7 +159,7 @@ class ActsAsFollowerTest < ActiveSupport::TestCase end should "call following_by_type_count" do - @metallica = Factory(:metallica) + @metallica = FactoryGirl.create(:metallica) @sam.follow(@metallica) assert_equal 2, @sam.following_bands_count assert_equal 1, @sam.following_users_count diff --git a/test/dummy30/config/environments/test.rb b/test/dummy30/config/environments/test.rb index c89e813..770a6ea 100644 --- a/test/dummy30/config/environments/test.rb +++ b/test/dummy30/config/environments/test.rb @@ -7,9 +7,6 @@ # and recreated between test runs. Don't rely on the data there! config.cache_classes = true - # Log error messages when you accidentally call methods on nil. - config.whiny_nils = true - # Show full error reports and disable caching config.consider_all_requests_local = true diff --git a/test/factories/bands.rb b/test/factories/bands.rb index 4e7e5f9..3e6eb9b 100644 --- a/test/factories/bands.rb +++ b/test/factories/bands.rb @@ -1,7 +1,9 @@ -Factory.define :oasis, :class => Band do |b| - b.name 'Oasis' -end +FactoryGirl.define do + factory :oasis, :class => Band do |b| + b.name 'Oasis' + end -Factory.define :metallica, :class => Band do |b| - b.name 'Metallica' + factory :metallica, :class => Band do |b| + b.name 'Metallica' + end end diff --git a/test/factories/users.rb b/test/factories/users.rb index 3aef0c0..b819f61 100644 --- a/test/factories/users.rb +++ b/test/factories/users.rb @@ -1,11 +1,13 @@ -Factory.define :jon, :class => User do |u| - u.name 'Jon' -end +FactoryGirl.define do + factory :jon, class: User do |u| + u.name 'Jon' + end -Factory.define :sam, :class => User do |u| - u.name 'Sam' -end + factory :sam, :class => User do |u| + u.name 'Sam' + end -Factory.define :bob, :class => User do |u| - u.name 'Bob' + factory :bob, :class => User do |u| + u.name 'Bob' + end end diff --git a/test/test_helper.rb b/test/test_helper.rb index 3875d13..c26c441 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -12,6 +12,7 @@ require File.dirname(__FILE__) + '/../lib/generators/templates/model.rb' require 'shoulda' +require 'shoulda_create' require 'factory_girl' +ActiveSupport::TestCase.extend(ShouldaCreate) FactoryGirl.find_definitions -