How can I have a single helper work on different models passed to it?

Posted by Angela on Stack Overflow See other posts from Stack Overflow or by Angela
Published on 2010-05-01T18:52:58Z Indexed on 2010/05/01 18:57 UTC
Read the original article Hit count: 139

Filed under:
|

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!

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about helpers