Sha256: 758403dce1ca5fa44f97af986801602458d32bd09db52f705cb2b9a05c20f1a4

Contents?: true

Size: 1.42 KB

Versions: 1

Compression:

Stored size: 1.42 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
          connection_spec = configuration.connection_spec
          klass = Class.new(ActiveRecord::Base) do
            self.abstract_class = true

            # 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

          # AR 4 requires AR models to be named.
          module_name = self.name
          klass.define_singleton_method(:name) do
            "#{module_name}::BaseRecord"
          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.3 lib/remote_db/concerns/configurable.rb