Skip to content

Commit

Permalink
Merge pull request #1976 from ruby-grape/bugfix/autoload
Browse files Browse the repository at this point in the history
Be sure classes/modules listed for autoload really exist
  • Loading branch information
dblock authored Jan 16, 2020
2 parents 6d2e332 + 752bf4c commit cacf82e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#### Fixes

* [#1976](https://github.com/ruby-grape/grape/pull/1976): Ensure classes/modules listed for autoload really exist - [@dnesteryuk](https://github.com/dnesteryuk).
* [#1971](https://github.com/ruby-grape/grape/pull/1971): Fix BigDecimal coercion - [@FlickStuart](https://github.com/FlickStuart).
* [#1968](https://github.com/ruby-grape/grape/pull/1968): Fix args forwarding in Grape::Middleware::Stack#merge_with for ruby 2.7.0 - [@dm1try](https://github.com/dm1try).

Expand Down
1 change: 0 additions & 1 deletion lib/grape.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ module Extensions
eager_autoload do
autoload :DeepMergeableHash
autoload :DeepSymbolizeHash
autoload :DeepHashWithIndifferentAccess
autoload :Hash
end
module ActiveSupport
Expand Down
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
require file
end

eager_load!

# The default value for this setting is true in a standard Rails app,
# so it should be set to true here as well to reflect that.
I18n.enforce_available_locales = true
Expand Down
19 changes: 19 additions & 0 deletions spec/support/eager_load.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

# Grape uses autoload https://api.rubyonrails.org/classes/ActiveSupport/Autoload.html.
# When a class/module get added to the list, ActiveSupport doesn't check whether it really exists.
# This method loads all classes/modules defined via autoload to be sure only existing
# classes/modules were listed.
def eager_load!(scope = Grape)
# get modules
scope.constants.each do |const_name|
const = scope.const_get(const_name)

next unless const.respond_to?(:eager_load!)

const.eager_load!

# check its modules, they might need to be loaded as well.
eager_load!(const)
end
end

0 comments on commit cacf82e

Please sign in to comment.