class ProfileVisit

Schema Information

Table name: profile_visits

id          :integer          not null, primary key
by_user_id  :integer
count       :integer          default(0)
created_at  :datetime
updated_at  :datetime
for_user_id :integer
university_id  :integer

Public Class Methods

increment!(for_user, by_user = nil) click to toggle source

To increment profile visit count

# File app/models/profile_visit.rb, line 23
def self.increment!(for_user, by_user = nil)
  return false if for_user == by_user
  profile_visit = find_or_initialize_by(for_user_id: for_user.id, by_user_id: by_user.try(:id))
  unless profile_visit.by_user_id and profile_visit.visited_today?
    profile_visit.count += 1
    profile_visit.save
  end
end
visits_count_for(user, by = nil) click to toggle source

It returns profile visit count for user.

# File app/models/profile_visit.rb, line 33
def self.visits_count_for(user, by = nil)
  visits = user.profile_visits
  visits = visits.where(by_user_id: by.id)
  visits.sum(:count)
end

Public Instance Methods

profile_notification() click to toggle source

Create profile visit notification.

# File app/models/profile_visit.rb, line 45
def profile_notification
  self.create_activity key: 'profile_visit.create', owner: by_user, recipient: for_user, type: 'Notification'
end
visited_today?() click to toggle source

Checks if profile visited today.

# File app/models/profile_visit.rb, line 40
def visited_today?
  updated_at and updated_at.in_time_zone.to_date == Date.today_in_time_zone
end