Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Support subclassing ratables

This allows ratable classes to have subclasses that properly accumulate ratings
in the same ratable category.
  • Loading branch information...
commit b01bc8b41ec22b2b63beb957475ce3f180084435 1 parent f2357dd
Aaron Royer aaronroyer authored
21 lib/recommendable/helpers/redis_key_mapper.rb
View
@@ -4,7 +4,12 @@ module RedisKeyMapper
class << self
%w[liked disliked hidden bookmarked recommended].each do |action|
define_method "#{action}_set_for" do |klass, id|
- [Recommendable.config.redis_namespace, Recommendable.config.user_class.to_s.tableize, id, "#{action}_#{klass.to_s.tableize}"].compact.join(':')
+ [
+ Recommendable.config.redis_namespace,
+ Recommendable.config.user_class.to_s.tableize,
+ id,
+ "#{action}_#{ratable_class(klass).to_s.tableize}"
+ ].compact.join(':')
end
end
@@ -13,19 +18,25 @@ def similarity_set_for(id)
end
def liked_by_set_for(klass, id)
- [Recommendable.config.redis_namespace, klass.to_s.tableize, id, 'liked_by'].compact.join(':')
+ [Recommendable.config.redis_namespace, ratable_class(klass).to_s.tableize, id, 'liked_by'].compact.join(':')
end
def disliked_by_set_for(klass, id)
- [Recommendable.config.redis_namespace, klass.to_s.tableize, id, 'disliked_by'].compact.join(':')
+ [Recommendable.config.redis_namespace, ratable_class(klass).to_s.tableize, id, 'disliked_by'].compact.join(':')
end
def score_set_for(klass)
- [Recommendable.config.redis_namespace, klass.to_s.tableize, 'scores'].join(':')
+ [Recommendable.config.redis_namespace, ratable_class(klass).to_s.tableize, 'scores'].join(':')
end
def temp_set_for(klass, id)
- [Recommendable.config.redis_namespace, klass.to_s.tableize, id, 'temp'].compact.join(':')
+ [Recommendable.config.redis_namespace, ratable_class(klass).to_s.tableize, id, 'temp'].compact.join(':')
+ end
+
+ private
+
+ def ratable_class(klass)
+ klass.respond_to?(:ratable_class) ? klass.ratable_class : klass
end
end
end
8 lib/recommendable/ratable.rb
View
@@ -52,6 +52,14 @@ def self.top(count = 1)
Recommendable.query(self, ids).sort_by { |item| ids.index(item.id.to_s) }
end
+ # Returns the class that has been explicitly been made ratable, whether it is this
+ # class or a superclass. This allows a ratable class and all of its subclasses to be
+ # considered the same type of ratable and give recommendations from the base class
+ # or any of the subclasses.
+ def self.ratable_class
+ ancestors.find { |klass| Recommendable.config.ratable_classes.include?(klass) }
+ end
+
private
# Completely removes this item from redis. Called from a before_destroy hook.
3  test/dummy/app/models/boat.rb
View
@@ -0,0 +1,3 @@
+# STI class that is not ratable
+class Boat < Vehicle
+end
4 test/dummy/app/models/car.rb
View
@@ -0,0 +1,4 @@
+# STI ratable class whose base class is not ratable
+class Car < Vehicle
+end
+
3  test/dummy/app/models/documentary.rb
View
@@ -0,0 +1,3 @@
+# STI subclass of a ratable class
+class Documentary < Movie
+end
2  test/dummy/app/models/user.rb
View
@@ -1,4 +1,4 @@
class User < ActiveRecord::Base
attr_accessible :email if ::ActiveRecord::VERSION::MAJOR < 4
- recommends :movies, :books
+ recommends :movies, :books, :cars
end
3  test/dummy/app/models/vehicle.rb
View
@@ -0,0 +1,3 @@
+# STI base class that is not ratable
+class Vehicle < ActiveRecord::Base
+end
BIN  test/dummy/db/development.sqlite3
View
Binary file not shown
10 test/dummy/db/migrate/20131226071447_create_vehicles.rb
View
@@ -0,0 +1,10 @@
+class CreateVehicles < ActiveRecord::Migration
+ def change
+ create_table :vehicles do |t|
+ t.string :color
+ t.string :type
+
+ t.timestamps
+ end
+ end
+end
5 test/dummy/db/migrate/20131226165647_add_type_to_movie.rb
View
@@ -0,0 +1,5 @@
+class AddTypeToMovie < ActiveRecord::Migration
+ def change
+ add_column :movies, :type, :string
+ end
+end
36 test/dummy/db/schema.rb
View
@@ -9,34 +9,42 @@
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
-# It's strongly recommended to check this file into your version control system.
+# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20121007213144) do
+ActiveRecord::Schema.define(version: 20131226165647) do
- create_table "books", :force => true do |t|
+ create_table "books", force: true do |t|
t.string "title"
t.string "author"
- t.datetime "created_at", :null => false
- t.datetime "updated_at", :null => false
+ t.datetime "created_at", null: false
+ t.datetime "updated_at", null: false
end
- create_table "movies", :force => true do |t|
+ create_table "movies", force: true do |t|
t.string "title"
t.integer "year"
- t.datetime "created_at", :null => false
- t.datetime "updated_at", :null => false
+ t.datetime "created_at", null: false
+ t.datetime "updated_at", null: false
+ t.string "type"
end
- create_table "rocks", :force => true do |t|
+ create_table "rocks", force: true do |t|
t.string "name"
- t.datetime "created_at", :null => false
- t.datetime "updated_at", :null => false
+ t.datetime "created_at", null: false
+ t.datetime "updated_at", null: false
end
- create_table "users", :force => true do |t|
+ create_table "users", force: true do |t|
t.string "email"
- t.datetime "created_at", :null => false
- t.datetime "updated_at", :null => false
+ t.datetime "created_at", null: false
+ t.datetime "updated_at", null: false
+ end
+
+ create_table "vehicles", force: true do |t|
+ t.string "color"
+ t.string "type"
+ t.datetime "created_at"
+ t.datetime "updated_at"
end
end
BIN  test/dummy/db/test.sqlite3
View
Binary file not shown
20 test/factories.rb
View
@@ -7,6 +7,12 @@
f.year { '200%d'.to_i }
end
+Factory.define(:documentary) do |f|
+ f.title '%{year}: A Space Documentary'
+ f.year { '200%d'.to_i }
+ f.type 'Documentary'
+end
+
Factory.define(:book) do |f|
f.title 'Harry Potter Vol. %d'
f.author 'J.K. Rowling'
@@ -15,3 +21,17 @@
Factory.define(:rock) do |f|
f.name 'Boring Specimen No. %d'
end
+
+Factory.define(:vehicle) do |f|
+ f.color 'blue'
+end
+
+Factory.define(:car) do |f|
+ f.type 'Car'
+ f.color 'red'
+end
+
+Factory.define(:boat) do |f|
+ f.type 'Boat'
+ f.color 'white'
+end
3  test/recommendable/helpers/calculations_test.rb
View
@@ -5,7 +5,8 @@ class CalculationsTest < MiniTest::Unit::TestCase
def setup
@user = Factory(:user)
5.times { |x| instance_variable_set(:"@user#{x+1}", Factory(:user)) }
- 10.times { |x| instance_variable_set(:"@movie#{x+1}", Factory(:movie)) }
+ 5.times { |x| instance_variable_set(:"@movie#{x+1}", Factory(:movie)) }
+ 5.upto(9) { |x| instance_variable_set(:"@movie#{x+1}", Factory(:documentary)) }
10.times { |x| instance_variable_set(:"@book#{x+1}", Factory(:book)) }
[@movie1, @movie2, @movie3, @book4, @book5, @book6].each { |obj| @user.like(obj) }
65 test/recommendable/helpers/redis_key_mapper_test.rb
View
@@ -37,4 +37,69 @@ def test_output_of_disliked_by_set_for
def test_output_of_score_set_for
assert_equal Recommendable::Helpers::RedisKeyMapper.score_set_for(Movie), 'recommendable:movies:scores'
end
+
+ def test_output_of_liked_set_for_subclass_of_ratable
+ assert_equal Recommendable::Helpers::RedisKeyMapper.liked_set_for(Documentary, 1), 'recommendable:users:1:liked_movies'
+ end
+
+ def test_output_of_disliked_set_for_subclass_of_ratable
+ assert_equal Recommendable::Helpers::RedisKeyMapper.disliked_set_for(Documentary, 1), 'recommendable:users:1:disliked_movies'
+ end
+
+ def test_output_of_hidden_set_for_subclass_of_ratable
+ assert_equal Recommendable::Helpers::RedisKeyMapper.hidden_set_for(Documentary, 1), 'recommendable:users:1:hidden_movies'
+ end
+
+ def test_output_of_bookmarked_set_for_subclass_of_ratable
+ assert_equal Recommendable::Helpers::RedisKeyMapper.bookmarked_set_for(Documentary, 1), 'recommendable:users:1:bookmarked_movies'
+ end
+
+ def test_output_of_recommended_set_for_subclass_of_ratable
+ assert_equal Recommendable::Helpers::RedisKeyMapper.recommended_set_for(Documentary, 1), 'recommendable:users:1:recommended_movies'
+ end
+
+ def test_output_of_liked_by_set_for_subclass_of_ratable
+ assert_equal Recommendable::Helpers::RedisKeyMapper.liked_by_set_for(Documentary, 1), 'recommendable:movies:1:liked_by'
+ end
+
+ def test_output_of_disliked_by_set_for_subclass_of_ratable
+ assert_equal Recommendable::Helpers::RedisKeyMapper.disliked_by_set_for(Documentary, 1), 'recommendable:movies:1:disliked_by'
+ end
+
+ def test_output_of_score_set_for_subclass_of_ratable
+ assert_equal Recommendable::Helpers::RedisKeyMapper.score_set_for(Documentary), 'recommendable:movies:scores'
+ end
+
+ def test_output_of_liked_set_for_ratable_subclass_of_nonratable
+ assert_equal Recommendable::Helpers::RedisKeyMapper.liked_set_for(Car, 1), 'recommendable:users:1:liked_cars'
+ end
+
+ def test_output_of_disliked_set_for_ratable_subclass_of_nonratable
+ assert_equal Recommendable::Helpers::RedisKeyMapper.disliked_set_for(Car, 1), 'recommendable:users:1:disliked_cars'
+ end
+
+ def test_output_of_hidden_set_for_ratable_subclass_of_nonratable
+ assert_equal Recommendable::Helpers::RedisKeyMapper.hidden_set_for(Car, 1), 'recommendable:users:1:hidden_cars'
+ end
+
+ def test_output_of_bookmarked_set_for_ratable_subclass_of_nonratable
+ assert_equal Recommendable::Helpers::RedisKeyMapper.bookmarked_set_for(Car, 1), 'recommendable:users:1:bookmarked_cars'
+ end
+
+ def test_output_of_recommended_set_for_ratable_subclass_of_nonratable
+ assert_equal Recommendable::Helpers::RedisKeyMapper.recommended_set_for(Car, 1), 'recommendable:users:1:recommended_cars'
+ end
+
+ def test_output_of_liked_by_set_for_ratable_subclass_of_nonratable
+ assert_equal Recommendable::Helpers::RedisKeyMapper.liked_by_set_for(Car, 1), 'recommendable:cars:1:liked_by'
+ end
+
+ def test_output_of_disliked_by_set_for_ratable_subclass_of_nonratable
+ assert_equal Recommendable::Helpers::RedisKeyMapper.disliked_by_set_for(Car, 1), 'recommendable:cars:1:disliked_by'
+ end
+
+ def test_output_of_score_set_for_ratable_subclass_of_nonratable
+ assert_equal Recommendable::Helpers::RedisKeyMapper.score_set_for(Car), 'recommendable:cars:scores'
+ end
+
end
53 test/recommendable/ratable_test.rb
View
@@ -6,15 +6,24 @@ def setup
@movie = Factory(:movie)
@book = Factory(:book)
@rock = Factory(:rock)
+ @vehicle = Factory(:vehicle)
end
def test_recommendable_predicate_works
assert Movie.recommendable?
assert @movie.recommendable?
+ assert Documentary.recommendable?
+ assert Factory(:documentary).recommendable?
assert Book.recommendable?
assert @book.recommendable?
refute Rock.recommendable?
refute @rock.recommendable?
+ assert Car.recommendable?
+ assert Factory(:car).recommendable?
+ refute Vehicle.recommendable?
+ refute @vehicle.recommendable?
+ refute Boat.recommendable?
+ refute Factory(:boat).recommendable?
end
def test_rated_predicate_works
@@ -41,6 +50,23 @@ def test_top_scope_returns_best_things
assert_equal top[2], @book
end
+ def test_top_scope_returns_best_things_for_ratable_base_class
+ @movie2 = Factory(:movie)
+ @doc = Factory(:documentary)
+ @user = Factory(:user)
+ @friend = Factory(:user)
+
+ @user.like(@doc)
+ @friend.like(@doc)
+ @user.like(@movie2)
+ @user.dislike(@movie)
+
+ top = Movie.top(3)
+ assert_equal top[0], @doc
+ assert_equal top[1], @movie2
+ assert_equal top[2], @movie
+ end
+
def test_removed_from_recommendable_upon_destruction
@user = Factory(:user)
@friend = Factory(:user)
@@ -72,6 +98,33 @@ def test_removed_from_recommendable_upon_destruction
assert_empty @buddy.bookmarked_books
end
+ def test_ratable_subclass_object_removed_from_recommendable_upon_destruction
+ @doc = Factory(:documentary)
+ @user = Factory(:user)
+ @friend = Factory(:user)
+ @buddy = Factory(:user)
+ @stranger = Factory(:user)
+ @user.like(@doc)
+ @friend.dislike(@doc)
+ @buddy.hide(@doc)
+ @stranger.bookmark(@doc)
+
+ liked_by_set = Recommendable::Helpers::RedisKeyMapper.liked_by_set_for(@doc.class, @doc.id)
+ disliked_by_set = Recommendable::Helpers::RedisKeyMapper.disliked_by_set_for(@doc.class, @doc.id)
+ [liked_by_set, disliked_by_set].each { |set| assert_equal Recommendable.redis.scard(set), 1 }
+
+ assert @user.likes?(@doc)
+ assert @friend.dislikes?(@doc)
+ assert @buddy.hides?(@doc)
+
+ @doc.destroy
+
+ [liked_by_set, disliked_by_set].each { |set| assert_equal Recommendable.redis.scard(set), 0 }
+
+ assert_empty @buddy.hidden_movies
+ assert_empty @stranger.bookmarked_books
+ end
+
def teardown
Recommendable.redis.flushdb
end
31 test/recommendable/rater/bookmarker_test.rb
View
@@ -5,6 +5,7 @@ class BookmarkerTest < MiniTest::Unit::TestCase
def setup
@user = Factory(:user)
@movie = Factory(:movie)
+ @doc = Factory(:documentary)
end
def test_that_bookmark_adds_to_bookmarked_set
@@ -13,6 +14,12 @@ def test_that_bookmark_adds_to_bookmarked_set
assert_includes @user.bookmarked_movie_ids, @movie.id
end
+ def test_that_bookmark_adds_subclass_bookmarks_to_bookmarked_set
+ refute_includes @user.bookmarked_movie_ids, @doc.id
+ @user.bookmark(@doc)
+ assert_includes @user.bookmarked_movie_ids, @doc.id
+ end
+
def test_that_cant_bookmark_already_bookmarked_object
assert @user.bookmark(@movie)
assert_nil @user.bookmark(@movie)
@@ -39,6 +46,13 @@ def test_that_unbookmark_removes_item_from_bookmarked_set
refute_includes @user.bookmarked_movie_ids, @movie.id
end
+ def test_that_unbookmark_removes_subclass_item_from_bookmarked_set
+ @user.bookmark(@doc)
+ assert_includes @user.bookmarked_movie_ids, @doc.id
+ @user.unbookmark(@doc)
+ refute_includes @user.bookmarked_movie_ids, @doc.id
+ end
+
def test_that_cant_unbookmark_item_unless_bookmarked
assert_nil @user.unbookmark(@movie)
end
@@ -47,6 +61,10 @@ def test_that_bookmarks_returns_bookmarked_records
refute_includes @user.bookmarks, @movie
@user.bookmark(@movie)
assert_includes @user.bookmarks, @movie
+
+ refute_includes @user.bookmarks, @doc
+ @user.bookmark(@doc)
+ assert_includes @user.bookmarks, @doc
end
def test_that_dynamic_bookmarked_finder_only_returns_relevant_records
@@ -65,8 +83,9 @@ def test_that_bookmarks_count_counts_all_bookmarks
@user.bookmark(@movie)
@user.bookmark(movie2)
@user.bookmark(book)
+ @user.bookmark(@doc)
- assert_equal @user.bookmarks_count, 3
+ assert_equal @user.bookmarks_count, 4
end
def test_that_dynamic_bookmarked_count_methods_only_count_relevant_bookmarks
@@ -75,9 +94,10 @@ def test_that_dynamic_bookmarked_count_methods_only_count_relevant_bookmarks
@user.bookmark(@movie)
@user.bookmark(movie2)
+ @user.bookmark(@doc)
@user.bookmark(book)
- assert_equal @user.bookmarked_movies_count, 2
+ assert_equal @user.bookmarked_movies_count, 3
assert_equal @user.bookmarked_books_count, 1
end
@@ -90,11 +110,14 @@ def test_that_bookmarks_in_common_with_returns_all_common_bookmarks
@user.bookmark(@movie)
@user.bookmark(book)
@user.bookmark(movie2)
+ @user.bookmark(@doc)
friend.bookmark(@movie)
friend.bookmark(book)
friend.bookmark(book2)
+ friend.bookmark(@doc)
assert_includes @user.bookmarks_in_common_with(friend), @movie
+ assert_includes @user.bookmarks_in_common_with(friend), @doc
assert_includes @user.bookmarks_in_common_with(friend), book
refute_includes @user.bookmarks_in_common_with(friend), movie2
refute_includes friend.bookmarks_in_common_with(@user), book2
@@ -106,14 +129,18 @@ def test_that_dynamic_bookmarked_in_common_with_only_returns_relevant_records
book = Factory(:book)
@user.bookmark(@movie)
+ @user.bookmark(@doc)
@user.bookmark(book)
friend.bookmark(@movie)
friend.bookmark(book)
+ friend.bookmark(@doc)
assert_includes @user.bookmarked_movies_in_common_with(friend), @movie
+ assert_includes @user.bookmarked_movies_in_common_with(friend), @doc
assert_includes @user.bookmarked_books_in_common_with(friend), book
refute_includes @user.bookmarked_movies_in_common_with(friend), book
refute_includes @user.bookmarked_books_in_common_with(friend), @movie
+ refute_includes @user.bookmarked_books_in_common_with(friend), @doc
end
def teardown
31 test/recommendable/rater/disliker_test.rb
View
@@ -5,6 +5,7 @@ class DislikerTest < MiniTest::Unit::TestCase
def setup
@user = Factory(:user)
@movie = Factory(:movie)
+ @doc = Factory(:documentary)
end
def test_that_dislike_adds_to_disliked_set
@@ -13,6 +14,12 @@ def test_that_dislike_adds_to_disliked_set
assert_includes @user.disliked_movie_ids, @movie.id
end
+ def test_that_dislike_adds_subclass_dislikes_to_disliked_set
+ refute_includes @user.disliked_movie_ids, @doc.id
+ @user.dislike(@doc)
+ assert_includes @user.disliked_movie_ids, @doc.id
+ end
+
def test_that_cant_dislike_already_disliked_object
assert @user.dislike(@movie)
assert_nil @user.dislike(@movie)
@@ -39,6 +46,13 @@ def test_that_undislike_removes_item_from_disliked_set
refute_includes @user.disliked_movie_ids, @movie.id
end
+ def test_that_undislike_removes_subclass_item_from_disliked_set
+ @user.dislike(@doc)
+ assert_includes @user.disliked_movie_ids, @doc.id
+ @user.undislike(@doc)
+ refute_includes @user.disliked_movie_ids, @doc.id
+ end
+
def test_that_cant_undislike_item_unless_disliked
assert_nil @user.undislike(@movie)
end
@@ -47,6 +61,10 @@ def test_that_dislikes_returns_disliked_records
refute_includes @user.dislikes, @movie
@user.dislike(@movie)
assert_includes @user.dislikes, @movie
+
+ refute_includes @user.dislikes, @doc
+ @user.dislike(@doc)
+ assert_includes @user.dislikes, @doc
end
def test_that_dynamic_disliked_finder_only_returns_relevant_records
@@ -65,8 +83,9 @@ def test_that_dislikes_count_counts_all_dislikes
@user.dislike(@movie)
@user.dislike(movie2)
@user.dislike(book)
+ @user.dislike(@doc)
- assert_equal @user.dislikes_count, 3
+ assert_equal @user.dislikes_count, 4
end
def test_that_dynamic_disliked_count_methods_only_count_relevant_dislikes
@@ -75,9 +94,10 @@ def test_that_dynamic_disliked_count_methods_only_count_relevant_dislikes
@user.dislike(@movie)
@user.dislike(movie2)
+ @user.dislike(@doc)
@user.dislike(book)
- assert_equal @user.disliked_movies_count, 2
+ assert_equal @user.disliked_movies_count, 3
assert_equal @user.disliked_books_count, 1
end
@@ -90,11 +110,14 @@ def test_that_dislikes_in_common_with_returns_all_common_dislikes
@user.dislike(@movie)
@user.dislike(book)
@user.dislike(movie2)
+ @user.dislike(@doc)
friend.dislike(@movie)
friend.dislike(book)
friend.dislike(book2)
+ friend.dislike(@doc)
assert_includes @user.dislikes_in_common_with(friend), @movie
+ assert_includes @user.dislikes_in_common_with(friend), @doc
assert_includes @user.dislikes_in_common_with(friend), book
refute_includes @user.dislikes_in_common_with(friend), movie2
refute_includes friend.dislikes_in_common_with(@user), book2
@@ -106,14 +129,18 @@ def test_that_dynamic_disliked_in_common_with_only_returns_relevant_records
book = Factory(:book)
@user.dislike(@movie)
+ @user.dislike(@doc)
@user.dislike(book)
friend.dislike(@movie)
+ friend.dislike(@doc)
friend.dislike(book)
assert_includes @user.disliked_movies_in_common_with(friend), @movie
+ assert_includes @user.disliked_movies_in_common_with(friend), @doc
assert_includes @user.disliked_books_in_common_with(friend), book
refute_includes @user.disliked_movies_in_common_with(friend), book
refute_includes @user.disliked_books_in_common_with(friend), @movie
+ refute_includes @user.disliked_books_in_common_with(friend), @doc
end
def teardown
28 test/recommendable/rater/hider_test.rb
View
@@ -5,6 +5,7 @@ class HiderTest < MiniTest::Unit::TestCase
def setup
@user = Factory(:user)
@movie = Factory(:movie)
+ @doc = Factory(:documentary)
end
def test_that_hide_adds_to_hidden_set
@@ -13,6 +14,12 @@ def test_that_hide_adds_to_hidden_set
assert_includes @user.hidden_movie_ids, @movie.id
end
+ def test_that_like_adds_subclass_hides_to_hidden_set
+ refute_includes @user.hidden_movie_ids, @doc.id
+ @user.hide(@doc)
+ assert_includes @user.hidden_movie_ids, @doc.id
+ end
+
def test_that_cant_hide_already_hidden_object
assert @user.hide(@movie)
assert_nil @user.hide(@movie)
@@ -39,6 +46,13 @@ def test_that_unhide_removes_item_from_hidden_set
refute_includes @user.hidden_movie_ids, @movie.id
end
+ def test_that_unhide_removes_subclass_item_from_hidden_set
+ @user.hide(@doc)
+ assert_includes @user.hidden_movie_ids, @doc.id
+ @user.unhide(@doc)
+ refute_includes @user.hidden_movie_ids, @doc.id
+ end
+
def test_that_cant_unhide_item_unless_hidden
assert_nil @user.unhide(@movie)
end
@@ -47,6 +61,10 @@ def test_that_hidden_returns_hidden_records
refute_includes @user.hiding, @movie
@user.hide(@movie)
assert_includes @user.hiding, @movie
+
+ refute_includes @user.hiding, @doc
+ @user.hide(@doc)
+ assert_includes @user.hiding, @doc
end
def test_that_dynamic_hidden_finder_only_returns_relevant_records
@@ -65,8 +83,9 @@ def test_that_hides_count_counts_all_hides
@user.hide(@movie)
@user.hide(movie2)
@user.hide(book)
+ @user.hide(@doc)
- assert_equal @user.hidden_count, 3
+ assert_equal @user.hidden_count, 4
end
def test_that_dynamic_hidden_count_methods_only_count_relevant_hides
@@ -90,11 +109,14 @@ def test_that_hides_in_common_with_returns_all_common_hides
@user.hide(@movie)
@user.hide(book)
@user.hide(movie2)
+ @user.hide(@doc)
friend.hide(@movie)
friend.hide(book)
friend.hide(book2)
+ friend.hide(@doc)
assert_includes @user.hiding_in_common_with(friend), @movie
+ assert_includes @user.hiding_in_common_with(friend), @doc
assert_includes @user.hiding_in_common_with(friend), book
refute_includes @user.hiding_in_common_with(friend), movie2
refute_includes friend.hiding_in_common_with(@user), book2
@@ -106,14 +128,18 @@ def test_that_dynamic_hidden_in_common_with_only_returns_relevant_records
book = Factory(:book)
@user.hide(@movie)
+ @user.hide(@doc)
@user.hide(book)
friend.hide(@movie)
+ friend.hide(@doc)
friend.hide(book)
assert_includes @user.hidden_movies_in_common_with(friend), @movie
+ assert_includes @user.hidden_movies_in_common_with(friend), @doc
assert_includes @user.hidden_books_in_common_with(friend), book
refute_includes @user.hidden_movies_in_common_with(friend), book
refute_includes @user.hidden_books_in_common_with(friend), @movie
+ refute_includes @user.hidden_books_in_common_with(friend), @doc
end
def teardown
31 test/recommendable/rater/liker_test.rb
View
@@ -5,6 +5,7 @@ class LikerTest < MiniTest::Unit::TestCase
def setup
@user = Factory(:user)
@movie = Factory(:movie)
+ @doc = Factory(:documentary)
end
def test_that_like_adds_to_liked_set
@@ -13,6 +14,12 @@ def test_that_like_adds_to_liked_set
assert_includes @user.liked_movie_ids, @movie.id
end
+ def test_that_like_adds_subclass_likes_to_liked_set
+ refute_includes @user.liked_movie_ids, @doc.id
+ @user.like(@doc)
+ assert_includes @user.liked_movie_ids, @doc.id
+ end
+
def test_that_cant_like_already_liked_object
assert @user.like(@movie)
assert_nil @user.like(@movie)
@@ -39,6 +46,13 @@ def test_that_unlike_removes_item_from_liked_set
refute_includes @user.liked_movie_ids, @movie.id
end
+ def test_that_unlike_removes_subclass_item_from_liked_set
+ @user.like(@doc)
+ assert_includes @user.liked_movie_ids, @doc.id
+ @user.unlike(@doc)
+ refute_includes @user.liked_movie_ids, @doc.id
+ end
+
def test_that_cant_unlike_item_unless_liked
assert_nil @user.unlike(@movie)
end
@@ -47,6 +61,10 @@ def test_that_likes_returns_liked_records
refute_includes @user.likes, @movie
@user.like(@movie)
assert_includes @user.likes, @movie
+
+ refute_includes @user.likes, @doc
+ @user.like(@doc)
+ assert_includes @user.likes, @doc
end
def test_that_dynamic_liked_finder_only_returns_relevant_records
@@ -65,8 +83,9 @@ def test_that_likes_count_counts_all_likes
@user.like(@movie)
@user.like(movie2)
@user.like(book)
+ @user.like(@doc)
- assert_equal @user.likes_count, 3
+ assert_equal @user.likes_count, 4
end
def test_that_dynamic_liked_count_methods_only_count_relevant_likes
@@ -75,9 +94,10 @@ def test_that_dynamic_liked_count_methods_only_count_relevant_likes
@user.like(@movie)
@user.like(movie2)
+ @user.like(@doc)
@user.like(book)
- assert_equal @user.liked_movies_count, 2
+ assert_equal @user.liked_movies_count, 3
assert_equal @user.liked_books_count, 1
end
@@ -90,11 +110,14 @@ def test_that_likes_in_common_with_returns_all_common_likes
@user.like(@movie)
@user.like(book)
@user.like(movie2)
+ @user.like(@doc)
friend.like(@movie)
friend.like(book)
friend.like(book2)
+ friend.like(@doc)
assert_includes @user.likes_in_common_with(friend), @movie
+ assert_includes @user.likes_in_common_with(friend), @doc
assert_includes @user.likes_in_common_with(friend), book
refute_includes @user.likes_in_common_with(friend), movie2
refute_includes friend.likes_in_common_with(@user), book2
@@ -106,14 +129,18 @@ def test_that_dynamic_liked_in_common_with_only_returns_relevant_records
book = Factory(:book)
@user.like(@movie)
+ @user.like(@doc)
@user.like(book)
friend.like(@movie)
+ friend.like(@doc)
friend.like(book)
assert_includes @user.liked_movies_in_common_with(friend), @movie
+ assert_includes @user.liked_movies_in_common_with(friend), @doc
assert_includes @user.liked_books_in_common_with(friend), book
refute_includes @user.liked_movies_in_common_with(friend), book
refute_includes @user.liked_books_in_common_with(friend), @movie
+ refute_includes @user.liked_books_in_common_with(friend), @doc
end
def teardown
3  test/recommendable/rater/recommender_test.rb
View
@@ -5,7 +5,8 @@ class RecommenderTest < MiniTest::Unit::TestCase
def setup
@user = Factory(:user)
5.times { |x| instance_variable_set(:"@user#{x+1}", Factory(:user)) }
- 10.times { |x| instance_variable_set(:"@movie#{x+1}", Factory(:movie)) }
+ 5.times { |x| instance_variable_set(:"@movie#{x+1}", Factory(:movie)) }
+ 5.upto(9) { |x| instance_variable_set(:"@movie#{x+1}", Factory(:documentary)) }
10.times { |x| instance_variable_set(:"@book#{x+1}", Factory(:book)) }
[@movie1, @movie2, @movie3, @book4, @book5, @book6].each { |obj| @user.like(obj) }
Please sign in to comment.
Something went wrong with that request. Please try again.