C8c549e9bb39135192db53d238e19f3e

This works but it's kind of ugly... Need help to cut down the lines in a good and smart way. Suggestions?

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 !

D16d53391068ff0830269149b060789d

Jason Dew

July 21, 2008, July 21, 2008 22:59, permalink

3 ratings. Login to rate!

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
880cbab435f00197613c9cc2065b4f5a

danielharan

July 21, 2008, July 21, 2008 23:01, permalink

No rating. Login to rate!

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
880cbab435f00197613c9cc2065b4f5a

danielharan

July 22, 2008, July 22, 2008 12:54, permalink

No rating. Login to rate!

Argh - hopefully now I'm awake:

module DashboardHelper
  def activity_message_for(object)
    render :partial => "#{object.class.name.downcase.pluralize}/activity"
  end
end
C8c549e9bb39135192db53d238e19f3e

Kryckan

July 22, 2008, July 22, 2008 18:38, permalink

No rating. Login to rate!

Thanks! Will try it out now!

9c9ec74ddb210192cf3cf73729b4454f

Lindajbabyyeah

January 22, 2012, January 22, 2012 14:10, permalink

No rating. Login to rate!

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

32a17d1829852ec9343566e448edf071

Lindabsyogagirl

January 11, 2012, January 11, 2012 08:01, permalink

No rating. Login to rate!

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.

Your refactoring





Format Copy from initial code

or Cancel