diff --git a/app/models/concerns/followable.rb b/app/models/concerns/followable.rb index 7c375f5955008b9ccdc19f2bcaadd4eed12d8749..c5485dbe1292c2669b121ddeba89891786378b87 100644 --- a/app/models/concerns/followable.rb +++ b/app/models/concerns/followable.rb @@ -5,16 +5,9 @@ module Followable has_many :follows, as: :followable, dependent: :destroy end - def follow(user) - Follow.create(user: user, followable: self) - end - - def unfollow(user) - Follow.where(user: user, followable: self).destroy_all - end - - def following?(followable) - !follows.where(followable_id: followable.id, followable_type: followable.class.name).blank? + # Retrieve the user followers + def followers + Follow.where(followable: self) end end diff --git a/app/models/user.rb b/app/models/user.rb index dc8ee7073d69c6dc2ffabdf81920502ddd7e067b..8d904eae26210db48d6218b4ff6939acae88932a 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -45,10 +45,6 @@ class User < ActiveRecord::Base } end - def followers - Follow.where(followable: self) - end - def is_admin? roles.each do |role| return true if role.name == "admin" @@ -56,6 +52,31 @@ class User < ActiveRecord::Base false end + # ~~~~ followable actions ~~~~ # + # An user can follow anothers users and collections + # Examples: + # current_user.follow(User.find_by_email('john@revelator.com')) + # current_user.follow(Collection.find(143)) + def follow(followable) + Follow.create(user: self, followable: followable) + end + + # Examples: + # current_user.unfollow(User.find_by_email('john@revelator.com')) + # current_user.unfollow(Collection.find(143)) + def unfollow(followable) + Follow.where(user: self, followable: followable).destroy_all + end + + # An user can see if is following another user + # Examples: + # current_user.following?(User.find_by_email('john@revelator.com')) => true + # current_user.following?(Collection.find(143)) => true + def following?(followable) + !follows.where(followable_id: followable.id, followable_type: followable.class.name).blank? + end + # ~~~~ end followable actions ~~~~ # + private def default_role diff --git a/test/fixtures/collections.yml b/test/fixtures/collections.yml new file mode 100644 index 0000000000000000000000000000000000000000..28f25d7ebcf69319f199778477372240267ec665 --- /dev/null +++ b/test/fixtures/collections.yml @@ -0,0 +1,4 @@ +ufpr: + name: 'UFPR collection' + owner: john (User) + privacy: 'public' \ No newline at end of file diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml index 4e56b618ff88b9d008b0d8bcdd530f484a2805d2..946f2e1c9bb939c07bf64a21bb34344402baf68d 100644 --- a/test/fixtures/users.yml +++ b/test/fixtures/users.yml @@ -1,7 +1,10 @@ john: name: 'John' email: 'john@test.com' - encrypted_password: 'test123455' + +jack: + name: 'Jack' + email: 'jack@rackan.com' one: name: 'John' diff --git a/test/models/user_test.rb b/test/models/user_test.rb index cab668434eedc452fce840879fabd203d3c88a1a..3341668e8b4e6a24a0ad73d64600eb069ee92905 100644 --- a/test/models/user_test.rb +++ b/test/models/user_test.rb @@ -3,20 +3,55 @@ require 'test_helper' class UserTest < ActiveSupport::TestCase should have_and_belong_to_many(:roles) should have_and_belong_to_many(:institutions) - should have_many :bookmarks should have_many(:bookmark_collections).through(:bookmarks).source(:bookmarkable) should have_many(:bookmark_learning_objects).through(:bookmarks).source(:bookmarkable) - should have_many :collections - should have_many :learning_objects - should have_many :views should have_many :downloads should have_many :likes should have_many :shares should have_many :follows - should have_many :reviews + + test 'an user can follow another user' do + john = users(:john) + jack = users(:jack) + assert john.follow(jack) + + # check jack followers + assert_count 1, jack.followers + assert_count 1, john.follows + assert john.following? jack + end + + test 'an user can follow a collection' do + john = users(:john) + ufpr_collection = collections(:ufpr) + assert john.follow(ufpr_collection) + + # check ufpr followers + assert_count 1, ufpr_collection.followers + assert_count 1, john.follows + assert john.following? ufpr_collection + end + + test 'an user can unfollow a collection' do + john = users(:john) + ufpr_collection = collections(:ufpr) + assert john.follow(ufpr_collection) + + # check ufpr followers + assert_count 1, ufpr_collection.followers + assert_count 1, john.follows + assert john.following? ufpr_collection + + john.unfollow(ufpr_collection) + + # check ufpr followers + assert_count 0, ufpr_collection.followers + assert_count 0, john.follows + assert_not john.following? ufpr_collection + end end