Class: Adhearsion::Rails::Plugin::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/adhearsion/rails/plugin/service.rb

Class Method Summary collapse

Class Method Details

.configObject

Access to Rails plugin configuration



49
50
51
# File 'lib/adhearsion/rails/plugin/service.rb', line 49

def config
  @config ||= Adhearsion.config[:adhearsion_rails]
end

.load_env_file(file) ⇒ Object



43
44
45
# File 'lib/adhearsion/rails/plugin/service.rb', line 43

def load_env_file file
  require file
end

.load_railsObject

Loads the Rails environment



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/adhearsion/rails/plugin/service.rb', line 30

def load_rails
  environment_file = File.expand_path("#{rails_path}/config/environment.rb")
  raise "There is no config/environment.rb file!" unless File.exists?(environment_file)
  if config.environment.nil? || config.environment.to_s.empty?
    msg = "Unable to load Rails. Unexpected nil environment."
    logger.fatal msg
    raise msg
  end
  ENV['RAILS_ENV'] = config.environment.to_s
  load_env_file environment_file
  logger.info "Rails loaded"
end

.rails_pathObject

Returns the absolute Rails root path based on the path config value

i.e.

Adhearsion.config[:adhearsion_rails].path = "/foo/bar/../bazz"
puts Adhearsion::Rails::Plugin::Service.rails_path => "/foo/bazz"


59
60
61
# File 'lib/adhearsion/rails/plugin/service.rb', line 59

def rails_path
  File.expand_path(config.path)
end

.startObject

Start the service



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/adhearsion/rails/plugin/service.rb', line 10

def start
  if File.directory?(rails_path)
    load_rails

    if defined? ActiveRecord
      # You may need to uncomment the following line for older versions of ActiveRecord
      # ActiveRecord::Base.allow_concurrency = true
      Adhearsion::Events.register_callback :before_call do
        ActiveRecord::Base.verify_active_connections!
      end
    end
  else
    msg = "Unable to load Rails. Invalid rails root path: #{rails_path.inspect}."
    logger.fatal msg
    raise msg
  end
end