我正在学习为我的代码编写规范,但我还是新手。尝试为我的模型问题编写规范/共享示例,但我不明白如何编写它,因为我发现这非常复杂。
如果有人可以帮助我或向我展示如何为这些代码编写规范,那将非常有帮助。下面是我关心的 AddStakeholder。
module AddStakeholder
extend ActiveSupport::Concern
included do
before_validation -> { add_stakeholders }
end
private
def add_stakeholders
return unless self.stakeholder
company = if self.is_a?(Certificate)
self.round ? self.round.company : self.company
else
self.company
end
existing_stakeholder = company.stakeholders.find_by_email(self.stakeholder.email)
if existing_stakeholder.present?
if self.stakeholder.name == existing_stakeholder.name
self.stakeholder = existing_stakeholder
else
self.stakeholder.company = company
end
else
self.stakeholder.company = company
end
end
end