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



16
17
18
19
20
# File 'lib/active_graph/railtie.rb', line 16

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

#final_driver_config!(config) ⇒ Object



85
86
87
# File 'lib/active_graph/railtie.rb', line 85

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

#register_neo4j_cypher_loggingObject



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/active_graph/railtie.rb', line 101

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



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/active_graph/railtie.rb', line 69

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



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

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



94
95
96
97
98
99
# File 'lib/active_graph/railtie.rb', line 94

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