mardi 16 juin 2015

model with namespace inheritance

I'd like to extract custom gem with shared models. This models will be shared between several applications and extended if needed. For example i have shared model like:

module CommonModels
  class Customer < ActiveRecord::Base
    has_many :orders
  end
end

module CommonModels
  class Order < ActiveRecord::Base
    has_one :customer
  end
end

And in app models extends common:

class Customer < CommonModels::Customer
  validates :name, presence: true
end

class Order < CommonModels::Order
  validates :price, presence: true
end

Inheritance works well but when i try to get customer order or build new object from relation instead of Order i will get CommonModel::Order. Is there a way to return extended Order class instead of parent (like some kind of polimorphism)?

I do not like to specify relations in each dependent model but i want to use all features from specified version without 'casting' to child object.

Aucun commentaire:

Enregistrer un commentaire