has_followers 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.rspec +1 -0
- data/.travis.yml +14 -0
- data/Gemfile +11 -0
- data/Gemfile.lock +91 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +65 -0
- data/Rakefile +49 -0
- data/VERSION +1 -0
- data/app/models/follow.rb +6 -0
- data/has_followers.gemspec +56 -0
- data/lib/generators/has_followers.rb +19 -0
- data/lib/generators/has_followers/migration/migration_generator.rb +13 -0
- data/lib/generators/has_followers/migration/templates/create_has_followers_tables.rb +21 -0
- data/lib/has_followers.rb +4 -0
- data/lib/has_followers/engine.rb +8 -0
- data/lib/has_followers/has_followers.rb +72 -0
- data/spec/has_followers_spec.rb +137 -0
- data/spec/schema.rb +18 -0
- data/spec/spec_helper.rb +45 -0
- metadata +77 -0
data/.document
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
actionpack (3.1.3)
|
5
|
+
activemodel (= 3.1.3)
|
6
|
+
activesupport (= 3.1.3)
|
7
|
+
builder (~> 3.0.0)
|
8
|
+
erubis (~> 2.7.0)
|
9
|
+
i18n (~> 0.6)
|
10
|
+
rack (~> 1.3.5)
|
11
|
+
rack-cache (~> 1.1)
|
12
|
+
rack-mount (~> 0.8.2)
|
13
|
+
rack-test (~> 0.6.1)
|
14
|
+
sprockets (~> 2.0.3)
|
15
|
+
activemodel (3.1.3)
|
16
|
+
activesupport (= 3.1.3)
|
17
|
+
builder (~> 3.0.0)
|
18
|
+
i18n (~> 0.6)
|
19
|
+
activerecord (3.1.3)
|
20
|
+
activemodel (= 3.1.3)
|
21
|
+
activesupport (= 3.1.3)
|
22
|
+
arel (~> 2.2.1)
|
23
|
+
tzinfo (~> 0.3.29)
|
24
|
+
activesupport (3.1.3)
|
25
|
+
multi_json (~> 1.0)
|
26
|
+
arel (2.2.1)
|
27
|
+
builder (3.0.0)
|
28
|
+
diff-lcs (1.1.3)
|
29
|
+
erubis (2.7.0)
|
30
|
+
git (1.2.5)
|
31
|
+
hike (1.2.1)
|
32
|
+
i18n (0.6.0)
|
33
|
+
jeweler (1.6.4)
|
34
|
+
bundler (~> 1.0)
|
35
|
+
git (>= 1.2.5)
|
36
|
+
rake
|
37
|
+
json (1.6.5)
|
38
|
+
multi_json (1.0.4)
|
39
|
+
rack (1.3.6)
|
40
|
+
rack-cache (1.1)
|
41
|
+
rack (>= 0.4)
|
42
|
+
rack-mount (0.8.3)
|
43
|
+
rack (>= 1.0.0)
|
44
|
+
rack-ssl (1.3.2)
|
45
|
+
rack
|
46
|
+
rack-test (0.6.1)
|
47
|
+
rack (>= 1.0)
|
48
|
+
railties (3.1.3)
|
49
|
+
actionpack (= 3.1.3)
|
50
|
+
activesupport (= 3.1.3)
|
51
|
+
rack-ssl (~> 1.3.2)
|
52
|
+
rake (>= 0.8.7)
|
53
|
+
rdoc (~> 3.4)
|
54
|
+
thor (~> 0.14.6)
|
55
|
+
rake (0.9.2.2)
|
56
|
+
rcov (0.9.11)
|
57
|
+
rdoc (3.12)
|
58
|
+
json (~> 1.4)
|
59
|
+
rspec (2.8.0)
|
60
|
+
rspec-core (~> 2.8.0)
|
61
|
+
rspec-expectations (~> 2.8.0)
|
62
|
+
rspec-mocks (~> 2.8.0)
|
63
|
+
rspec-core (2.8.0)
|
64
|
+
rspec-expectations (2.8.0)
|
65
|
+
diff-lcs (~> 1.1.2)
|
66
|
+
rspec-mocks (2.8.0)
|
67
|
+
rspec-rails (2.8.1)
|
68
|
+
actionpack (>= 3.0)
|
69
|
+
activesupport (>= 3.0)
|
70
|
+
railties (>= 3.0)
|
71
|
+
rspec (~> 2.8.0)
|
72
|
+
sprockets (2.0.3)
|
73
|
+
hike (~> 1.2)
|
74
|
+
rack (~> 1.0)
|
75
|
+
tilt (~> 1.1, != 1.3.0)
|
76
|
+
sqlite3 (1.3.5)
|
77
|
+
thor (0.14.6)
|
78
|
+
tilt (1.3.3)
|
79
|
+
tzinfo (0.3.31)
|
80
|
+
|
81
|
+
PLATFORMS
|
82
|
+
ruby
|
83
|
+
|
84
|
+
DEPENDENCIES
|
85
|
+
activerecord (~> 3.1.3)
|
86
|
+
bundler (~> 1.0.0)
|
87
|
+
jeweler (~> 1.6.4)
|
88
|
+
rcov
|
89
|
+
rspec
|
90
|
+
rspec-rails (~> 2.4)
|
91
|
+
sqlite3
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2012 Fabiano Almeida
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
= has_followers {<img src="https://secure.travis-ci.org/fabianoalmeida/has_followers.png" />}[http://travis-ci.org/fabianoalmeida/has_followers]
|
2
|
+
|
3
|
+
*ATTENTION:* This is a Rails 3 implementation.
|
4
|
+
|
5
|
+
NOTE: It's necessary there's a *User* model. When is executing the migration this gem will create two columns in users' table: <code>followeds_count</code> and <code>followers_count</code>.
|
6
|
+
|
7
|
+
== Instalation
|
8
|
+
* The first step is add this gem to your Gemfile <code>gem 'has_followers'</code>
|
9
|
+
* After that, install the gem in your development environment <code>bundle install</code>
|
10
|
+
* Execute the migration generator to create tables and columns necessary <code>rails g has_followers:migration</code>
|
11
|
+
* Run the migrations with <code>rake db:migrate</code>
|
12
|
+
|
13
|
+
== Usage
|
14
|
+
It's very simple to use this gem. See the examples below.
|
15
|
+
|
16
|
+
To follow someone you need only do this
|
17
|
+
you.follow(someone)
|
18
|
+
This action automatically increment your <code>followeds_count</code> column and his (someone) <code>followers_count</code> column too.
|
19
|
+
|
20
|
+
Cause you want to unfollow someone, no problem. It's similar to follow someone
|
21
|
+
you.unfollow(someone)
|
22
|
+
When you <code>unfollow</code> someone, this automatically decrement your <code>followeds_count</code> column and his (someone) <code>followers_count</code> column too.
|
23
|
+
|
24
|
+
You can check how many users you is following
|
25
|
+
you.followeds_count
|
26
|
+
|
27
|
+
Or how many users are following you
|
28
|
+
you.follwers_count
|
29
|
+
|
30
|
+
If you want to know who are your followeds users
|
31
|
+
you.followeds
|
32
|
+
|
33
|
+
And your followers
|
34
|
+
you.followers
|
35
|
+
|
36
|
+
Check if the user is you
|
37
|
+
you.is?(someone) # => true if someone is you
|
38
|
+
|
39
|
+
If you want to know if you is following someone
|
40
|
+
you.following?(someone) # => Follow object if you already following someone
|
41
|
+
|
42
|
+
There's a user that you want to know whether he is following you
|
43
|
+
you.follow_me?(someone) # => Follow object if he is following you
|
44
|
+
|
45
|
+
Or cause you want to know which relationship between you and your followed or follower
|
46
|
+
you.following_for(someone)
|
47
|
+
Or
|
48
|
+
you.followed_for(someone)
|
49
|
+
|
50
|
+
The first one will finding on your followeds list, however the second one will find on your followers list.
|
51
|
+
|
52
|
+
== Contributing to has_followers
|
53
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
54
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
55
|
+
* Fork the project
|
56
|
+
* Start a feature/bugfix branch
|
57
|
+
* Commit and push until you are happy with your contribution
|
58
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
59
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
60
|
+
|
61
|
+
== Copyright
|
62
|
+
|
63
|
+
Copyright (c) 2012 Fabiano Almeida. See LICENSE.txt for
|
64
|
+
further details.
|
65
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "has_followers"
|
18
|
+
gem.homepage = "http://github.com/fabianoalmeida/has_followers"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = "Add followers support to Rails 3 apps"
|
21
|
+
gem.description = "Add followers support to Rails 3 apps"
|
22
|
+
gem.email = "fabianoalmeida@gmail.com"
|
23
|
+
gem.authors = ["Fabiano Almeida"]
|
24
|
+
# dependencies defined in Gemfile
|
25
|
+
end
|
26
|
+
Jeweler::RubygemsDotOrgTasks.new
|
27
|
+
|
28
|
+
require 'rspec/core'
|
29
|
+
require 'rspec/core/rake_task'
|
30
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
31
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
32
|
+
end
|
33
|
+
|
34
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
35
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
36
|
+
spec.rcov = true
|
37
|
+
end
|
38
|
+
|
39
|
+
task :default => :spec
|
40
|
+
|
41
|
+
require 'rake/rdoctask'
|
42
|
+
Rake::RDocTask.new do |rdoc|
|
43
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
44
|
+
|
45
|
+
rdoc.rdoc_dir = 'rdoc'
|
46
|
+
rdoc.title = "has_followers #{version}"
|
47
|
+
rdoc.rdoc_files.include('README*')
|
48
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
49
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
@@ -0,0 +1,56 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{has_followers}
|
8
|
+
s.version = "0.0.1"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = [%q{Fabiano Almeida}]
|
12
|
+
s.date = %q{2012-01-24}
|
13
|
+
s.description = %q{Add followers support to Rails 3 apps}
|
14
|
+
s.email = %q{fabianoalmeida@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE.txt",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".rspec",
|
22
|
+
".travis.yml",
|
23
|
+
"Gemfile",
|
24
|
+
"Gemfile.lock",
|
25
|
+
"LICENSE.txt",
|
26
|
+
"README.rdoc",
|
27
|
+
"Rakefile",
|
28
|
+
"VERSION",
|
29
|
+
"app/models/follow.rb",
|
30
|
+
"has_followers.gemspec",
|
31
|
+
"lib/generators/has_followers.rb",
|
32
|
+
"lib/generators/has_followers/migration/migration_generator.rb",
|
33
|
+
"lib/generators/has_followers/migration/templates/create_has_followers_tables.rb",
|
34
|
+
"lib/has_followers.rb",
|
35
|
+
"lib/has_followers/engine.rb",
|
36
|
+
"lib/has_followers/has_followers.rb",
|
37
|
+
"spec/has_followers_spec.rb",
|
38
|
+
"spec/schema.rb",
|
39
|
+
"spec/spec_helper.rb"
|
40
|
+
]
|
41
|
+
s.homepage = %q{http://github.com/fabianoalmeida/has_followers}
|
42
|
+
s.licenses = [%q{MIT}]
|
43
|
+
s.require_paths = [%q{lib}]
|
44
|
+
s.rubygems_version = %q{1.8.9}
|
45
|
+
s.summary = %q{Add followers support to Rails 3 apps}
|
46
|
+
|
47
|
+
if s.respond_to? :specification_version then
|
48
|
+
s.specification_version = 3
|
49
|
+
|
50
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
51
|
+
else
|
52
|
+
end
|
53
|
+
else
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'rails/generators/migration'
|
2
|
+
|
3
|
+
module HasFollowers
|
4
|
+
module Generators
|
5
|
+
class Base < Rails::Generators::Base
|
6
|
+
|
7
|
+
include Rails::Generators::Migration
|
8
|
+
|
9
|
+
def self.source_root
|
10
|
+
@_has_followers_source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'has_followers', generator_name, 'templates'))
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.next_migration_number(path)
|
14
|
+
Time.now.utc.strftime("%Y%m%d%H%M%S")
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'generators/has_followers'
|
2
|
+
|
3
|
+
module HasFollowers
|
4
|
+
module Generators
|
5
|
+
class MigrationGenerator < Base
|
6
|
+
desc "Generate has_followers migration"
|
7
|
+
|
8
|
+
def create_migration_file
|
9
|
+
migration_template "create_has_followers_tables.rb", File.join('db', 'migrate', "create_has_followers_tables.rb")
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
class CreateHasFollowersTables < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
create_table :follows, :force => true do |t|
|
4
|
+
t.references :followed, :user
|
5
|
+
t.timestamps
|
6
|
+
end
|
7
|
+
|
8
|
+
add_index :follows, :followed_id
|
9
|
+
add_index :follows, :user_id
|
10
|
+
|
11
|
+
add_column :users, :followers_count, :integer, :default => 0, :null => false
|
12
|
+
add_column :users, :followeds_count, :integer, :default => 0, :null => false
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.down
|
16
|
+
remove_column :users, :followers_count
|
17
|
+
remove_column :users, :followeds_count
|
18
|
+
|
19
|
+
drop_table :follows
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
module Has
|
2
|
+
module Followers
|
3
|
+
def self.included(base)
|
4
|
+
base.extend Has::Followers::ClassMethods
|
5
|
+
end
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
def has_followers
|
9
|
+
include Has::Followers::InstanceMethods
|
10
|
+
|
11
|
+
has_many :followed_to, :class_name => 'Follow', :foreign_key => 'user_id'
|
12
|
+
has_many :follower_to, :class_name => 'Follow', :foreign_key => 'followed_id'
|
13
|
+
has_many :followeds, :through => :followed_to, :source => :followed
|
14
|
+
has_many :followers, :through => :follower_to, :source => :follower
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
module InstanceMethods
|
19
|
+
def follow(followed)
|
20
|
+
unless following?(followed) || is?(followed)
|
21
|
+
ActiveRecord::Base.transaction do
|
22
|
+
Follow.create(:user_id => self.id, :followed_id => followed.id)
|
23
|
+
increment_counters(followed)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def unfollow(followed)
|
29
|
+
if following?(followed) && ! is?(followed)
|
30
|
+
ActiveRecord::Base.transaction do
|
31
|
+
follow = following_for(followed)
|
32
|
+
unless follow.nil?
|
33
|
+
follow.destroy
|
34
|
+
decrement_counters(followed)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def following?(followed)
|
41
|
+
return follow = following_for(followed)
|
42
|
+
end
|
43
|
+
|
44
|
+
def following_for(followed)
|
45
|
+
self.followeds.where( :id => followed.id ).first
|
46
|
+
end
|
47
|
+
|
48
|
+
def follow_me?(follower)
|
49
|
+
return follow = followed_for(follower)
|
50
|
+
end
|
51
|
+
|
52
|
+
def followed_for(follower)
|
53
|
+
self.followers.where( :id => follower.id ).first
|
54
|
+
end
|
55
|
+
|
56
|
+
def is?(followed)
|
57
|
+
self.id.eql?( followed.id )
|
58
|
+
end
|
59
|
+
|
60
|
+
private
|
61
|
+
def increment_counters(follower)
|
62
|
+
self.update_attributes( { :followeds_count => self.followeds_count + 1 } )
|
63
|
+
follower.update_attributes( { :followers_count => follower.followers_count + 1 } )
|
64
|
+
end
|
65
|
+
|
66
|
+
def decrement_counters(followed)
|
67
|
+
self.update_attributes( { :followeds_count => self.followeds_count - 1 } )
|
68
|
+
followed.update_attributes( { :followers_count => followed.followers_count - 1 } )
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,137 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe "HasFollowers" do
|
4
|
+
before (:each) do
|
5
|
+
User.delete_all
|
6
|
+
|
7
|
+
@josh = User.create(:login => "josh")
|
8
|
+
@stuart = User.create(:login => "stuart")
|
9
|
+
@kevin = User.create(:login => "kevin")
|
10
|
+
@bill = User.create(:login => "bill")
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "methods" do
|
14
|
+
it "should respond to has_followers method" do
|
15
|
+
User.should respond_to(:has_followers)
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should respond to follow method" do
|
19
|
+
@josh.should respond_to(:follow)
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should respond to unfollow method" do
|
23
|
+
@stuart.should respond_to(:unfollow)
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should respond to following? method" do
|
27
|
+
@kevin.should respond_to(:following?)
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should respond to following_for method" do
|
31
|
+
@bill.should respond_to(:following_for)
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should respond to follow_me? method" do
|
35
|
+
@josh.should respond_to(:follow_me?)
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should respond to followed_for method" do
|
39
|
+
@stuart.should respond_to(:followed_for)
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should respond to followeds method" do
|
43
|
+
@bill.should respond_to(:followeds)
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should respond to followers method" do
|
47
|
+
@josh.should respond_to(:followers)
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should respond to is? method" do
|
51
|
+
@stuart.should respond_to(:is?)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
describe "user" do
|
56
|
+
before (:each) do
|
57
|
+
@josh.follow(@stuart)
|
58
|
+
@josh.follow(@kevin)
|
59
|
+
@kevin.follow(@bill)
|
60
|
+
@stuart.follow(@josh)
|
61
|
+
end
|
62
|
+
|
63
|
+
describe "'josh'" do
|
64
|
+
it "should be yourself" do
|
65
|
+
@josh.is?(@josh).should be_true
|
66
|
+
end
|
67
|
+
|
68
|
+
it "should not can follow yourself" do
|
69
|
+
@josh.follow(@josh).should be_false
|
70
|
+
end
|
71
|
+
|
72
|
+
it "should have followeds count equals two" do
|
73
|
+
@josh.followeds_count.should be(2)
|
74
|
+
end
|
75
|
+
|
76
|
+
it "should have followers count equals zero" do
|
77
|
+
@josh.followers_count.should be(1)
|
78
|
+
end
|
79
|
+
|
80
|
+
it "should have two followeds" do
|
81
|
+
@josh.followeds.should have(2).items
|
82
|
+
end
|
83
|
+
|
84
|
+
it "should have empty followers" do
|
85
|
+
@josh.followers.should have(1).items
|
86
|
+
end
|
87
|
+
|
88
|
+
it "should be following 'stuart'" do
|
89
|
+
@josh.following?(@stuart).should be_true
|
90
|
+
end
|
91
|
+
|
92
|
+
it "should not be following 'bill'" do
|
93
|
+
@josh.following?(@bill).should be_false
|
94
|
+
end
|
95
|
+
|
96
|
+
it "should be been followed for 'stuart'" do
|
97
|
+
@josh.followed_for(@stuart).should_not be_nil
|
98
|
+
end
|
99
|
+
|
100
|
+
it "should be been followed for 'stuart'" do
|
101
|
+
@josh.follow_me?(@stuart).should be_true
|
102
|
+
end
|
103
|
+
|
104
|
+
it "should not be been followed for 'kevin'" do
|
105
|
+
@josh.follow_me?(@kevin).should be_false
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
describe "'kevin'" do
|
110
|
+
describe "unfollowing 'bill'" do
|
111
|
+
before (:each) do
|
112
|
+
@kevin.unfollow(@bill)
|
113
|
+
end
|
114
|
+
|
115
|
+
after (:each) do
|
116
|
+
@kevin.follow(@bill)
|
117
|
+
end
|
118
|
+
|
119
|
+
it "should have no followeds" do
|
120
|
+
@kevin.followeds.should have(0).items
|
121
|
+
end
|
122
|
+
|
123
|
+
it "should have no followers" do
|
124
|
+
@kevin.followers.should have(1).items
|
125
|
+
end
|
126
|
+
|
127
|
+
it "should have followers count equals zero" do
|
128
|
+
@kevin.followers_count.should be(1)
|
129
|
+
end
|
130
|
+
|
131
|
+
it "should have followers count equals zero" do
|
132
|
+
@kevin.followeds_count.should be(0)
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
data/spec/schema.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
ActiveRecord::Schema.define(:version => 0) do
|
2
|
+
begin
|
3
|
+
drop_table :users
|
4
|
+
drop_table :follows
|
5
|
+
rescue
|
6
|
+
end
|
7
|
+
|
8
|
+
create_table :users do |t|
|
9
|
+
t.string :login
|
10
|
+
t.integer :followeds_count, :default => 0, :null => false
|
11
|
+
t.integer :followers_count, :default => 0, :null => false
|
12
|
+
end
|
13
|
+
|
14
|
+
create_table :follows do |t|
|
15
|
+
t.references :followed, :user
|
16
|
+
t.timestamps
|
17
|
+
end
|
18
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
require 'rspec'
|
4
|
+
require 'active_record'
|
5
|
+
require 'has_followers'
|
6
|
+
|
7
|
+
ActiveRecord::Base.configurations = {'test' => {:adapter => 'sqlite3', :database => ":memory:"}}
|
8
|
+
ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations["test"])
|
9
|
+
|
10
|
+
load(File.join(File.dirname(__FILE__), 'schema.rb'))
|
11
|
+
|
12
|
+
RSpec.configure do |config|
|
13
|
+
config.mock_with :rspec
|
14
|
+
end
|
15
|
+
|
16
|
+
class Object
|
17
|
+
def self.unset_class(*args)
|
18
|
+
class_eval do
|
19
|
+
args.each do |klass|
|
20
|
+
eval(klass) rescue nil
|
21
|
+
remove_const(klass) if const_defined?(klass)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
alias :doing :lambda
|
28
|
+
|
29
|
+
# unset models used for testing purposes
|
30
|
+
Object.unset_class('User')
|
31
|
+
|
32
|
+
class User < ActiveRecord::Base
|
33
|
+
has_followers
|
34
|
+
end
|
35
|
+
|
36
|
+
class Follow < ActiveRecord::Base
|
37
|
+
belongs_to :follower, :class_name => "User", :foreign_key => "user_id"
|
38
|
+
belongs_to :followed, :class_name => "User", :foreign_key => "followed_id"
|
39
|
+
|
40
|
+
# callback
|
41
|
+
after_destroy do |f|
|
42
|
+
User.decrement_counter(:followeds_count, f.user_id)
|
43
|
+
User.decrement_counter(:followers_count, f.follower_id)
|
44
|
+
end
|
45
|
+
end
|
metadata
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: has_followers
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Fabiano Almeida
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2012-01-24 00:00:00 Z
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Add followers support to Rails 3 apps
|
17
|
+
email: fabianoalmeida@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- LICENSE.txt
|
24
|
+
- README.rdoc
|
25
|
+
files:
|
26
|
+
- .document
|
27
|
+
- .rspec
|
28
|
+
- .travis.yml
|
29
|
+
- Gemfile
|
30
|
+
- Gemfile.lock
|
31
|
+
- LICENSE.txt
|
32
|
+
- README.rdoc
|
33
|
+
- Rakefile
|
34
|
+
- VERSION
|
35
|
+
- app/models/follow.rb
|
36
|
+
- has_followers.gemspec
|
37
|
+
- lib/generators/has_followers.rb
|
38
|
+
- lib/generators/has_followers/migration/migration_generator.rb
|
39
|
+
- lib/generators/has_followers/migration/templates/create_has_followers_tables.rb
|
40
|
+
- lib/has_followers.rb
|
41
|
+
- lib/has_followers/engine.rb
|
42
|
+
- lib/has_followers/has_followers.rb
|
43
|
+
- spec/has_followers_spec.rb
|
44
|
+
- spec/schema.rb
|
45
|
+
- spec/spec_helper.rb
|
46
|
+
homepage: http://github.com/fabianoalmeida/has_followers
|
47
|
+
licenses:
|
48
|
+
- MIT
|
49
|
+
post_install_message:
|
50
|
+
rdoc_options: []
|
51
|
+
|
52
|
+
require_paths:
|
53
|
+
- lib
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
hash: 4085056179445136315
|
60
|
+
segments:
|
61
|
+
- 0
|
62
|
+
version: "0"
|
63
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
|
+
none: false
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: "0"
|
69
|
+
requirements: []
|
70
|
+
|
71
|
+
rubyforge_project:
|
72
|
+
rubygems_version: 1.8.9
|
73
|
+
signing_key:
|
74
|
+
specification_version: 3
|
75
|
+
summary: Add followers support to Rails 3 apps
|
76
|
+
test_files: []
|
77
|
+
|