I'm creating rails app. I add to devise user colums: total_online and last_seen to track their online total time.
app/models/user.rb
class User < ApplicationRecord
devise :session_limitable, :database_authenticatable, :registerable, :confirmable,
:recoverable, :rememberable, :trackable, :validatable
validates :full_name, presence: true
def active_now!
if (Time.now - self.last_seen) <= 3.minutes
self.total_online += (Time.now - self.last_seen)
end
self.last_seen = Time.now
save!
end
end
app/controller/application_controller.rb
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
responders :flash
respond_to :html
before_action :record_user_activity
private
def record_user_activity
current_user.active_now! if current_user
end
end
The main idea - user should do any actions on pages in 3 minutes to save this time to total_online. But I have a video watching pages, and I want to calculate the time, which user was on the video page.(when user is watching videos he doesn't do any actions, so total_online not updated). I guess the time on this pages should calculate separetely and then added to total_online. How can I do this?