Sha256: 1d67c75d626a8a515a6481ac514e2187b745e10dbd3661e06d385f2f593c2491

Contents?: true

Size: 1.14 KB

Versions: 87

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

87 entries across 87 versions & 1 rubygems

Version Path
renalware-core-2.1.1 app/models/renalware/system/message.rb
renalware-core-2.1.0 app/models/renalware/system/message.rb
renalware-core-2.0.167 app/models/renalware/system/message.rb
renalware-core-2.0.166 app/models/renalware/system/message.rb
renalware-core-2.0.165 app/models/renalware/system/message.rb
renalware-core-2.0.164 app/models/renalware/system/message.rb
renalware-core-2.0.163 app/models/renalware/system/message.rb
renalware-core-2.0.162 app/models/renalware/system/message.rb
renalware-core-2.0.161 app/models/renalware/system/message.rb
renalware-core-2.0.160 app/models/renalware/system/message.rb
renalware-core-2.0.159 app/models/renalware/system/message.rb
renalware-core-2.0.158 app/models/renalware/system/message.rb
renalware-core-2.0.157 app/models/renalware/system/message.rb
renalware-core-2.0.156 app/models/renalware/system/message.rb
renalware-core-2.0.155 app/models/renalware/system/message.rb
renalware-core-2.0.153 app/models/renalware/system/message.rb
renalware-core-2.0.152 app/models/renalware/system/message.rb
renalware-core-2.0.151 app/models/renalware/system/message.rb
renalware-core-2.0.149 app/models/renalware/system/message.rb
renalware-core-2.0.148 app/models/renalware/system/message.rb