Class: AMEE::Rails::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/amee/rails.rb

Class Method Summary collapse

Class Method Details

.global(options = {}) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/amee/rails.rb', line 12

def self.global(options = {})
  unless defined?($AMEE_CONFIG)
    amee_config = "#{::Rails.root}/config/amee.yml"
    if File.exist? amee_config
      $AMEE_CONFIG = AMEE::Config.setup(amee_config, ::Rails.env)
    else
      $AMEE_CONFIG = AMEE::Config.setup
    end
  end
  unless @connection
    $AMEE_CONFIG ||= {} # Make default values nil
    if $AMEE_CONFIG[:ssl] == false
      options.merge! :ssl => false
    end
    if $AMEE_CONFIG[:retries]
      options.merge! :retries => $AMEE_CONFIG[:retries].to_i
    end
    if $AMEE_CONFIG[:timeout]
      options.merge! :timeout => $AMEE_CONFIG[:timeout].to_i
    end
    if $AMEE_CONFIG[:cache] == 'rails'
      # Pass in the rails cache store
      options[:cache_store] = ActionController::Base.cache_store
    else
      options[:cache] ||= $AMEE_CONFIG[:cache] if $AMEE_CONFIG[:cache].present?
    end
    options[:enable_debug]   ||= $AMEE_CONFIG[:debug] if $AMEE_CONFIG[:debug].present?
    @connection = self.connect($AMEE_CONFIG[:server], $AMEE_CONFIG[:username], $AMEE_CONFIG[:password], options)
    # Also store as $amee for backwards compatibility, though this is now deprecated
    $amee = @connection
  end
  @connection
end