我怎样才能让一个助手在传递给它的不同模型上工作?

发布于 2024-08-31 11:13:33 字数 1143 浏览 5 评论 0原文

我可能需要分两步进行重构,因为我仍在开发项目并学习用例,因为这是为了解决我自己的问题。我有三种模型:信件、电话、电子邮件。它们有一些相似之处,但我预计它们也会有一些不同的属性,正如您从它们的描述中可以看出的那样。

理想情况下,我可以将它们重构为事件,类型为信件、电话、电子邮件,但不知道如何扩展子类。

我的迫切需要是这样的:我有一个助手,可以检查电子邮件(例如)是否发送给特定联系人的状态:

def show_email_status(contact, email)

  @contact_email = ContactEmail.find(:first,
     :conditions => {:contact_id => contact.id, :email_id => email.id })
  if ! @contact_email.nil?
    return @contact_email.status
  end
end

我意识到我当然想知道是否拨打了电话的状态也是一个联系人,所以我写道:

def show_call_status(contact, call)

  @contact_call = ContactCall.find(:first, 
     :conditions => {:contact_id => contact.id, :call_id => call.id })
  if ! @contact_call.nil?
    return @contact_call.status
  end
end

我希望能够有一个助手 show_status ,我可以在其中说 show_status(contact,call) 或 show_status(contact,email) 并且它会知道是否寻找对象 @ contact_call 或 @contact_email。

是的,如果只是 @contact_event 会更容易,但我想在程序启动和运行时进行一些小的重构,这将使为给定联系人创建历史记录的能力变得更加容易。

谢谢!

注意:目前我将状态作为 contact_email、contact_call 等的属性。Contact_email 仅在发送电子邮件时创建,因此如果尚未发送电子邮件,则不会有 contact_email,并且我需要知道状态是“未发送”...

I am probably going to need to refactor in two steps since I'm still developing the project and learning the use-cases as I go along since it is to scratch my own itch. I have three models: Letters, Calls, Emails. They have some similarilty, but I anticipate they also will have some different attributes as you can tell from their description.

Ideally I could refactor them as Events, with a type as Letters, Calls, Emails, but didn't know how to extend subclasses.

My immediate need is this: I have a helper which checks on the status of whether an email (for example) was sent to a specific contact:

def show_email_status(contact, email)

  @contact_email = ContactEmail.find(:first,
     :conditions => {:contact_id => contact.id, :email_id => email.id })
  if ! @contact_email.nil?
    return @contact_email.status
  end
end

I realized that I, of course, want to know the status for whether a call was made to a contact as well, so I wrote:

def show_call_status(contact, call)

  @contact_call = ContactCall.find(:first, 
     :conditions => {:contact_id => contact.id, :call_id => call.id })
  if ! @contact_call.nil?
    return @contact_call.status
  end
end

I would love to be able to just have a single helper show_status where I can say show_status(contact,call) or show_status(contact,email) and it would know whether to look for the object @contact_call or @contact_email.

Yes, it would be easier if it were just @contact_event, but I want to do a small refactoring while I get the program up and running, and this would make the ability to do a history for a given contact much easier.

Thanks!

NOTE: Currently I have status as an attribute of contact_email, contact_call, etc. Contact_email only gets created when the email is sent, so there is not contact_email if an email hasn't been sent, and I need to know that the status is "unsent"...

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

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

发布评论

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

评论(3

淡淡離愁欲言轉身 2024-09-07 11:13:34

假设从示例中,您的关联如下所示:

class Contact
    has_many :emails, :through => :contact_emails
    has_many :calls, :through => :contact_calls
end

状态是 ContactEmail / ContactCall 上的一个属性,但您需要电子邮件 / Call 对象的状态,然后(基于此处的 Kandada 答案):

class Contact 
  def event_status(event)
    event_type = event.class.name
    foreign_key = ("%s_id" % event_type.downcase).to_sym

    assoc = "Contact#{event_type}".tableize
    contact_event = send(:assoc).first(:conditions => {foreign_key => event.id})
    contact_event.try(:status) 
  end
end

Assuming from the example, that your associations look like this:

class Contact
    has_many :emails, :through => :contact_emails
    has_many :calls, :through => :contact_calls
end

and status is an attribute on ContactEmail / ContactCall, but you need a status for Email / Call object, then (based on Kandada answer here):

class Contact 
  def event_status(event)
    event_type = event.class.name
    foreign_key = ("%s_id" % event_type.downcase).to_sym

    assoc = "Contact#{event_type}".tableize
    contact_event = send(:assoc).first(:conditions => {foreign_key => event.id})
    contact_event.try(:status) 
  end
end
宫墨修音 2024-09-07 11:13:34

您可以将助手移至 Contact 模型。

class Contact < ActiveRecord::Base

  has_many :contact_emails
  has_many :contact_calls
  has_many :contact_letters

  def event_status event
    assoc_name = "Contact#{event.class.name}".pluralize.underscore
    foreign_key = "%s_id" % event.class.name.underscore
    ce = send(assoc_name).first(:conditions => {foreign_key => event.id})
    ce ? ce.status : nil
  end

end

现在您可以获得如下状态:

contact.event_status(email1)
contact.event_status(letter2)
contact.event_status(call12)

这减轻了对不同助手的需求。解决不同事件的一种方法。

You can move the helper to the Contact model.

class Contact < ActiveRecord::Base

  has_many :contact_emails
  has_many :contact_calls
  has_many :contact_letters

  def event_status event
    assoc_name = "Contact#{event.class.name}".pluralize.underscore
    foreign_key = "%s_id" % event.class.name.underscore
    ce = send(assoc_name).first(:conditions => {foreign_key => event.id})
    ce ? ce.status : nil
  end

end

Now you can get the status as follows:

contact.event_status(email1)
contact.event_status(letter2)
contact.event_status(call12)

This alleviates the need for different helpers. One method to address different events.

稳稳的幸福 2024-09-07 11:13:34

由于您已经有了 show_call_status 和 show_email_status,因此您可以编写第三个 show_letter_status。

然后使用这个:

def show_status(contact, call_or_email_or_letter)
  model_name = call_or_email_or_letter.class.name.tableize.singularize
  send "show_#{model_name}_status", contact, call_or_email_or_letter
end

Since you already have show_call_status and show_email_status, you can write a third show_letter_status.

Then use this:

def show_status(contact, call_or_email_or_letter)
  model_name = call_or_email_or_letter.class.name.tableize.singularize
  send "show_#{model_name}_status", contact, call_or_email_or_letter
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文