Class: UserLink
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- UserLink
- Includes:
- Toggleable
- Defined in:
- app/models/user_link.rb
Constant Summary collapse
- PER_PAGE =
10
Class Method Summary collapse
- .filtered(side, filter) ⇒ Object
- .follow(follower, followee) ⇒ Object
- .page_for_user(page) ⇒ Object
- .unfollow(follower, followee) ⇒ Object
Instance Method Summary collapse
Class Method Details
.filtered(side, filter) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'app/models/user_link.rb', line 45 def self.filtered(side, filter) link = joins(side).where("users.id = user_links.#{side}_id") unless filter[:screen_name].blank? link = link.where('users.screen_name ilike ?', "%#{filter[:screen_name]}%") end unless filter[:name].blank? link = link.where('users.name ilike ?', "%#{filter[:name]}%") end unless filter[:surname].blank? link = link.where('users.surname ilike ?', "%#{filter[:surname]}%") end unless filter[:online].blank? link = link.where('users.last_seen >= ?', 3.minutes.ago) end link end |
.follow(follower, followee) ⇒ Object
26 27 28 29 30 31 32 33 34 35 |
# File 'app/models/user_link.rb', line 26 def self.follow(follower, followee) if follower.is_a?(User) && followee.is_a?(User) criteria = { follower_id: follower.id, followee_id: followee.id } link = find_or_create_by(criteria) link.reciprocal.update(visible: true) if link.mutual? link else raise "#{follower.class}, #{followee.class}" end end |
.page_for_user(page) ⇒ Object
20 21 22 |
# File 'app/models/user_link.rb', line 20 def self.page_for_user(page) recent.page(page).per(PER_PAGE) end |
.unfollow(follower, followee) ⇒ Object
39 40 41 |
# File 'app/models/user_link.rb', line 39 def self.unfollow(follower, followee) with_follower(follower).with_followee(followee).destroy_all end |
Instance Method Details
#mutual? ⇒ Boolean
62 63 64 |
# File 'app/models/user_link.rb', line 62 def mutual? reciprocal.exists? end |
#reciprocal ⇒ Object
66 67 68 |
# File 'app/models/user_link.rb', line 66 def reciprocal UserLink.where(follower: followee, followee: follower) end |