Sha256: 8ff98d60878c1aefa706fd3c8cd0fe46563c9ce610b316a73de95f0fcb7d148c

Contents?: true

Size: 1.3 KB

Versions: 2

Compression:

Stored size: 1.3 KB

Contents

# frozen_string_literal: true

require 'active_support'

module NotificationSettings
  module Status
    extend ActiveSupport::Concern

    included do
      validates :status,
                inclusion: { in: NotificationSettings.configuration.statuses }

      include NotificationSettings::Status::InstanceMethods
    end

    module InstanceMethods
      def status
        self[:status] || default_status
      end

      private

      def default_status
        if idle? && !offline?
          'idle'
        elsif offline?
          'offline'
        else
          'online'
        end
      end

      def idle?
        return unless time_since_last_seen_round

        time_since_last_seen_round >= idle_after
      end

      def offline?
        return unless time_since_last_seen_round

        time_since_last_seen_round >= offline_after
      end

      def time_since_last_seen_round
        time_since_last_seen&.round
      end

      def time_since_last_seen
        return unless respond_to?(NotificationSettings.configuration.last_seen)

        Time.now - send(NotificationSettings.configuration.last_seen)
      end

      def idle_after
        NotificationSettings.configuration.idle_after
      end

      def offline_after
        NotificationSettings.configuration.offline_after
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
notification-settings-3.0.2 lib/notification_settings/status.rb
notification-settings-2.0.0 lib/notification_settings/status.rb