如何在 Ruby/Rails 中抽象出与父类类似的验证/关系?

发布于 2024-11-12 05:05:59 字数 2001 浏览 1 评论 0原文

我有这些非常相似的类:

class DeliveryDocument < CommercialDocument
  # Relations
  belongs_to :biller, :class_name => 'Company'
  belongs_to :customer, :class_name => 'Company'
  belongs_to :customer_center, :class_name => 'Center'
  has_many :delivery_document_lines, :dependent => :destroy
  alias_attribute :lines, :delivery_document_lines
  # Some configuration
  accepts_nested_attributes_for :delivery_document_lines
  acts_as_freezable :only_dependencies => true,
    :has_many => [:delivery_document_lines],
    :belongs_to => [:biller, :customer, :customer_center]
  acts_as_clonable :has_many => [:delivery_document_lines]
  validates_each :lines do |record, attr, value|
    # ...
  end
end

并且:

class InvoiceDocument < CommercialDocument
  self.
  # Relations
  belongs_to :biller, :class_name => 'Company'
  belongs_to :customer, :class_name => 'Company'
  belongs_to :customer_center, :class_name => 'Center'
  has_many :invoice_document_lines, :dependent => :destroy
  alias_attribute :lines, :invoice_document_lines
  # Some configuration
  accepts_nested_attributes_for :invoice_document_lines
  acts_as_freezable :only_dependencies => true,
    :has_many => [:invoice_document_lines],
    :belongs_to => [:biller, :customer, :customer_center]
  acts_as_clonable :has_many => [:invoice_document_lines]
  # Validations
  validates_each :lines do |record, attr, value|
    # ...
  end
end

我还有一些没有粘贴的方法,可以将其提取到父级。我只需要知道父类中的类名。当我这样做时:

class CommercialDocument < Document  # document inherits from AR::Base
  # ...
  has_many :"#{self.to_s.underscore}_lines", :dependent => :destroy
  # ...
  accepts_nested_attributes_for :"#{self.to_s.underscore}_lines"
  # ...
end

它不起作用,因为 self.to_sCommercialDocument

您将如何在父类中重构这种行为? 我可以将内容放入模块中并导入它,但是整个文档层次结构几乎变得毫无用处。 我已经有了文档的层次结构,所以如果可以的话,我想在有办法的情况下使用它。

I have these VERY similar classes:

class DeliveryDocument < CommercialDocument
  # Relations
  belongs_to :biller, :class_name => 'Company'
  belongs_to :customer, :class_name => 'Company'
  belongs_to :customer_center, :class_name => 'Center'
  has_many :delivery_document_lines, :dependent => :destroy
  alias_attribute :lines, :delivery_document_lines
  # Some configuration
  accepts_nested_attributes_for :delivery_document_lines
  acts_as_freezable :only_dependencies => true,
    :has_many => [:delivery_document_lines],
    :belongs_to => [:biller, :customer, :customer_center]
  acts_as_clonable :has_many => [:delivery_document_lines]
  validates_each :lines do |record, attr, value|
    # ...
  end
end

And:

class InvoiceDocument < CommercialDocument
  self.
  # Relations
  belongs_to :biller, :class_name => 'Company'
  belongs_to :customer, :class_name => 'Company'
  belongs_to :customer_center, :class_name => 'Center'
  has_many :invoice_document_lines, :dependent => :destroy
  alias_attribute :lines, :invoice_document_lines
  # Some configuration
  accepts_nested_attributes_for :invoice_document_lines
  acts_as_freezable :only_dependencies => true,
    :has_many => [:invoice_document_lines],
    :belongs_to => [:biller, :customer, :customer_center]
  acts_as_clonable :has_many => [:invoice_document_lines]
  # Validations
  validates_each :lines do |record, attr, value|
    # ...
  end
end

I also have some methods I didn't paste that could be extracted to the parent. I only need to know the class name in the parent. When I do this:

class CommercialDocument < Document  # document inherits from AR::Base
  # ...
  has_many :"#{self.to_s.underscore}_lines", :dependent => :destroy
  # ...
  accepts_nested_attributes_for :"#{self.to_s.underscore}_lines"
  # ...
end

it doesn't work, because self.to_s is CommercialDocument.

How would you refactor this behavior in parent class?
I could put things in a module and import it, but then the whole hierarchy of documents becomes almost useless.
I already have the documents' hierarchy, so if I can, I want to use it if there is a way.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半衾梦 2024-11-19 05:05:59

您可以尝试使用 Class.inherited

You could try using Class.inherited

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文