Sha256: ba7e780a0bc846e43c2c1a8650a37d9ea4a5c44f12b4dc00340e26916187d2d6

Contents?: true

Size: 1.12 KB

Versions: 4

Compression:

Stored size: 1.12 KB

Contents

# frozen_string_literal: true

module SuperSettings
  module Storage
    class ActiveRecordStorage
      # Base class that the models extend from.
      class ApplicationRecord < ActiveRecord::Base
        self.abstract_class = true
      end

      class Model < ApplicationRecord
        self.table_name = "super_settings"

        has_many :history_items, class_name: "SuperSettings::Storage::ActiveRecordStorage::HistoryModel", foreign_key: :key, primary_key: :key

        class << self
          # ActiveRecord storage is only available if the connection pool is connected and the table exists.
          def available?
            connection_pool&.connected? && table_exists?
          end
        end
      end

      class HistoryModel < ApplicationRecord
        self.table_name = "super_settings_histories"

        # Since these models are created automatically on a callback, ensure that the data will
        # fit into the database columns since we can't handle any validation errors.
        before_validation do
          self.changed_by = changed_by.to_s[0, 150] if changed_by.present?
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
super_settings-2.2.1 lib/super_settings/storage/active_record_storage/models.rb
super_settings-2.2.0 lib/super_settings/storage/active_record_storage/models.rb
super_settings-2.1.2 lib/super_settings/storage/active_record_storage/models.rb
super_settings-2.1.1 lib/super_settings/storage/active_record_storage/models.rb