Sha256: f82a53d13ca0e9db300f6a1ecc163ef54f16e1adbbe1af7ff7e5e511429cf7ce

Contents?: true

Size: 1.14 KB

Versions: 27

Compression:

Stored size: 1.14 KB

Contents

# frozen_string_literal: true

require_dependency "renalware/system"

module Renalware
  module System
    class Message < ApplicationRecord
      extend Enumerize
      validates :body, presence: true
      validates :display_from, timeliness: { type: :datetime }, presence: true
      validates :display_until,
                timeliness: { type: :datetime, allow_blank: true, after: :display_from }
      enumerize :severity, in: %i(default warning info success)

      scope :active, lambda{
        where(
          "display_from <= ? and (display_until is null or display_until >= ?)",
          Time.zone.now,
          Time.zone.now
        )
      }

      # Return true if the message should be visible.
      # A message is visible if display_from is in the past and display_until is nil
      # or in the future
      def active?
        return false if display_from.nil?

        now = Time.zone.now
        self.display_until ||= far_future_date
        return true if display_from < now && display_until > now

        false
      end

      private

      def far_future_date
        Time.zone.parse("2999-01-01 00:00:01")
      end
    end
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
renalware-core-2.0.77 app/models/renalware/system/message.rb
renalware-core-2.0.76 app/models/renalware/system/message.rb
renalware-core-2.0.75 app/models/renalware/system/message.rb
renalware-core-2.0.74 app/models/renalware/system/message.rb
renalware-core-2.0.73 app/models/renalware/system/message.rb
renalware-core-2.0.72 app/models/renalware/system/message.rb
renalware-core-2.0.71 app/models/renalware/system/message.rb
renalware-core-2.0.70 app/models/renalware/system/message.rb
renalware-core-2.0.69 app/models/renalware/system/message.rb
renalware-core-2.0.68 app/models/renalware/system/message.rb
renalware-core-2.0.67 app/models/renalware/system/message.rb
renalware-core-2.0.64 app/models/renalware/system/message.rb
renalware-core-2.0.63 app/models/renalware/system/message.rb
renalware-core-2.0.62 app/models/renalware/system/message.rb
renalware-core-2.0.61 app/models/renalware/system/message.rb
renalware-core-2.0.60 app/models/renalware/system/message.rb
renalware-core-2.0.58 app/models/renalware/system/message.rb
renalware-core-2.0.57 app/models/renalware/system/message.rb
renalware-core-2.0.56 app/models/renalware/system/message.rb
renalware-core-2.0.55 app/models/renalware/system/message.rb