Skip to content

Commit

Permalink
Fix README.markdown instructions for reloading.
Browse files Browse the repository at this point in the history
Original instructions didn't seem to work on Rails 3.2.x. Followed instructions
on wiki
(https://github.com/intridea/grape/wiki/Reload-Grape-API-on-every-request-in-Rails)
and it works without issue. In the original instructions, I could only get grape
to reload my endpoint once.

Using this submitted approach looks also looks like a standard way to hook
reloading into any inbound Rails request.
  • Loading branch information
jyn committed Jan 25, 2013
1 parent 9f0ac55 commit 3b4e26e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 27 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Next Release
* [#309](https://github.com/intridea/grape/pull/309): An XML format API will return an error instead of returning a string representation of the response if the latter cannot be converted to XML - [@dblock](http://github.com/dblock).
* [#309](https://github.com/intridea/grape/pull/309): Added XML support to entities - [@johnnyiller](https://github.com/johnnyiller), [@dblock](http://github.com/dblock).
* A formatter that raises an exception will cause the API to return a 500 error - [@dblock](http://github.com/dblock).
* [#131](https://github.com/intridea/grape/issues/131) Fix README.markdown instructions regarding Rails 3.x reloading. Using instructions from Wiki works better - [@jyn](http://github.com/jyn)
* Your contribution here.

0.2.6 (01/11/2013)
Expand Down
42 changes: 15 additions & 27 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -1002,41 +1002,29 @@ end

### Rails 3.x

Add the following to `config/environments/development.rb`:

``` ruby
@last_api_change = Time.now
api_files = Dir["#{Rails.root}/app/api/**/*.rb"] # adjust the path to your API files
api_reloader = ActiveSupport::FileUpdateChecker.new(api_files) do |reloader|
times = api_files.map { |f| File.mtime(f) }

Rails.logger.debug "! Change detected: reloading following files:"
api_files.each_with_index do |s,i|
if times[i] > @last_api_change
Rails.logger.debug " - #{s}"
load s
end
end
Source: http://stackoverflow.com/questions/3282655/ruby-on-rails-3-reload-lib-directory-for-each-request/4368838#4368838

Rails.application.reload_routes!
Rails.application.routes_reloader.reload!
Rails.application.eager_load!
end
Create file `config/initializers/reload_lib.rb`

```ruby
if Rails.env.development?
lib_reloader = ActiveSupport::FileUpdateChecker.new(Dir["app/api/**/*"]) do
Rails.application.reload_routes!
end

ActionDispatch::Reloader.to_prepare do
api_reloader.execute_if_updated
ActionDispatch::Callbacks.to_prepare do
lib_reloader.execute_if_updated
end
end
```

Add the following to `config/routes.rb`:
In `config/application.rb`, add

``` ruby
require_dependency "app/api/api.rb" if Rails.env.development?
```ruby
config.autoload_paths += %W(#{config.root}/app/api)
config.autoload_paths += Dir["#{config.root}/app/api/**/"]
```

Additional `require_dependency` entries are also required for every mounted Grape endpoint.


## Performance Monitoring

Grape integrates with NewRelic via the [newrelic-grape](https://github.com/flyerhzm/newrelic-grape) gem.
Expand Down

0 comments on commit 3b4e26e

Please sign in to comment.