Sha256: fd45e7b7e000b6634877a4de69dcd0353f7ca92266c65d4edcb11441f40218da

Contents?: true

Size: 1.28 KB

Versions: 1

Compression:

Stored size: 1.28 KB

Contents

require 'active_record'
require 'postfix_admin/models/concerns/existing_timestamp'

module PostfixAdmin
  class ApplicationRecord < ActiveRecord::Base
    self.abstract_class = true

    include ExistingTimestamp

    RE_DOMAIN_NAME_LIKE_BASE = '([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,}'
    RE_EMAIL_LIKE_BASE = '[^@\s]+@([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,}'

    RE_DOMAIN_NAME_LIKE = /#{RE_DOMAIN_NAME_LIKE_BASE}/
    RE_EMAIL_LIKE = /#{RE_EMAIL_LIKE_BASE}/

    RE_DOMAIN_NAME_LIKE_WITH_ANCHORS = /\A#{RE_DOMAIN_NAME_LIKE_BASE}\z/
    RE_EMAIL_LIKE_WITH_ANCHORS = /\A#{RE_EMAIL_LIKE_BASE}\z/

    scope :active, -> { where(active: true) }

    def inactive?
      !active?
    end

    def active_str
      active? ? "Active" : "Inactive"
    end

    # This is a workaround to set current time to timestamp columns when a record is created.
    # Activerecord does not insert timestamps if default values are set for the columns.
    before_create :set_current_time_to_timestamp_columns, if: :has_timestamp_columns?

    def set_current_time_to_timestamp_columns
      now = Time.now
      self.created = now
      self.modified = now
    end

    def has_timestamp_columns?
      column_names = self.class.column_names
      column_names.include?("created") && column_names.include?("modified")
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
postfix_admin-0.3.1 lib/postfix_admin/models/application_record.rb