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



3
4
5
6
7
# File 'lib/active_graph/railtie.rb', line 3

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

#final_driver_config!(config) ⇒ Object



72
73
74
# File 'lib/active_graph/railtie.rb', line 72

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

#register_neo4j_cypher_loggingObject



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/active_graph/railtie.rb', line 88

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



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/active_graph/railtie.rb', line 56

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



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

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



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

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