2010-09-17 91 views
0

我正在一个社交网站(基本上是facebook的副本,说实话...),我重用了大部分的insoshi。但是,对于我的喜好来说,精神的兴奋是不够准确的。因为它不支持更专门的信息。你会明白我的意思在以下 代码:rails检查现有的实例属性

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 

我有不同类型的项目。通常物品有“用户”和“联系人”。除了帖子,他们有一个“用户”和一个“所有者”。因为另一篇文章可以将它写在某人的墙上(为此所有者)。

现在,只要我尝试将联系人设置为item.contact,就会出现问题。它只是让'MyMethod'错误指出item.contact不存在。 (如果该项目是帖子而不是“连接”或可比较的话,这是显而易见的)。

所以我在征求您的意见: 1)解决这个问题的一些更多的红宝石,或者2)改变后模型,使一个职位有'用户'和'联系'?

谢谢你们 斯特凡诺

+0

Uargh。请使用符号而不是整数。 – Reactormonk 2010-09-17 11:51:31

+1

我讨厌成为一个家伙,但是这段代码真的很难遵循,看起来很诡异。我绝对不会通过向此添加更多代码来解决问题。我建议阅读单表继承,多态性,并考虑将'关系'移动到模型。 – 2010-09-17 12:14:05

+0

纯粹是为了定制助手的输出而创建一个包含表格的新模型? @Tass:符号不会让它更混乱吗? :user_same_as_contact,:current_user_same_as_user我不知道如果我明白你的意思。 – KimJongIl 2010-09-17 12:31:53

回答

0

根据你的逻辑,关系3和4永远不会返回。我想你在哪里有current_user != contact or current_user != user,你的意思是有and。就个人而言,我总是使用& &,因为如果第一个条件为假,它会短路。然而,在我的重构中,你不需要它,因为如果没有其他情况匹配,它将返回5。

我将关系逻辑移至Item模型,并在帮助器中进行适当的更新。

视图助手:

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 

项目类

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 
+0

加了一个推荐的重构谢谢,非常感谢。但似乎整数键是要走的路(?) – KimJongIl 2010-09-28 15:36:56

+1

您可以通过使用常量或符号轻松地改进它。 '关系::朋友'或':朋友' – glebm 2013-01-20 01:22:12

0

我会用Ruby代码修复。

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

使用的respond_to?这将适用于任何有联系人的班级。