Class: ForestAdminRails::Engine

Inherits:
Rails::Engine
  • Object
show all
Extended by:
ActiveSupport::Concern
Defined in:
lib/forest_admin_rails/engine.rb

Instance Method Summary collapse

Instance Method Details

#check_create_agent_locationObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/forest_admin_rails/engine.rb', line 59

def check_create_agent_location
  old_path = Rails.root.join('app', 'lib', 'forest_admin_rails', 'create_agent.rb')
  new_path = Rails.root.join('lib', 'forest_admin_rails', 'create_agent.rb')

  return unless File.exist?(old_path) && !File.exist?(new_path)

  logger = ActiveSupport::Logger.new($stdout)
  logger.warn <<~WARNING
    ⚠️  DEPRECATION WARNING: create_agent.rb detected in old location!

    The file 'app/lib/forest_admin_rails/create_agent.rb' should now be located at:
    'lib/forest_admin_rails/create_agent.rb'

    Please move your file to the new location:
      mkdir -p lib/forest_admin_rails
      mv app/lib/forest_admin_rails/create_agent.rb lib/forest_admin_rails/create_agent.rb

    This will become a hard requirement in a future version.
  WARNING
end

#create_agent_file_exists?Boolean

Returns:

  • (Boolean)


80
81
82
83
84
85
# File 'lib/forest_admin_rails/engine.rb', line 80

def create_agent_file_exists?
  old_path = Rails.root.join('app', 'lib', 'forest_admin_rails', 'create_agent.rb')
  new_path = Rails.root.join('lib', 'forest_admin_rails', 'create_agent.rb')

  File.exist?(new_path) || File.exist?(old_path)
end

#load_configurationObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/forest_admin_rails/engine.rb', line 44

def load_configuration
  return unless running_web_server?

  check_create_agent_location
  return unless create_agent_file_exists?

  # force eager loading models
  Rails.application.eager_load!

  setup_agent_and_cache_routes

  sse = ForestAdminAgent::Services::SSECacheInvalidation
  sse.run if ForestAdminRails.config[:instant_cache_refresh]
end

#load_corsObject



120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/forest_admin_rails/engine.rb', line 120

def load_cors
  return if ENV['FOREST_CORS_DEACTIVATED']

  config.middleware.insert_before 0, Rack::Cors do
    allow do
      hostnames = [/\A.*\.forestadmin\.com\z/]
      hostnames += ENV['CORS_ORIGINS'].split(',') if ENV['CORS_ORIGINS']

      origins hostnames
      resource '*', headers: :any, methods: :any, credentials: true, max_age: 86_400
    end
  end
end

#precache_routesObject



96
97
98
99
100
101
102
103
104
105
# File 'lib/forest_admin_rails/engine.rb', line 96

def precache_routes
  return if ForestAdminAgent::Http::Router.cache_disabled?

  start_time = Time.now
  routes = ForestAdminAgent::Http::Router.cached_routes
  elapsed = ((Time.now - start_time) * 1000).round(2)

  logger = ActiveSupport::Logger.new($stdout)
  logger.info("[ForestAdmin] Successfully pre-cached #{routes.size} routes in #{elapsed}ms")
end

#running_web_server?Boolean

Returns:

  • (Boolean)


107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/forest_admin_rails/engine.rb', line 107

def running_web_server?
  return true if defined?(::Rails::Server)

  # check if a web server is running with the given command line arguments
  server_commands = %w[puma unicorn thin passenger rackup]
  return true if server_commands.any? { |cmd| ARGV.any? { |arg| arg.include?(cmd) } }

  # check if running via common server executables
  return true if $PROGRAM_NAME.match?(/puma|unicorn|thin|passenger/)

  false
end

#setup_agent_and_cache_routesObject



87
88
89
90
91
92
93
94
# File 'lib/forest_admin_rails/engine.rb', line 87

def setup_agent_and_cache_routes
  ForestAdminRails::CreateAgent.setup!
  precache_routes
rescue StandardError => e
  logger = ActiveSupport::Logger.new($stdout)
  logger.warn 'An error has occurred during setup of the Forest Admin agent.'
  raise e.message
end