Commit 03a58f530ab993f24f33a733d08f16598d8b7fbc

Authored by Douglas F Shearer
1 parent b96b63ef

Added remaining test files.

Added rcov rake tasks.
.gitignore
1   -test/debug.log
2 1 \ No newline at end of file
  2 +test/debug.log
  3 +coverage
3 4 \ No newline at end of file
... ...
Rakefile
... ... @@ -20,3 +20,17 @@ Rake::RDocTask.new(:rdoc) do |rdoc|
20 20 rdoc.rdoc_files.include('README')
21 21 rdoc.rdoc_files.include('lib/**/*.rb')
22 22 end
  23 +
  24 +namespace :rcov do
  25 +
  26 + desc "Generate a coverage report in coverage/"
  27 + task :gen do
  28 + sh "rcov --output coverage test/*_test.rb"
  29 + end
  30 +
  31 + desc "Remove generated coverage files."
  32 + task :clobber do
  33 + sh "rm -rdf coverage"
  34 + end
  35 +
  36 +end
23 37 \ No newline at end of file
... ...
init.rb
1   -require 'acts_as_follower'
2   -require 'acts_as_followable'
3   -require 'follow.rb'
  1 +require 'lib/acts_as_follower'
  2 +require 'lib/acts_as_followable'
  3 +require 'lib/follow'
4 4  
5 5 ActiveRecord::Base.send(:include, ActiveRecord::Acts::Follower)
6 6 ActiveRecord::Base.send(:include, ActiveRecord::Acts::Followable)
... ...
lib/acts_as_follower.rb
... ... @@ -10,18 +10,13 @@ module ActiveRecord
10 10 def acts_as_follower
11 11 has_many :follows, :as => :follower, :dependent => :nullify # If a following entity is deleted, keep the follows.
12 12 include ActiveRecord::Acts::Follower::InstanceMethods
13   - extend ActiveRecord::Acts::Follower::SingletonMethods
14 13 end
15 14 end
16 15  
17   - # This module contains class methods
18   - module SingletonMethods
19   -
20   - end
21   -
22 16 # This module contains instance methods
23 17 module InstanceMethods
24 18  
  19 + # Returns true if this instance is following the object passed as an argument.
25 20 def following?(followable)
26 21 0 < Follow.count(:all, :conditions => [
27 22 "follower_id = ? AND follower_type = ? AND followable_id = ? AND followable_type = ?",
... ... @@ -29,10 +24,13 @@ module ActiveRecord
29 24 ])
30 25 end
31 26  
  27 + # Returns the number of objects this instance is following.
32 28 def follow_count
33 29 Follow.count(:all, :conditions => ["follower_id = ? AND follower_type = ?", self.id, self.class.name])
34 30 end
35 31  
  32 + # Creates a new follow record for this instance to follow the passed object.
  33 + # Does not allow duplicate records to be created.
36 34 def follow(followable)
37 35 follow = get_follow(followable)
38 36 unless follow
... ... @@ -40,6 +38,7 @@ module ActiveRecord
40 38 end
41 39 end
42 40  
  41 + # Deletes the follow record if it exists.
43 42 def stop_following(followable)
44 43 follow = get_follow(followable)
45 44 if follow
... ... @@ -47,16 +46,19 @@ module ActiveRecord
47 46 end
48 47 end
49 48  
  49 + # Returns the follow records related to this instance by type.
50 50 def follows_by_type(followable_type)
51 51 Follow.find(:all, :conditions => ["follower_id = ? AND follower_type = ? AND followable_type = ?", self.id, self.class.name, followable_type])
52 52 end
53 53  
  54 + # Returns the follow records related to this instance by type.
54 55 def all_follows
55 56 Follow.find(:all, :conditions => ["follower_id = ? AND follower_type = ?", self.id, self.class.name])
56 57 end
57 58  
58 59 private
59 60  
  61 + # Returns a follow record for the current instance and followable object.
60 62 def get_follow(followable)
61 63 Follow.find(:first, :conditions => ["follower_id = ? AND follower_type = ? AND followable_id = ? AND followable_type = ?", self.id, self.class.name, followable.id, followable.class.name])
62 64 end
... ...
test/acts_as_follower_test.rb 0 โ†’ 100644
... ... @@ -0,0 +1,55 @@
  1 +require File.dirname(__FILE__) + '/test_helper'
  2 +
  3 +class ActsAsFollowerTest < Test::Unit::TestCase
  4 + fixtures :users, :follows
  5 +
  6 + def test_instance_methods_should_be_defined
  7 + assert users(:sam).respond_to?(:following?)
  8 + assert users(:sam).respond_to?(:follow_count)
  9 + assert users(:sam).respond_to?(:follow)
  10 + assert users(:sam).respond_to?(:stop_following)
  11 + assert users(:sam).respond_to?(:follows_by_type)
  12 + assert users(:sam).respond_to?(:all_follows)
  13 + end
  14 +
  15 + def test_following_should_returns_following_status
  16 + assert_equal true, users(:sam).following?(users(:jon))
  17 + assert_equal false, users(:jon).following?(users(:sam))
  18 + end
  19 +
  20 + def test_follow_count_should_return_count_of_followed_users
  21 + assert_equal 2, users(:sam).follow_count
  22 + assert_equal 0, users(:jon).follow_count
  23 + end
  24 +
  25 + def test_follow_should_create_relevant_follow_record
  26 + assert_difference "Follow.count", 1 do
  27 + assert_difference "users(:jon).follow_count", 1 do
  28 + users(:jon).follow(users(:sam))
  29 + end
  30 + end
  31 + assert_equal users(:jon), Follow.last.follower
  32 + assert_equal users(:sam), Follow.last.followable
  33 + end
  34 +
  35 + def test_stop_following_should_create_relevant_follow_record
  36 + assert_difference "Follow.count", -1 do
  37 + assert_difference "users(:sam).follow_count", -1 do
  38 + users(:sam).stop_following(users(:jon))
  39 + end
  40 + end
  41 + end
  42 +
  43 + def test_follows_by_type_should_return_only_requested_records
  44 + assert_equal [follows(:band)], users(:sam).follows_by_type('Band')
  45 + assert_equal [follows(:user)], users(:sam).follows_by_type('User')
  46 + end
  47 +
  48 + def test_all_follows_should_return_all_folows
  49 + follows = users(:sam).all_follows
  50 + assert_equal 2, follows.size
  51 + assert follows.include?(follows(:band))
  52 + assert follows.include?(follows(:user))
  53 + end
  54 +
  55 +end
... ...
test/fixtures/band.rb 0 โ†’ 100644
... ... @@ -0,0 +1,4 @@
  1 +class Band < ActiveRecord::Base
  2 + validates_presence_of :name
  3 + acts_as_followable
  4 +end
... ...
test/fixtures/bands.yml 0 โ†’ 100644
... ... @@ -0,0 +1,3 @@
  1 +oasis:
  2 + id: 1
  3 + name: Oasis
0 4 \ No newline at end of file
... ...
test/fixtures/follows.yml
1   -one:
  1 +user:
2 2 id: 1
3 3 followable_id: 1
4 4 followable_type: User
5 5 follower_id: 2
  6 + follower_type: User
  7 +
  8 +band:
  9 + id: 2
  10 + followable_id: 1
  11 + followable_type: Band
  12 + follower_id: 2
6 13 follower_type: User
7 14 \ No newline at end of file
... ...
test/follow_test.rb 0 โ†’ 100644
... ... @@ -0,0 +1,10 @@
  1 +require File.dirname(__FILE__) + '/test_helper'
  2 +
  3 +class FollowTest < Test::Unit::TestCase
  4 +
  5 + # Replace with real tests
  6 + def test_assert_true_should_be_true
  7 + assert true
  8 + end
  9 +
  10 +end
... ...
test/schema.rb
... ... @@ -5,6 +5,8 @@ ActiveRecord::Schema.define :version =&gt; 0 do
5 5 t.string "followable_type", :null => false
6 6 t.integer "follower_id", :null => false
7 7 t.string "follower_type", :null => false
  8 + t.datetime "created_at"
  9 + t.datetime "updated_at"
8 10 end
9 11  
10 12 create_table :users, :force => true do |t|
... ...