Blankman
Blankman

Reputation: 267020

Help refactoring this controller logic, should this be a helper?

So if I am listing blog post entries, and under each entry I display 5 comments. There is a label below the last comment that says:

'show more'

or

'add comment'

The logic for this currently is in my controller:

@posts.each do |p|

  if p.comment_count > 3
    p.some_label = 'show more'
  else
    p.some_label = 'add comment'
  end

end

I had to add a 'some_label' attribute to my posts model just for this purpose.

Does this seem right or should it be re-factored?

Upvotes: 1

Views: 63

Answers (1)

Pete
Pete

Reputation: 18075

This feels like view logic to me and it seems a little funny to have it use an attribute on the model for something like which link to display to the user.

I would suggest going more towards the route you mentioned by making use of a helper method. When you get to the point of rendering the link below the post, just make a call to the helper which can do a quick check on the comments count for that post and could simply return the string (or full link) you are looking for in that situation.

Aside from determining how many comments the post has, this logic shouldn't need to interact directly with the model at all.

Upvotes: 1

Related Questions