File tree 3 files changed +48
-12
lines changed
3 files changed +48
-12
lines changed Original file line number Diff line number Diff line change 8
8
9
9
* [ #2176 ] ( https://github.com/ruby-grape/grape/pull/2176 ) : Fix: OPTIONS fails if matching all routes - [ @myxoh ] ( https://github.com/myxoh ) .
10
10
* [ #2177 ] ( https://github.com/ruby-grape/grape/pull/2177 ) : Fix: ` default ` validator fails if preceded by ` as ` validator - [ @Catsuko ] ( https://github.com/Catsuko ) .
11
+ * [ #2180 ] ( https://github.com/ruby-grape/grape/pull/2180 ) : Call ` super ` in ` API.inherited ` - [ @yogeshjain999 ] ( https://github.com/yogeshjain999 ) .
11
12
* Your contribution here.
12
13
### 1.5.3 (2021/03/07)
13
14
Original file line number Diff line number Diff line change @@ -20,10 +20,11 @@ def new(*args, &block)
20
20
21
21
# When inherited, will create a list of all instances (times the API was mounted)
22
22
# It will listen to the setup required to mount that endpoint, and replicate it on any new instance
23
- def inherited ( api , base_instance_parent = Grape ::API ::Instance )
24
- api . initial_setup ( base_instance_parent )
23
+ def inherited ( api )
24
+ super
25
+
26
+ api . initial_setup ( Grape ::API == self ? Grape ::API ::Instance : @base_instance )
25
27
api . override_all_methods!
26
- make_inheritable ( api )
27
28
end
28
29
29
30
# Initialize the instance variables on the remountable class, and the base_instance
@@ -68,15 +69,6 @@ def call(*args, &block)
68
69
instance_for_rack . call ( *args , &block )
69
70
end
70
71
71
- # Allows an API to itself be inheritable:
72
- def make_inheritable ( api )
73
- # When a child API inherits from a parent API.
74
- def api . inherited ( child_api )
75
- # The instances of the child API inherit from the instances of the parent API
76
- Grape ::API . inherited ( child_api , base_instance )
77
- end
78
- end
79
-
80
72
# Alleviates problems with autoloading by tring to search for the constant
81
73
def const_missing ( *args )
82
74
if base_instance . const_defined? ( *args )
Original file line number Diff line number Diff line change @@ -4081,6 +4081,49 @@ def before
4081
4081
end
4082
4082
end
4083
4083
4084
+ describe '.inherited' do
4085
+ context 'overriding within class' do
4086
+ let ( :root_api ) do
4087
+ Class . new ( Grape ::API ) do
4088
+ @bar = 'Hello, world'
4089
+
4090
+ def self . inherited ( child_api )
4091
+ super
4092
+ child_api . instance_variable_set ( :@foo , @bar . dup )
4093
+ end
4094
+ end
4095
+ end
4096
+
4097
+ let ( :child_api ) { Class . new ( root_api ) }
4098
+
4099
+ it 'allows overriding the hook' do
4100
+ expect ( child_api . instance_variable_get ( :@foo ) ) . to eq ( 'Hello, world' )
4101
+ end
4102
+ end
4103
+
4104
+ context 'overriding via composition' do
4105
+ module Inherited
4106
+ def inherited ( api )
4107
+ super
4108
+ api . instance_variable_set ( :@foo , @bar . dup )
4109
+ end
4110
+ end
4111
+
4112
+ let ( :root_api ) do
4113
+ Class . new ( Grape ::API ) do
4114
+ @bar = 'Hello, world'
4115
+ extend Inherited
4116
+ end
4117
+ end
4118
+
4119
+ let ( :child_api ) { Class . new ( root_api ) }
4120
+
4121
+ it 'allows overriding the hook' do
4122
+ expect ( child_api . instance_variable_get ( :@foo ) ) . to eq ( 'Hello, world' )
4123
+ end
4124
+ end
4125
+ end
4126
+
4084
4127
describe 'const_missing' do
4085
4128
subject ( :grape_api ) { Class . new ( Grape ::API ) }
4086
4129
let ( :mounted ) do
You can’t perform that action at this time.
0 commit comments