acts_as_follower 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +8 -0
- data/Gemfile +4 -0
- data/MIT-LICENSE +44 -0
- data/README.rdoc +208 -0
- data/Rakefile +39 -0
- data/acts_as_follower.gemspec +25 -0
- data/init.rb +1 -0
- data/lib/acts_as_follower.rb +11 -0
- data/lib/acts_as_follower/followable.rb +102 -0
- data/lib/acts_as_follower/follower.rb +100 -0
- data/lib/acts_as_follower/follower_lib.rb +15 -0
- data/lib/acts_as_follower/railtie.rb +15 -0
- data/lib/acts_as_follower/version.rb +3 -0
- data/lib/generators/USAGE +5 -0
- data/lib/generators/acts_as_follower_generator.rb +30 -0
- data/lib/generators/templates/migration.rb +17 -0
- data/lib/generators/templates/model.rb +21 -0
- data/test/README +24 -0
- data/test/acts_as_followable_test.rb +241 -0
- data/test/acts_as_follower_test.rb +209 -0
- data/test/dummy30/Gemfile +1 -0
- data/test/dummy30/Rakefile +7 -0
- data/test/dummy30/app/models/band.rb +4 -0
- data/test/dummy30/app/models/user.rb +5 -0
- data/test/dummy30/config.ru +4 -0
- data/test/dummy30/config/application.rb +42 -0
- data/test/dummy30/config/boot.rb +10 -0
- data/test/dummy30/config/database.yml +7 -0
- data/test/dummy30/config/environment.rb +5 -0
- data/test/dummy30/config/environments/development.rb +19 -0
- data/test/dummy30/config/environments/test.rb +23 -0
- data/test/dummy30/config/initializers/backtrace_silencers.rb +7 -0
- data/test/dummy30/config/initializers/inflections.rb +10 -0
- data/test/dummy30/config/initializers/secret_token.rb +7 -0
- data/test/dummy30/config/initializers/session_store.rb +8 -0
- data/test/dummy30/config/locales/en.yml +5 -0
- data/test/dummy30/config/routes.rb +2 -0
- data/test/factories/bands.rb +7 -0
- data/test/factories/users.rb +11 -0
- data/test/follow_test.rb +10 -0
- data/test/schema.rb +21 -0
- data/test/test_helper.rb +17 -0
- metadata +190 -0
@@ -0,0 +1,209 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
|
3
|
+
class ActsAsFollowerTest < ActiveSupport::TestCase
|
4
|
+
|
5
|
+
context "instance methods" do
|
6
|
+
setup do
|
7
|
+
@sam = Factory(:sam)
|
8
|
+
end
|
9
|
+
|
10
|
+
should "be defined" do
|
11
|
+
assert @sam.respond_to?(:following?)
|
12
|
+
assert @sam.respond_to?(:follow_count)
|
13
|
+
assert @sam.respond_to?(:follow)
|
14
|
+
assert @sam.respond_to?(:stop_following)
|
15
|
+
assert @sam.respond_to?(:follows_by_type)
|
16
|
+
assert @sam.respond_to?(:all_follows)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
context "acts_as_follower" do
|
21
|
+
setup do
|
22
|
+
@sam = Factory(:sam)
|
23
|
+
@jon = Factory(:jon)
|
24
|
+
@oasis = Factory(:oasis)
|
25
|
+
@sam.follow(@jon)
|
26
|
+
@sam.follow(@oasis)
|
27
|
+
end
|
28
|
+
|
29
|
+
context "following" do
|
30
|
+
should "return following_status" do
|
31
|
+
assert_equal true, @sam.following?(@jon)
|
32
|
+
assert_equal false, @jon.following?(@sam)
|
33
|
+
end
|
34
|
+
|
35
|
+
should "return follow_count" do
|
36
|
+
assert_equal 2, @sam.follow_count
|
37
|
+
assert_equal 0, @jon.follow_count
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
context "follow a friend" do
|
42
|
+
setup do
|
43
|
+
@jon.follow(@sam)
|
44
|
+
end
|
45
|
+
|
46
|
+
should_change("Follow count", :by => 1) { Follow.count }
|
47
|
+
should_change("@jon.follow_count", :by => 1) { @jon.follow_count }
|
48
|
+
|
49
|
+
should "set the follower" do
|
50
|
+
assert_equal @jon, Follow.last.follower
|
51
|
+
end
|
52
|
+
|
53
|
+
should "set the followable" do
|
54
|
+
assert_equal @sam, Follow.last.followable
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
context "follow yourself" do
|
59
|
+
setup do
|
60
|
+
@jon.follow(@jon)
|
61
|
+
end
|
62
|
+
|
63
|
+
should_not_change("Follow count") { Follow.count }
|
64
|
+
should_not_change("@jon.follow_count") { @jon.follow_count }
|
65
|
+
|
66
|
+
should "not set the follower" do
|
67
|
+
assert_not_equal @jon, Follow.last.follower
|
68
|
+
end
|
69
|
+
|
70
|
+
should "not set the followable" do
|
71
|
+
assert_not_equal @jon, Follow.last.followable
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
context "stop_following" do
|
76
|
+
setup do
|
77
|
+
@sam.stop_following(@jon)
|
78
|
+
end
|
79
|
+
|
80
|
+
should_change("Follow count", :by => -1) { Follow.count }
|
81
|
+
should_change("@sam.follow_count", :by => -1) { @sam.follow_count }
|
82
|
+
end
|
83
|
+
|
84
|
+
context "follows" do
|
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])
|
88
|
+
end
|
89
|
+
|
90
|
+
context "follows_by_type" do
|
91
|
+
should "only return requested follows" do
|
92
|
+
assert_equal [@band_follow], @sam.follows_by_type('Band')
|
93
|
+
assert_equal [@user_follow], @sam.follows_by_type('User')
|
94
|
+
end
|
95
|
+
|
96
|
+
should "accept AR options" do
|
97
|
+
@metallica = Factory(:metallica)
|
98
|
+
@sam.follow(@metallica)
|
99
|
+
assert_equal 1, @sam.follows_by_type('Band', :limit => 1).count
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
context "following_by_type_count" do
|
104
|
+
should "return the count of the requested type" do
|
105
|
+
@metallica = Factory(:metallica)
|
106
|
+
@sam.follow(@metallica)
|
107
|
+
assert_equal 2, @sam.following_by_type_count('Band')
|
108
|
+
assert_equal 1, @sam.following_by_type_count('User')
|
109
|
+
assert_equal 0, @jon.following_by_type_count('Band')
|
110
|
+
@jon.block(@sam)
|
111
|
+
assert_equal 0, @sam.following_by_type_count('User')
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
context "all_follows" do
|
116
|
+
should "return all follows" do
|
117
|
+
assert_equal 2, @sam.all_follows.size
|
118
|
+
assert @sam.all_follows.include?(@band_follow)
|
119
|
+
assert @sam.all_follows.include?(@user_follow)
|
120
|
+
assert_equal [], @jon.all_follows
|
121
|
+
end
|
122
|
+
|
123
|
+
should "accept AR options" do
|
124
|
+
assert_equal 1, @sam.all_follows(:limit => 1).count
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
context "all_following" do
|
130
|
+
should "return the actual follow records" do
|
131
|
+
assert_equal 2, @sam.all_following.size
|
132
|
+
assert @sam.all_following.include?(@oasis)
|
133
|
+
assert @sam.all_following.include?(@jon)
|
134
|
+
assert_equal [], @jon.all_following
|
135
|
+
end
|
136
|
+
|
137
|
+
should "accept AR options" do
|
138
|
+
assert_equal 1, @sam.all_following(:limit => 1).count
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
context "following_by_type" do
|
143
|
+
should "return only requested records" do
|
144
|
+
assert_equal [@oasis], @sam.following_by_type('Band')
|
145
|
+
assert_equal [@jon], @sam.following_by_type('User')
|
146
|
+
end
|
147
|
+
|
148
|
+
should "accept AR options" do
|
149
|
+
@metallica = Factory(:metallica)
|
150
|
+
@sam.follow(@metallica)
|
151
|
+
assert_equal 1, @sam.following_by_type('Band', :limit => 1).count
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
context "method_missing" do
|
156
|
+
should "call following_by_type" do
|
157
|
+
assert_equal [@oasis], @sam.following_bands
|
158
|
+
assert_equal [@jon], @sam.following_users
|
159
|
+
end
|
160
|
+
|
161
|
+
should "call following_by_type_count" do
|
162
|
+
@metallica = Factory(:metallica)
|
163
|
+
@sam.follow(@metallica)
|
164
|
+
assert_equal 2, @sam.following_bands_count
|
165
|
+
assert_equal 1, @sam.following_users_count
|
166
|
+
assert_equal 0, @jon.following_bands_count
|
167
|
+
@jon.block(@sam)
|
168
|
+
assert_equal 0, @sam.following_users_count
|
169
|
+
end
|
170
|
+
|
171
|
+
should "raise on no method" do
|
172
|
+
assert_raises (NoMethodError){ @sam.foobar }
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
context "destroying follower" do
|
177
|
+
setup do
|
178
|
+
@jon.destroy
|
179
|
+
end
|
180
|
+
|
181
|
+
should_change("Follow.count", :by => -1) { Follow.count }
|
182
|
+
should_change("@sam.follow_count", :by => -1) { @sam.follow_count }
|
183
|
+
end
|
184
|
+
|
185
|
+
context "blocked by followable" do
|
186
|
+
setup do
|
187
|
+
@jon.block(@sam)
|
188
|
+
end
|
189
|
+
|
190
|
+
should "return following_status" do
|
191
|
+
assert_equal false, @sam.following?(@jon)
|
192
|
+
end
|
193
|
+
|
194
|
+
should "return follow_count" do
|
195
|
+
assert_equal 1, @sam.follow_count
|
196
|
+
end
|
197
|
+
|
198
|
+
should "not return record of the blocked follows" do
|
199
|
+
assert_equal 1, @sam.all_follows.size
|
200
|
+
assert !@sam.all_follows.include?(@user_follow)
|
201
|
+
assert !@sam.all_following.include?(@jon)
|
202
|
+
assert_equal [], @sam.following_by_type('User')
|
203
|
+
assert_equal [], @sam.follows_by_type('User')
|
204
|
+
assert_equal [], @sam.following_users
|
205
|
+
end
|
206
|
+
end
|
207
|
+
end
|
208
|
+
|
209
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
gem 'rails', '~>3.0.10'
|
@@ -0,0 +1,7 @@
|
|
1
|
+
# Add your own tasks in files placed in lib/tasks ending in .rake,
|
2
|
+
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
|
3
|
+
|
4
|
+
require File.expand_path('../config/application', __FILE__)
|
5
|
+
require 'rake'
|
6
|
+
|
7
|
+
Dummy::Application.load_tasks
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require File.expand_path('../boot', __FILE__)
|
2
|
+
|
3
|
+
require "active_model/railtie"
|
4
|
+
require "active_record/railtie"
|
5
|
+
|
6
|
+
Bundler.require
|
7
|
+
require "acts_as_follower"
|
8
|
+
|
9
|
+
module Dummy
|
10
|
+
class Application < Rails::Application
|
11
|
+
# Settings in config/environments/* take precedence over those specified here.
|
12
|
+
# Application configuration should go into files in config/initializers
|
13
|
+
# -- all .rb files in that directory are automatically loaded.
|
14
|
+
|
15
|
+
# Custom directories with classes and modules you want to be autoloadable.
|
16
|
+
# config.autoload_paths += %W(#{config.root}/extras)
|
17
|
+
|
18
|
+
# Only load the plugins named here, in the order given (default is alphabetical).
|
19
|
+
# :all can be used as a placeholder for all plugins not explicitly named.
|
20
|
+
# config.plugins = [ :exception_notification, :ssl_requirement, :all ]
|
21
|
+
|
22
|
+
# Activate observers that should always be running.
|
23
|
+
# config.active_record.observers = :cacher, :garbage_collector, :forum_observer
|
24
|
+
|
25
|
+
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
|
26
|
+
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
|
27
|
+
# config.time_zone = 'Central Time (US & Canada)'
|
28
|
+
|
29
|
+
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
|
30
|
+
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
|
31
|
+
# config.i18n.default_locale = :de
|
32
|
+
|
33
|
+
# JavaScript files you want as :defaults (application.js is always included).
|
34
|
+
# config.action_view.javascript_expansions[:defaults] = %w(jquery rails)
|
35
|
+
|
36
|
+
# Configure the default encoding used in templates for Ruby 1.9.
|
37
|
+
config.encoding = "utf-8"
|
38
|
+
|
39
|
+
# Configure sensitive parameters which will be filtered from the log file.
|
40
|
+
config.filter_parameters += [:password]
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
Dummy::Application.configure do
|
2
|
+
# Settings specified here will take precedence over those in config/application.rb
|
3
|
+
|
4
|
+
# In the development environment your application's code is reloaded on
|
5
|
+
# every request. This slows down response time but is perfect for development
|
6
|
+
# since you don't have to restart the webserver when you make code changes.
|
7
|
+
config.cache_classes = false
|
8
|
+
|
9
|
+
# Log error messages when you accidentally call methods on nil.
|
10
|
+
config.whiny_nils = true
|
11
|
+
|
12
|
+
# Show full error reports and disable caching
|
13
|
+
config.consider_all_requests_local = true
|
14
|
+
|
15
|
+
# Print deprecation notices to the Rails logger
|
16
|
+
config.active_support.deprecation = :log
|
17
|
+
|
18
|
+
end
|
19
|
+
|
@@ -0,0 +1,23 @@
|
|
1
|
+
Dummy::Application.configure do
|
2
|
+
# Settings specified here will take precedence over those in config/application.rb
|
3
|
+
|
4
|
+
# The test environment is used exclusively to run your application's
|
5
|
+
# test suite. You never need to work with it otherwise. Remember that
|
6
|
+
# your test database is "scratch space" for the test suite and is wiped
|
7
|
+
# and recreated between test runs. Don't rely on the data there!
|
8
|
+
config.cache_classes = true
|
9
|
+
|
10
|
+
# Log error messages when you accidentally call methods on nil.
|
11
|
+
config.whiny_nils = true
|
12
|
+
|
13
|
+
# Show full error reports and disable caching
|
14
|
+
config.consider_all_requests_local = true
|
15
|
+
|
16
|
+
# Use SQL instead of Active Record's schema dumper when creating the test database.
|
17
|
+
# This is necessary if your schema can't be completely dumped by the schema dumper,
|
18
|
+
# like if you have constraints or database-specific column types
|
19
|
+
# config.active_record.schema_format = :sql
|
20
|
+
|
21
|
+
# Print deprecation notices to the stderr
|
22
|
+
config.active_support.deprecation = :stderr
|
23
|
+
end
|
@@ -0,0 +1,7 @@
|
|
1
|
+
# Be sure to restart your server when you modify this file.
|
2
|
+
|
3
|
+
# You can add backtrace silencers for libraries that you're using but don't wish to see in your backtraces.
|
4
|
+
# Rails.backtrace_cleaner.add_silencer { |line| line =~ /my_noisy_library/ }
|
5
|
+
|
6
|
+
# You can also remove all the silencers if you're trying to debug a problem that might stem from framework code.
|
7
|
+
# Rails.backtrace_cleaner.remove_silencers!
|
@@ -0,0 +1,10 @@
|
|
1
|
+
# Be sure to restart your server when you modify this file.
|
2
|
+
|
3
|
+
# Add new inflection rules using the following format
|
4
|
+
# (all these examples are active by default):
|
5
|
+
# ActiveSupport::Inflector.inflections do |inflect|
|
6
|
+
# inflect.plural /^(ox)$/i, '\1en'
|
7
|
+
# inflect.singular /^(ox)en/i, '\1'
|
8
|
+
# inflect.irregular 'person', 'people'
|
9
|
+
# inflect.uncountable %w( fish sheep )
|
10
|
+
# end
|
@@ -0,0 +1,7 @@
|
|
1
|
+
# Be sure to restart your server when you modify this file.
|
2
|
+
|
3
|
+
# Your secret key for verifying the integrity of signed cookies.
|
4
|
+
# If you change this key, all old signed cookies will become invalid!
|
5
|
+
# Make sure the secret is at least 30 characters and all random,
|
6
|
+
# no regular words or you'll be exposed to dictionary attacks.
|
7
|
+
Dummy::Application.config.secret_token = '7b0ce915dc4c2ee60581c2769798abb5e4078292ad23670fc8d728953fc13aec19863558873234816b58a54f6a35be58b2b0a26749a7dfddcd2f02ee82d7e94f'
|
@@ -0,0 +1,8 @@
|
|
1
|
+
# Be sure to restart your server when you modify this file.
|
2
|
+
|
3
|
+
Dummy::Application.config.session_store :cookie_store, :key => '_dummy_session'
|
4
|
+
|
5
|
+
# Use the database for sessions instead of the cookie-based default,
|
6
|
+
# which shouldn't be used to store highly confidential information
|
7
|
+
# (create the session table with "rails generate session_migration")
|
8
|
+
# Dummy::Application.config.session_store :active_record_store
|