Skip to content

Commit

Permalink
Fixes methods defined on included modules after remountable APIs (#1818)
Browse files Browse the repository at this point in the history
  • Loading branch information
myxoh authored and dblock committed Nov 13, 2018
1 parent 87b6243 commit b14b196
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Expand Up @@ -4,7 +4,7 @@

* Your contribution here.
* [#1813](https://github.com/ruby-grape/grape/pull/1813): Add ruby 2.5 support, drop 2.2. Update rails version in travis - [@darren987469](https://github.com/darren987469).
* [#1803](https://github.com/ruby-grape/grape/pull/1803): Adds the ability to re-mount all endpoints in any location - [@myxoh](https://github.com/bschmeck).
* [#1803](https://github.com/ruby-grape/grape/pull/1803): Adds the ability to re-mount all endpoints in any location - [@myxoh](https://github.com/myxoh).
* [#1795](https://github.com/ruby-grape/grape/pull/1795): Fix vendor/subtype parsing of an invalid Accept header - [@bschmeck](https://github.com/bschmeck).
* [#1791](https://github.com/ruby-grape/grape/pull/1791): Support `summary`, `hidden`, `deprecated`, `is_array`, `nickname`, `produces`, `consumes`, `tags` options in `desc` block - [@darren987469](https://github.com/darren987469).

Expand Down
16 changes: 15 additions & 1 deletion lib/grape/api.rb
Expand Up @@ -7,7 +7,8 @@ module Grape
class API
# Class methods that we want to call on the API rather than on the API object
NON_OVERRIDABLE = %I[define_singleton_method instance_variable_set inspect class is_a? ! kind_of?
respond_to? const_defined? const_missing parent parent_name name equal? to_s parents].freeze
respond_to? respond_to_missing? const_defined? const_missing parent
parent_name name equal? to_s parents].freeze

class << self
attr_accessor :base_instance, :instances
Expand Down Expand Up @@ -81,6 +82,19 @@ def respond_to?(method, include_private = false)
super(method, include_private) || base_instance.respond_to?(method, include_private)
end

def respond_to_missing?(method, include_private = false)
base_instance.respond_to?(method, include_private)
end

def method_missing(method, *args, &block)
# If there's a missing method, it may be defined on the base_instance instead.
if respond_to_missing?(method)
base_instance.send(method, *args, &block)
else
super
end
end

private

# Adds a new stage to the set up require to get a Grape::API up and running
Expand Down
37 changes: 37 additions & 0 deletions spec/grape/api_spec.rb
Expand Up @@ -3209,6 +3209,43 @@ def static
expect { a.mount b }.to_not raise_error
end
end

context 'when including a module' do
let(:included_module) do
Module.new do
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
def my_method
@test = true
end
end
end
end

it 'should correctly include module in nested mount' do
module_to_include = included_module
v1 = Class.new(Grape::API) do
version :v1, using: :path
include module_to_include
my_method
end
v2 = Class.new(Grape::API) do
version :v2, using: :path
end
segment_base = Class.new(Grape::API) do
mount v1
mount v2
end

Class.new(Grape::API) do
mount segment_base
end

expect(v1.my_method).to be_truthy
end
end
end
end

Expand Down

0 comments on commit b14b196

Please sign in to comment.