Created
November 22, 2012 09:53
-
-
Save pikender/4130310 to your computer and use it in GitHub Desktop.
Reload Grape API in Development Environment for Rails 3.1 and higher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
INSPIRATION: https://github.com/intridea/grape/issues/131#issuecomment-10413342 | |
File: config/application.rb | |
config.autoload_paths += %W(#{config.root}/app/api #{config.root}/app/api/*/*) | |
File: config/environments/development.rb | |
# Add below code before DummyApp::Application.configure BLOCK | |
@last_api_change = Time.now | |
api_reloader = ActiveSupport::FileUpdateChecker.new(Dir["#{Rails.root}/app/api/**/*.rb"]) do |reloader| | |
times = Dir["#{Rails.root}/app/api/**/*.rb"].map{|f| File.mtime(f) } | |
files = Dir["#{Rails.root}/app/api/**/*.rb"].map{|f| f } | |
Rails.logger.debug "! Change detected: reloading following files:" | |
files.each_with_index do |s,i| | |
if times[i] > @last_api_change | |
Rails.logger.debug " - #{s}" | |
# load s | |
require_dependency s | |
end | |
end | |
Rails.application.reload_routes! | |
Rails.application.routes_reloader.reload! | |
Rails.application.eager_load! | |
end | |
ActionDispatch::Reloader.to_prepare do | |
api_reloader.execute_if_updated | |
end | |
FILE: config/routes.rb | |
# Use require_dependency instead of require to include the Grape Api file | |
require_dependency 'module_name(as in your application)/grape_api_file_name' | |
NOTE: To be on safer side, I used require_dependency to load grape too in grape_api_file_name. | |
FILE: app/api/module_name/grape_api_file_name.rb | |
require_dependency 'grape' | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment