module DashboardHelper
def activity_message_for(object)
case object.class.name
when 'Job'
job_image_tag = image_tag("snippet.png", :class => "icon")
if object.created_at == object.updated_at
"<td>#{job_image_tag} The job #{link_to object.title, job_path(object)}
was added.</td><td> #{object.updated_at.to_formatted_s(:short)}</td>"
elsif object.updated_at > object.created_at
"<td>#{job_image_tag} The job #{link_to object.title, job_path(object)}
was updated.</td><td> #{object.updated_at.to_formatted_s(:short)}</td>"
end
when 'Candidate'
candidate_image_tag = image_tag("candidate.png", :class => "icon")
if object.created_at == object.updated_at
"<td>#{candidate_image_tag} The Candidate #{link_to object.name, job_candidate_path(object.job_id, object)}
was added to #{link_to object.job.title,job_path(object.job)}.</td><td> #{object.created_at.to_formatted_s(:short)}</td>"
elsif object.updated_at > object.created_at
"<td>#{candidate_image_tag} The Candidate #{link_to object.name, job_candidate_path(object.job_id, object)}
that belongs to job #{link_to object.job.title,job_path(object.job)} was updated.</td><td> #{object.updated_at.to_formatted_s(:short)}</td>"
end
when 'Comment'
comment_image_tag = image_tag("comment.png", :class => "icon")
if object.created_at == object.updated_at
"<td>#{comment_image_tag} A comment was added to candidate
#{link_to object.candidate.name, job_candidate_path(object.candidate.job_id, object.candidate_id)} by
#{get_pretty_user_from_object(object)} </td><td> #{object.created_at.to_formatted_s(:short)}</td>"
end
end
end
end
Refactorings
No refactoring yet !
Jason Dew
July 21, 2008, July 21, 2008 22:59, permalink
I would consider moving the cases into their respective helpers and calling them that way:
So you would define a method in JobHelper, CandidateHelper, and CommentHelper called activity_message. Hope this helps,
module DashboardHelper
def activity_message_for object
"#{object.class.name.classify}Helper".constantize.send(:activity_message, object)
end
end
danielharan
July 21, 2008, July 21, 2008 23:01, permalink
HTML inside helpers is ugly, get that into partials and it gets way easier to deal with:
module DashboardHelper
def activity_message_for(object)
render :partial => "#{object.class.name.downcase}/_#{object.class.name.downcase}_activity"
end
end
danielharan
July 22, 2008, July 22, 2008 12:54, permalink
Argh - hopefully now I'm awake:
module DashboardHelper
def activity_message_for(object)
render :partial => "#{object.class.name.downcase.pluralize}/activity"
end
end
Lindajbabyyeah
January 22, 2012, January 22, 2012 14:10, permalink
Howdy there there i am Linda Wiggins,I taught at Cambridge Uni and i have labored as a wellness and wellbeing advisor on loads of tv unveils in the two similarly the United kingdom and the USA.I am also an superior yoga instructor
Lindabsyogagirl
January 11, 2012, January 11, 2012 08:01, permalink
Hi i am Linda B,i laboured at Liverpool college and i have been employed as a health and fitness authority on diverse popular radio shows in both the UK and the US.I'm also an advanced dance instructor.
This works but it's kind of ugly... Need help to cut down the lines in a good and smart way. Suggestions?