Method: Grape::DSL::InsideRoute#entity_class_for_obj
- Defined in:
- lib/grape/dsl/inside_route.rb
#entity_class_for_obj(object, options) ⇒ Class
Attempt to locate the Entity class for a given object, if not given explicitly. This is done by looking for the presence of Klass::Entity, where Klass is the class of the object parameter, or one of its ancestors.
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 |
# File 'lib/grape/dsl/inside_route.rb', line 355 def entity_class_for_obj(object, ) entity_class = .delete(:with) if entity_class.nil? # entity class not explicitely defined, auto-detect from relation#klass or first object in the collection object_class = if object.respond_to?(:klass) object.klass else object.respond_to?(:first) ? object.first.class : object.class end object_class.ancestors.each do |potential| entity_class ||= (namespace_stackable_with_hash(:representations) || {})[potential] end entity_class ||= object_class.const_get(:Entity) if object_class.const_defined?(:Entity) && object_class.const_get(:Entity).respond_to?(:represent) end entity_class end |