Sha256: b61bbedd43f144ebda3fd311d592b02ced581a009dadb1f54e7dc0cc93b737f5

Contents?: true

Size: 1.39 KB

Versions: 1

Compression:

Stored size: 1.39 KB

Contents

module RemoteDb
  module Concerns
    module Configurable
      extend ActiveSupport::Concern

      module ClassMethods
        def configure(config_hash = nil)
          if config_hash
            config_hash.each do |k,v|
              setter = "#{k}="
              if configuration.respond_to?(setter )
                configuration.send(setter , v)
              end
            end
          end

          yield(configuration) if block_given?
        end

        def configuration
          @configuration ||= RemoteDb::Configuration.new
        end

        def load_models!
          define_base_record
          send(:require_models) if respond_to?(:require_models)
        end

        private

        def define_base_record
          module_name = self.name
          connection_spec = configuration.connection_spec
          klass = Class.new(ActiveRecord::Base) do
            self.abstract_class = true

            # AR 4 requires AR models to be named.
            define_singleton_method(:name) { "#{module_name}::BaseRecord" }

            # Connect via custom db settings
            establish_connection(connection_spec)

            # Force model to be ready-only
            include Concerns::ReadOnlyModel

            # Scope which columns are exposed
            include Concerns::RestrictedColumns
          end

          self.const_set 'BaseRecord', klass
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
remote_db-0.0.4 lib/remote_db/concerns/configurable.rb