Class: ActiveGraph::Railtie

Inherits:
Rails::Railtie
  • Object
show all
Defined in:
lib/active_graph/railtie.rb

Instance Method Summary collapse

Instance Method Details

#empty_configObject


9
10
11
12
13
# File 'lib/active_graph/railtie.rb', line 9

def empty_config
  ActiveSupport::OrderedOptions.new.tap do |cfg|
    cfg.driver = ActiveSupport::OrderedOptions.new
  end
end

#final_driver_config!(config) ⇒ Object


78
79
80
# File 'lib/active_graph/railtie.rb', line 78

def final_driver_config!(config)
  { url: ENV['NEO4J_URL'] }.compact.merge(config[:driver]).merge(yaml_config_data)
end

#register_neo4j_cypher_loggingObject


94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/active_graph/railtie.rb', line 94

def register_neo4j_cypher_logging
  return if @neo4j_cypher_logging_registered

  ActiveGraph::Core::Query.pretty_cypher = ActiveGraph::Config[:pretty_logged_cypher_queries]

  logger_proc = ->(message) do
    (ActiveGraph::Config[:logger] ||= Rails.logger).debug message
  end
  ActiveGraph::Base.subscribe_to_query(&logger_proc)
  ActiveGraph::Base.subscribe_to_request(&logger_proc)

  @neo4j_cypher_logging_registered = true
end

#setup!(config = empty_config) ⇒ Object


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/active_graph/railtie.rb', line 62

def setup!(config = empty_config)
  config = final_driver_config!(config)
  scheme = config.delete(:scheme) || 'bolt'
  host = config.delete(:host) || 'localhost'
  port = config.delete(:port) || 7687
  url = config.delete(:url) || URI::Generic.build( scheme: scheme, host: host, port: port ).to_s
  username = config.delete(:username)
  password = config.delete(:password)
  auth_token = config.delete(:auth_token)
  auth_token ||= username ? Neo4j::Driver::AuthTokens.basic(username, password) : Neo4j::Driver::AuthTokens.none
  register_neo4j_cypher_logging

  method = url.is_a?(Enumerable) ? :routing_driver : :driver
  Neo4j::Driver::GraphDatabase.send(method, url, auth_token, config)
end

#yaml_config_dataObject


82
83
84
85
# File 'lib/active_graph/railtie.rb', line 82

def yaml_config_data
  @yaml_config_data ||=
    yaml_path ? YAML.load(ERB.new(yaml_path.read).result)[Rails.env].transform_keys!(&:to_sym) : {}
end

#yaml_pathObject


87
88
89
90
91
92
# File 'lib/active_graph/railtie.rb', line 87

def yaml_path
  return unless defined?(Rails)
  @yaml_path ||= %w(config/neo4j.yml config/neo4j.yaml).map do |path|
    Rails.root.join(path)
  end.detect(&:exist?)
end