jeudi 20 juillet 2017

Keep namespacing of a controller when including a model in Rails

I am trying to keep the namespace of a class when including a module. Lets say I have these Models:

class Shop < ApplicationRecord
  self.abstract_class = true
end
class A::Shop < ::Shop
end
class B::Shop < ::Shop
end

And this controller:

module A
  class ShopController < AuthenticatedController
    include Basic::Features
    def test
      p Shop.new #YES! its a A::Shop 
    end
  end
end

And this Module:

module Basic
  module Features
    def test
      p Shop.new  #Shop (abstract)
    end
  end
end

In the above example, the namespace is overwritten when including the module. As I want to use the Basic::Features module at multiple places in my codebase, I would like to automatically switch between A::Shop and B::Shop when including it in the controller.

Anybody any idea if this is possible, and how.

Aucun commentaire:

Enregistrer un commentaire