Ruby on rails 在开发过程中,我应该如何处理Rails引擎外部所需的模型?
我正在将Rails应用程序的各个部分提取到引擎中。引擎包含与安装引擎的应用程序中的模型类具有关系的模型类。在某些情况下,这些关系是必需的Ruby on rails 在开发过程中,我应该如何处理Rails引擎外部所需的模型?,ruby-on-rails,ruby,rails-engines,Ruby On Rails,Ruby,Rails Engines,我正在将Rails应用程序的各个部分提取到引擎中。引擎包含与安装引擎的应用程序中的模型类具有关系的模型类。在某些情况下,这些关系是必需的 module Carrier class Profile < ActiveRecord::Base attr_accessible :company_id belongs_to :company, class_name: Carrier.company_class_name validates :company, presen
module Carrier
class Profile < ActiveRecord::Base
attr_accessible :company_id
belongs_to :company, class_name: Carrier.company_class_name
validates :company, presence: true
end
end
模块载体
类配置文件
由于引擎没有
Company
类,在开发过程中应该如何处理这种关系?其他人如何“存根”外部类?在引擎中为所需类创建一个模型(示例中是公司)
如果在安装引擎时未设置
公司类名称
,则可能需要引发异常。对于主应用程序中的任何迁移和模型,我将在虚拟应用程序中复制迁移,并在虚拟应用程序中为其定义空模型
module Carrier
class Company < ActiveRecord::Base
end
end
class CreateCarrierCompanies < ActiveRecord::Migration
def change
if Rails.application.class.parent_name == "Dummy"
create_table :carrier_companies do |t|
t.timestamps
end
end
end
end
module Carrier
mattr_accessor :company_class_name
def self.company_class_name
@@company_class_name || "Carrier::Company"
end
def self.company_class
company_class_name.constantize
end
end