I have three models, Calls, Emails, and Letters and those are basically templates of what gets sent to individuals, modeled as Contacts.
When a Call is made, a row in model in ContactCalls gets created. If an Email is sent, an entry in ContactEmails is made.
Each has its own controller: contact_calls_controller.rb and contact_emails_controller.rb. I would like to create a single table inheritance called ContactEvents which has types Calls, Emails, and Letters. But I'm not clear how I pass the type information or how to consolidate the controllers.
Here's the two controllers I have, as you can see, there's alot of duplication, but some
differences that needs to be preserved. In the case of letter and postcards (another Model), it's even more so.
class ContactEmailsController < ApplicationController
def new
@contact_email = ContactEmail.new
@contact_email.contact_id = params[:contact]
@contact_email.email_id = params[:email]
@contact = Contact.find(params[:contact])
@company = Company.find(@contact.company_id)
contacts = @company.contacts.collect(&:full_name)
contacts.each do |contact|
@colleagues = contacts.reject{ |c|
[email protected]_name }
end
@email = Email.find(@contact_email.email_id)
@contact_email.subject = @email.subject
@contact_email.body = @email.message
@email.message.gsub!("{FirstName}", @contact.first_name)
@email.message.gsub!("{Company}", @contact.company_name)
@email.message.gsub!("{Colleagues}", @colleagues.to_sentence)
@email.message.gsub!("{NextWeek}", (Date.today + 7.days).strftime("%A, %B %d"))
@contact_email.status = "sent"
end
def create
@contact_email = ContactEmail.new(params[:contact_email])
@contact = Contact.find_by_id(@contact_email.contact_id)
@email = Email.find_by_id(@contact_email.email_id)
if @contact_email.save
flash[:notice] = "Successfully created contact email."
# send email using class in outbound_mailer.rb
OutboundMailer.deliver_campaign_email(@contact,@contact_email)
redirect_to todo_url
else
render :action => 'new'
end
end
AND:
class ContactCallsController < ApplicationController
def new
@contact_call = ContactCall.new
@contact_call.contact_id = params[:contact]
@contact_call.call_id = params[:call]
@contact_call.status = params[:status]
@contact = Contact.find(params[:contact])
@company = Company.find(@contact.company_id)
@contact = Contact.find(@contact_call.contact_id)
@call = Call.find(@contact_call.call_id)
@contact_call.title = @call.title
contacts = @company.contacts.collect(&:full_name)
contacts.each do |contact|
@colleagues = contacts.reject{ |c|
[email protected]_name }
end
@contact_call.script = @call.script
@call.script.gsub!("{FirstName}", @contact.first_name)
@call.script.gsub!("{Company}", @contact.company_name )
@call.script.gsub!("{Colleagues}", @colleagues.to_sentence)
end
def create
@contact_call = ContactCall.new(params[:contact_call])
if @contact_call.save
flash[:notice] = "Successfully created contact call."
redirect_to contact_path(@contact_call.contact_id)
else
render :action => 'new'
end
end