/
engine.rb
78 lines (64 loc) · 3.44 KB
/
engine.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
###########################################
###########################################
## _____ _ ##
## | ___| (_) ##
## | |__ _ __ __ _ _ _ __ ___ ##
## | __| '_ \ / _` | | '_ \ / _ \ ##
## | |__| | | | (_| | | | | | __/ ##
## \____/_| |_|\__, |_|_| |_|\___| ##
## __/ | ##
## |___/ ##
###########################################
###########################################
module ExceptionHandler
class Engine < Rails::Engine
# => Rails default MIME types:
# => http://apidock.com/rails/ActionController/MimeResponds/InstanceMethods/respond_to#14-Rails-defined-Mime-Types
#########################################################
#########################################################
# => Wraps helpers in ExceptionHandler module
# => http://guides.rubyonrails.org/engines.html#inside-an-engine
# => http://stackoverflow.com/questions/31877839/accessing-helpers-from-the-parent-app-in-an-isolated-rails-engine
isolate_namespace ExceptionHandler
# => Tests
config.generators do |g|
g.test_framework :rspec
end
# => Assets
# => For Sprockets 4, had to include link_tree in exception_handler.css
config.assets.precompile << "exception_handler.css"
#########################################################
#########################################################
# => Config
# => Builds lib/exception_handler/config.rb
# => config.before_initialize do |app| => Needs to be fixed for ActiveRecord::Base
# => to support later version of config, "with_indifferent_access" used in config.rb
initializer :exception_handler_config, before: "better_errors.configure_rails_initialization" do |app|
ExceptionHandler.config ||= ExceptionHandler::Config.new config.try(:exception_handler)
end
#########################################################
#########################################################
# => Middleware
# => This should be config.before_initialize but because ActiveRecord is not initialized, cannot check for table
initializer :exception_handler, before: "better_errors.configure_rails_initialization" do |app|
app.config.exceptions_app = ->(env) { ExceptionHandler::ExceptionsController.action(:show).call(env) }
app.config.consider_all_requests_local = !ExceptionHandler.config.try(:dev) if Rails.env.development? || Rails.env.test?
end
# => Custom Exceptions
# => This just mimicks standard Rails behaviour
# => Look for "config.action_dispatch.rescue_responses" for more info)
initializer :exception_handler_custom_exceptions do |app|
app.config.action_dispatch.rescue_responses.merge! ExceptionHandler.config.custom_exceptions if ExceptionHandler.config.custom_exceptions
end
# => Migrations
# => This has to be kept in an initializer (to access app)
# => https://blog.pivotal.io/labs/labs/leave-your-migrations-in-your-rails-engines
initializer :migration_paths do |app|
config.paths["db/migrate"].expanded.each do |expanded_path|
app.config.paths["db/migrate"] << expanded_path if ExceptionHandler.config.try(:db)
end
end
#########################################################
#########################################################
end
end