Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update the code to work with Rails 4. #32

Merged
merged 3 commits into from
Sep 15, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions acts_as_follower.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 2 additions & 1 deletion lib/acts_as_follower/follow_scopes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
22 changes: 15 additions & 7 deletions lib/acts_as_follower/followable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down Expand Up @@ -59,19 +59,27 @@ def blocked_followers_count
self.followings.blocked.count
end

# Returns the following records.
# Returns the followings records scoped
def followers_scoped
self.followings.includes(:follower)
end

def followers(options={})
self.followings.unblocked.includes(:follower).all(options).collect{|f| f.follower}
followers_scope = followers_scoped.unblocked
followers_scope = apply_options_to_scope(followers_scope, options)
followers_scope.to_a.collect{|f| f.follower}
end

def blocks(options={})
self.followings.blocked.includes(:follower).all(options).collect{|f| f.follower}
blocked_followers_scope = followers_scoped.blocked
blocked_followers_scope = apply_options_to_scope(blocked_followers_scope, options)
blocked_followers_scope.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)
Expand All @@ -89,7 +97,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)
Expand Down
17 changes: 12 additions & 5 deletions lib/acts_as_follower/follower.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -40,14 +40,21 @@ def stop_following(followable)
end
end

# returns the follows records to the current instance
def follows_scoped
self.follows.unblocked.includes(:followable)
end

# 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)
follows_scope = follows_scoped.for_followable_type(followable_type)
follows_scope = apply_options_to_scope(follows_scope, options)
end

# Returns the follow records related to this instance with the followable included.
def all_follows(options={})
self.follows.unblocked.includes(:followable).all(options)
follows_scope = follows_scoped
follows_scope = apply_options_to_scope(follows_scope, options)
end

# Returns the actual records which this instance is following.
Expand All @@ -60,8 +67,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])
Expand Down
18 changes: 18 additions & 0 deletions lib/acts_as_follower/follower_lib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,23 @@ def parent_class_name(obj)
return obj.class.name
end

def apply_options_to_scope(scope, options = {})
if options.has_key?(:limit)
scope = scope.limit(options[:limit])
end
if options.has_key?(:includes)
scope = scope.includes(options[:includes])
end
if options.has_key?(:joins)
scope = scope.joins(options[:joins])
end
if options.has_key?(:where)
scope = scope.order(options[:where])
end
if options.has_key?(:order)
scope = scope.order(options[:order])
end
scope
end
end
end
22 changes: 11 additions & 11 deletions test/acts_as_followable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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
Expand All @@ -45,15 +45,15 @@ 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
assert_equal [@sam], @bob.followers
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
Expand All @@ -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
Expand All @@ -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

Expand All @@ -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
Expand Down
20 changes: 10 additions & 10 deletions test/acts_as_follower_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -94,15 +94,15 @@ 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
end

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')
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
3 changes: 0 additions & 3 deletions test/dummy30/config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 7 additions & 5 deletions test/factories/bands.rb
Original file line number Diff line number Diff line change
@@ -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
18 changes: 10 additions & 8 deletions test/factories/users.rb
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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