Skip to content

.find on a namespaced decorator class fails if there is not a "base" inferrable decorator class #959

@julioag-rmd

Description

@julioag-rmd

Env

  • Affected versions: 4.0.5, 4.0.6
  • Unaffected version: 4.0.4
  • Rails Version: 8.0.3
  • Ruby Version: 3.4.8

Context and Issue

I use decorators under namespaces to provide logic depending on the application context. Eg: Admin:: namespaced decorators are to use inside our admin namespace, API::V1:: namespaced decorators are for use in API v1, etc.

There may be decorators on the root app/decorators folder, there may be not.

NOTE I am not using inheritance between decorators, as in, all inherit from Draper::Decorator.

Eg:

Admin::EncounterDecorator < Draper::Decorator and EncounterDecorator < Draper::Decorator.

So, given this setup on a Rails app

app/models/encounter.rb
app/models/consult.rb
app/decorators/encounter_decorator.rb
app/decorators/admin/encounter_decorator.rb
app/decorators/admin/consult_decorator.rb

👀 Notice how there is no app/decorators/consult.rb

In a Rails console:

> EncounterDecorator.find(2134)
  Encounter Load (0.8ms)  SQL blabla
=> #<EncounterDecorator:0x0000000135d32300
 @context={},
 @object=
  #<Encounter:0x000000013589ac98
   id: 1127442,

and

> Admin::EncounterDecorator.find(2134)
  Encounter Load (0.8ms)  SQL blabla
=> #<Admin::EncounterDecorator:0x0000000135d32300
 @context={},
 @object=
  #<Encounter:0x000000013589ac98
   id: 1127442,

All good so far, but:

> Admin::ConsultDecorator.find(4321)
  Consult Load (0.8ms)  SQL bla bla
Draper::UninferrableDecoratorError: Could not infer a decorator for Consult. (Draper::UninferrableDecoratorError)

          raise Draper::UninferrableDecoratorError.new(called_on)
          ^^^^^
from /Users/julio/.local/share/mise/installs/ruby/3.4.8/lib/ruby/gems/3.4.0/gems/draper-4.0.6/lib/draper/decoratable.rb:87:in 'Draper::Decoratable::ClassMethods#decorator_class'

Expected Behavior

If the class is a decorator itself, there's no attempt at inferring a defaut decorator, and the class itself is the decorator class?

Question

Or, is it intended/expected/enforced that there is an app/decorators/consult_decorator.rb and namespaced decorators inherit from it?

Metadata

Metadata

Type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions