Class: SourceMonitor::Setup::InitializerPatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/source_monitor/setup/initializer_patcher.rb

Constant Summary collapse

AUTH_MARKER =
"# ---- Authentication".freeze

Instance Method Summary collapse

Constructor Details

#initialize(path: "config/initializers/source_monitor.rb") ⇒ InitializerPatcher

Returns a new instance of InitializerPatcher.



10
11
12
# File 'lib/source_monitor/setup/initializer_patcher.rb', line 10

def initialize(path: "config/initializers/source_monitor.rb")
  @path = Pathname.new(path)
end

Instance Method Details

#ensure_devise_hooksObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/source_monitor/setup/initializer_patcher.rb', line 26

def ensure_devise_hooks
  return false unless path.exist?

  contents = path.read
  return false if contents.include?("config.authentication.authenticate_with :authenticate_user!")

  snippet = indent(devise_snippet)
  updated = if contents.include?(AUTH_MARKER)
    contents.sub(AUTH_MARKER, "#{AUTH_MARKER}\n#{snippet}")
  else
    contents.sub(/SourceMonitor.configure do \|config\|/m, "\\0\n#{snippet}")
  end

  path.write(updated)
  true
end

#ensure_navigation_hint(mount_path: Workflow::DEFAULT_MOUNT_PATH) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/source_monitor/setup/initializer_patcher.rb', line 14

def ensure_navigation_hint(mount_path: Workflow::DEFAULT_MOUNT_PATH)
  return false unless path.exist?

  comment = "# Mount SourceMonitor at #{mount_path} so admins can find the dashboard."
  contents = path.read
  return false if contents.include?(comment)

  updated = contents.sub("# SourceMonitor engine configuration.", "# SourceMonitor engine configuration.\n#{comment}")
  path.write(updated)
  true
end