Skip to content

Commit fefb809

Browse files
committed
Allow entity_name to be called on parent classes
This resolves issue #659.
1 parent 17fe8a6 commit fefb809

File tree

5 files changed

+40
-1
lines changed

5 files changed

+40
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#### Features
44

5+
* [#794](https://github.com/ruby-grape/grape-swagger/pull/794): Allow entity_name to be inherited, fixes #659 - [@urkle](https://githubcom/urkle).
56
* Your contribution here.
67

78
#### Fixes

UPGRADING.md

+24
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,29 @@
11
## Upgrading Grape-swagger
22

3+
### Upgrading to >= 1.1.1
4+
5+
The entity_name class method is now called on parent classes for inherited entities. Now you can do this
6+
7+
```ruby
8+
module Some::Long::Module
9+
class Base < Grape::Entity
10+
# ... other shared logic
11+
def self.entity_name
12+
"V2::#{self.to_s.demodulize}"
13+
end
14+
end
15+
16+
def MyEntity < Base
17+
# ....
18+
end
19+
20+
def OtherEntity < Base
21+
# revert back to the default behavior by hiding the method
22+
private_class_method :entity_name
23+
end
24+
end
25+
```
26+
327
### Upgrading to >= 1.1.0
428

529
Full class name is used for referencing entity by default (e.g. `A::B::C` instead of just `C`). `Entity` and `Entities` suffixes and prefixes are omitted (e.g. if entity name is `Entities::SomeScope::MyFavourite::Entity` only `SomeScope::MyFavourite` will be used).

lib/grape-swagger/doc_methods/data_type.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def parse_multi_type(raw_data_type)
4848
end
4949

5050
def parse_entity_name(model)
51-
if model.methods(false).include?(:entity_name)
51+
if model.respond_to?(:entity_name)
5252
model.entity_name
5353
elsif model.to_s.end_with?('::Entity', '::Entities')
5454
model.to_s.split('::')[0..-2].join('::')

spec/issues/430_entity_definitions_spec.rb

+2
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ def self.entity_name
5555
class Class7
5656
class SeventhEntity < Class6::SixthEntity
5757
expose :seventh_thing
58+
59+
private_class_method :entity_name
5860
end
5961
end
6062
end

spec/lib/data_type_spec.rb

+12
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,18 @@
4949
it { is_expected.to eq 'MyInteger' }
5050
end
5151

52+
describe 'Types in array with inherited entity_name' do
53+
before do
54+
stub_const 'EntityBase', Class.new
55+
allow(EntityBase).to receive(:entity_name).and_return 'MyInteger'
56+
stub_const 'MyEntity', Class.new(EntityBase)
57+
end
58+
59+
let(:value) { { type: '[MyEntity]' } }
60+
61+
it { is_expected.to eq 'MyInteger' }
62+
end
63+
5264
describe 'Rack::Multipart::UploadedFile' do
5365
let(:value) { { type: Rack::Multipart::UploadedFile } }
5466

0 commit comments

Comments
 (0)