views:

33

answers:

2

Hi guys

I'm working on a social networking site (basically a copy of facebook to be honest...) and I reused big parts of insoshi. But the feed of insoshi is not accurate enough for my likings. As in it doesnt support more specialized messages. You will see what I mean in the following code:

item = activity.item
relationship = relationship(item)
case relationship
   when 1
     raw %(<p>You wrote on your own wall: <br/>
     #{truncate(item.body, :length => 20)}</p>)
   when 2
     raw %(<p>#{link_to item.user.name, item.user} wrote on your wall</p>)
   when 3
     raw %(<p>#{link_to item.user.name, item.user} wrote on his wall</p>)
   when 4
     raw %(<p>You wrote on #{link_to item.user.name, item.user}'s wall</p>)
   when 5
     raw %(<p>#{link_to item.user.name, item.user} wrote on 
              #{link_to item.contact.name, item.contact}'s wall</p>)
end

    def relationship(item) 
        unless item.owner.nil?
          contact = item.owner #so that it works for posts as well
        else
          contact = item.contact
        end
        user = item.user

        if current_user != contact or current_user != user
          return 5
        else
          if current_user == contact
            if current_user == user
              return 1
            else
              return 2
            end
          else
            if contact == user
              return 3
            else
              return 4
            end
          end
        end
end

I have different types of items. Normally items have a "user" and a "contact". Except posts, they have a "user" and an "owner". Because the other of a post can write it on somebody's wall (therefor the owner).

Now the problem arises as soon as I try to set the contact to the item.contact... it just keeps bugging me with 'NoMethod' error saying that item.contact does not exist. (which is obvious if the item is a post and not a 'connection' or comparable).

So I am asking for your opinion: 1) Fix the problem with some more ruby, or 2) change the post model so that a post has a 'user' and a 'contact'?

Thanks guys Stefano

A: 

I would fix with Ruby code.

contact = item.contact if item.respond_to? :contact

By using respond_to? this will work for any class that has a contact.

Bill Leeper
A: 

According to your logic, relationships 3 and 4 would never get returned. I think where you have current_user != contact or current_user != user, you meant to have and. Personally, I always use && because it short circuits if the first condition is false. However, in my refactor, you don't need it because it returns 5 if no other cases match.

I moved the relationship logic to the Item model and made appropriate updates in the helper.

View Helper:

case item.relationship_to_user(current_user)
when 1
  raw %(<p>You wrote on your own wall: <br/>
  #{truncate(item.body, :length => 20)}</p>)
when 2
  raw %(<p>#{link_to item.user.name, item.user} wrote on your wall</p>)
when 3
  raw %(<p>#{link_to item.user.name, item.user} wrote on his wall</p>)
when 4
  raw %(<p>You wrote on #{link_to item.user.name, item.user}'s wall</p>)
when 5
  raw %(<p>#{link_to item.user.name, item.user} wrote on 
  #{link_to item.contact.name, item.contact}'s wall</p>)
end

Item Class

class Item < ActiveRecord::Base

  def relationship_to_user(current_user)
    contact = owner || contact  

    return 1 if current_user == contact && current_user == user
    return 2 if current_user == contact
    return 3 if current_user != contact
    return 4 if current_user != contact && contact != user

    return 5
    # return 5 if current_user != contact or current_user != user
  end

end
Beerlington
thanks, greatly appreciated. but it seems that the integer keys are the way to go(?)
Stefano