Skip to content
Snippets Groups Projects
Commit 9e5a8662 authored by Mauricio Giacomini Girardello's avatar Mauricio Giacomini Girardello
Browse files

refactoring followable concern and user model

parent c4fd355a
No related branches found
No related tags found
No related merge requests found
...@@ -5,16 +5,9 @@ module Followable ...@@ -5,16 +5,9 @@ module Followable
has_many :follows, as: :followable, dependent: :destroy has_many :follows, as: :followable, dependent: :destroy
end end
def follow(user) # Retrieve the user followers
Follow.create(user: user, followable: self) def followers
end Follow.where(followable: self)
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?
end end
end end
...@@ -45,10 +45,6 @@ class User < ActiveRecord::Base ...@@ -45,10 +45,6 @@ class User < ActiveRecord::Base
} }
end end
def followers
Follow.where(followable: self)
end
def is_admin? def is_admin?
roles.each do |role| roles.each do |role|
return true if role.name == "admin" return true if role.name == "admin"
...@@ -56,6 +52,31 @@ class User < ActiveRecord::Base ...@@ -56,6 +52,31 @@ class User < ActiveRecord::Base
false false
end 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 private
def default_role def default_role
......
ufpr:
name: 'UFPR collection'
owner: john (User)
privacy: 'public'
\ No newline at end of file
john: john:
name: 'John' name: 'John'
email: 'john@test.com' email: 'john@test.com'
encrypted_password: 'test123455'
jack:
name: 'Jack'
email: 'jack@rackan.com'
one: one:
name: 'John' name: 'John'
......
...@@ -3,20 +3,55 @@ require 'test_helper' ...@@ -3,20 +3,55 @@ require 'test_helper'
class UserTest < ActiveSupport::TestCase class UserTest < ActiveSupport::TestCase
should have_and_belong_to_many(:roles) should have_and_belong_to_many(:roles)
should have_and_belong_to_many(:institutions) should have_and_belong_to_many(:institutions)
should have_many :bookmarks should have_many :bookmarks
should have_many(:bookmark_collections).through(:bookmarks).source(:bookmarkable) should have_many(:bookmark_collections).through(:bookmarks).source(:bookmarkable)
should have_many(:bookmark_learning_objects).through(:bookmarks).source(:bookmarkable) should have_many(:bookmark_learning_objects).through(:bookmarks).source(:bookmarkable)
should have_many :collections should have_many :collections
should have_many :learning_objects should have_many :learning_objects
should have_many :views should have_many :views
should have_many :downloads should have_many :downloads
should have_many :likes should have_many :likes
should have_many :shares should have_many :shares
should have_many :follows should have_many :follows
should have_many :reviews 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 end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment