acts_as_favoritor 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +8 -0
  3. data/.travis.yml +4 -0
  4. data/Gemfile +6 -0
  5. data/LICENSE +21 -0
  6. data/README.md +73 -0
  7. data/Rakefile +37 -0
  8. data/acts_as_favoritor.gemspec +29 -0
  9. data/init.rb +1 -0
  10. data/lib/acts_as_favoritor.rb +35 -0
  11. data/lib/acts_as_favoritor/favoritable.rb +115 -0
  12. data/lib/acts_as_favoritor/favorite_scopes.rb +45 -0
  13. data/lib/acts_as_favoritor/favoritor.rb +113 -0
  14. data/lib/acts_as_favoritor/favoritor_lib.rb +43 -0
  15. data/lib/acts_as_favoritor/railtie.rb +16 -0
  16. data/lib/acts_as_favoritor/version.rb +5 -0
  17. data/lib/generators/USAGE +5 -0
  18. data/lib/generators/acts_as_follower_generator.rb +28 -0
  19. data/lib/generators/templates/migration.rb.erb +18 -0
  20. data/lib/generators/templates/model.rb +16 -0
  21. data/test/README +24 -0
  22. data/test/acts_as_followable_test.rb +283 -0
  23. data/test/acts_as_follower_test.rb +224 -0
  24. data/test/dummy30/Gemfile +1 -0
  25. data/test/dummy30/Rakefile +7 -0
  26. data/test/dummy30/app/models/application_record.rb +3 -0
  27. data/test/dummy30/app/models/band.rb +4 -0
  28. data/test/dummy30/app/models/band/punk.rb +4 -0
  29. data/test/dummy30/app/models/band/punk/pop_punk.rb +4 -0
  30. data/test/dummy30/app/models/custom_record.rb +3 -0
  31. data/test/dummy30/app/models/some.rb +5 -0
  32. data/test/dummy30/app/models/user.rb +5 -0
  33. data/test/dummy30/config.ru +4 -0
  34. data/test/dummy30/config/application.rb +42 -0
  35. data/test/dummy30/config/boot.rb +10 -0
  36. data/test/dummy30/config/database.yml +6 -0
  37. data/test/dummy30/config/environment.rb +5 -0
  38. data/test/dummy30/config/environments/development.rb +18 -0
  39. data/test/dummy30/config/environments/test.rb +20 -0
  40. data/test/dummy30/config/initializers/backtrace_silencers.rb +7 -0
  41. data/test/dummy30/config/initializers/inflections.rb +10 -0
  42. data/test/dummy30/config/initializers/secret_token.rb +7 -0
  43. data/test/dummy30/config/initializers/session_store.rb +8 -0
  44. data/test/dummy30/config/locales/en.yml +5 -0
  45. data/test/dummy30/config/routes.rb +2 -0
  46. data/test/factories/bands.rb +17 -0
  47. data/test/factories/somes.rb +9 -0
  48. data/test/factories/users.rb +13 -0
  49. data/test/follow_test.rb +28 -0
  50. data/test/schema.rb +25 -0
  51. data/test/test_helper.rb +18 -0
  52. metadata +181 -0
@@ -0,0 +1,224 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class ActsAsFollowerTest < ActiveSupport::TestCase
4
+
5
+ context "instance methods" do
6
+ setup do
7
+ @sam = FactoryGirl.create(: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?(:remove_favorite)
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 = FactoryGirl.create(:sam)
23
+ @jon = FactoryGirl.create(:jon)
24
+ @oasis = FactoryGirl.create(: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 "remove_favorite" do
76
+ setup do
77
+ @sam.remove_favorite(@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.where("follower_id = ? and follower_type = 'User' and followable_id = ? and followable_type = 'Band'", @sam.id, @oasis.id).first
87
+ @user_follow = Follow.where("follower_id = ? and follower_type = 'User' and followable_id = ? and followable_type = 'User'", @sam.id, @jon.id).first
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 = FactoryGirl.create(: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 = FactoryGirl.create(: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 limit option" do
138
+ assert_equal 1, @sam.all_following(limit: 1).count
139
+ end
140
+
141
+ should "accept AR where option" do
142
+ assert_equal 1, @sam.all_following(where: { id: @oasis.id }).count
143
+ end
144
+ end
145
+
146
+ context "following_by_type" do
147
+ should "return only requested records" do
148
+ assert_equal [@oasis], @sam.following_by_type('Band')
149
+ assert_equal [@jon], @sam.following_by_type('User')
150
+ end
151
+
152
+ should "accept AR options" do
153
+ @metallica = FactoryGirl.create(:metallica)
154
+ @sam.follow(@metallica)
155
+ assert_equal 1, @sam.following_by_type('Band', limit: 1).to_a.size
156
+ end
157
+ end
158
+
159
+ context "method_missing" do
160
+ should "call following_by_type" do
161
+ assert_equal [@oasis], @sam.following_bands
162
+ assert_equal [@jon], @sam.following_users
163
+ end
164
+
165
+ should "call following_by_type_count" do
166
+ @metallica = FactoryGirl.create(:metallica)
167
+ @sam.follow(@metallica)
168
+ assert_equal 2, @sam.following_bands_count
169
+ assert_equal 1, @sam.following_users_count
170
+ assert_equal 0, @jon.following_bands_count
171
+ @jon.block(@sam)
172
+ assert_equal 0, @sam.following_users_count
173
+ end
174
+
175
+ should "raise on no method" do
176
+ assert_raises (NoMethodError){ @sam.foobar }
177
+ end
178
+ end
179
+
180
+ context "respond_to?" do
181
+ should "advertise that it responds to following methods" do
182
+ assert @sam.respond_to?(:following_users)
183
+ assert @sam.respond_to?(:following_users_count)
184
+ end
185
+
186
+ should "return false when called with a nonexistent method" do
187
+ assert (not @sam.respond_to?(:foobar))
188
+ end
189
+ end
190
+
191
+ context "destroying follower" do
192
+ setup do
193
+ @jon.destroy
194
+ end
195
+
196
+ should_change("Follow.count", by: -1) { Follow.count }
197
+ should_change("@sam.follow_count", by: -1) { @sam.follow_count }
198
+ end
199
+
200
+ context "blocked by followable" do
201
+ setup do
202
+ @jon.block(@sam)
203
+ end
204
+
205
+ should "return following_status" do
206
+ assert_equal false, @sam.following?(@jon)
207
+ end
208
+
209
+ should "return follow_count" do
210
+ assert_equal 1, @sam.follow_count
211
+ end
212
+
213
+ should "not return record of the blocked follows" do
214
+ assert_equal 1, @sam.all_follows.size
215
+ assert !@sam.all_follows.include?(@user_follow)
216
+ assert !@sam.all_following.include?(@jon)
217
+ assert_equal [], @sam.following_by_type('User')
218
+ assert_equal [], @sam.follows_by_type('User')
219
+ assert_equal [], @sam.following_users
220
+ end
221
+ end
222
+ end
223
+
224
+ end
@@ -0,0 +1 @@
1
+ gem 'rails', '~> 5.1.3'
@@ -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,3 @@
1
+ class ApplicationRecord < ActiveRecord::Base
2
+ self.abstract_class = true
3
+ end
@@ -0,0 +1,4 @@
1
+ class Band < ApplicationRecord
2
+ validates_presence_of :name
3
+ acts_as_favoritable
4
+ end
@@ -0,0 +1,4 @@
1
+ class Band::Punk < Band
2
+ validates_presence_of :name
3
+ acts_as_favoritable
4
+ end
@@ -0,0 +1,4 @@
1
+ class Band::Punk::PopPunk < Band::Punk
2
+ validates_presence_of :name
3
+ acts_as_favoritable
4
+ end
@@ -0,0 +1,3 @@
1
+ class CustomRecord < ActiveRecord::Base
2
+ self.abstract_class = true
3
+ end
@@ -0,0 +1,5 @@
1
+ class Some < CustomRecord
2
+ validates_presence_of :name
3
+ acts_as_favoritor
4
+ acts_as_favoritable
5
+ end
@@ -0,0 +1,5 @@
1
+ class User < ApplicationRecord
2
+ validates_presence_of :name
3
+ acts_as_favoritor
4
+ acts_as_favoritable
5
+ end
@@ -0,0 +1,4 @@
1
+ # This file is used by Rack-based servers to start the application.
2
+
3
+ require ::File.expand_path '../config/environment', __FILE__
4
+ run Dummy::Application
@@ -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_favoritor'
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,10 @@
1
+ require 'rubygems'
2
+ gemfile = File.expand_path '../../../../Gemfile', __FILE__
3
+
4
+ if File.exist?(gemfile)
5
+ ENV['BUNDLE_GEMFILE'] = gemfile
6
+ require 'bundler'
7
+ Bundler.setup
8
+ end
9
+
10
+ $:.unshift File.expand_path '../../../../lib', __FILE__
@@ -0,0 +1,6 @@
1
+ test:
2
+ adapter: sqlite3
3
+ database: ':memory:'
4
+ development:
5
+ adapter: sqlite3
6
+ database: ':memory:'
@@ -0,0 +1,5 @@
1
+ # Load the rails application
2
+ require File.expand_path '../application', __FILE__
3
+
4
+ # Initialize the rails application
5
+ Dummy::Application.initialize!
@@ -0,0 +1,18 @@
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
@@ -0,0 +1,20 @@
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
+ # Show full error reports and disable caching
11
+ config.consider_all_requests_local = true
12
+
13
+ # Use SQL instead of Active Record's schema dumper when creating the test database.
14
+ # This is necessary if your schema can't be completely dumped by the schema dumper,
15
+ # like if you have constraints or database-specific column types
16
+ # config.active_record.schema_format = :sql
17
+
18
+ # Print deprecation notices to the stderr
19
+ config.active_support.deprecation = :stderr
20
+ 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
@@ -0,0 +1,5 @@
1
+ # Sample localization file for English. Add more files in this directory for other locales.
2
+ # See http://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.
3
+
4
+ en:
5
+ hello: 'Hello world'