Home > Software engineering >  How to use Draper decorator in multiple models?
How to use Draper decorator in multiple models?

Time:08-21

I'm using Draper gem for decorators. I have two similar models, that doing the same, but using different databases. In my namespaces I have prefix Clone::. So, for instance: Car and Clone::Car. I already have a decorator CarDecorator for Car model. All I want is to use this decorator for Clone::Car as well, but when I'm trying to apply such code by using inheritance:

class Clone::CarDecorator < CarDecorator
end

I have an error in my specs:

Circular dependency detected while autoloading constant Clone::Car.

What is the best practice to implement single decorator per multiple models via Drapper gem?

CodePudding user response:

There were no answers, so I will post my solution. It's possible to do this without inheritance and adding additional decorator by adding method #decorator_class into "child" class. So, according to the example above, it will looks like:

class CloneCar < ApplicationRecord
  #...

  def self.decorator_class
    CarDecorator
  end

  #...
end

So, by such means, CarDecorator will be using in the Car model and CloneCar model as well.

  • Related