如何优化我的代码

时间:2012-12-04 11:23:52

标签: ruby-on-rails ruby-on-rails-3

我想优化我的代码我怎么样?

如果我添加了新角色,那么另外一个条件会添加,但我不想要。我想要任何不。角色,但我只需要一个条件是可能的吗?

after_save :announcement_send

def announcement_send
  if self.send_now == true && self.group_id.to_s == "Artists"
    User.having_role("artist").each do |user|
      ArtistMailer.announcement_user(self, user).deliver
    end
  elsif self.send_now == true && self.group_id.to_s == "Fans"
    User.having_role("fan").each do |user|
      ArtistMailer.announcement_user(self, user).deliver
    end
  elsif self.send_now == true && self.group_id.to_s == "Both"
     User.not_having_role("admin").each do |user|
       ArtistMailer.announcement_user(self, user).deliver
     end
  end        
end

4 个答案:

答案 0 :(得分:3)

没有真正测试过这个,但我会这样打破这个:

def role(s)                                                                                                                                                                               
  if s == "Both"                                                                                                                                                                          
    User.not_having_role("admin")                                                                                                                                                         
  else                                                                                                                                                                                    
    User.having_role(s.singularize.downcase)                                                                                                                                              
  end                                                                                                                                                                                     
end         

if self.send_now                                                                                                                                                                          
  role(self.group_id.to_s).each do |user|                                                                                                                                                 
    ArtistMailer.announcement_user(self, user).deliver                                                                                                                                    
  end                                                                                                                                                                                     
end 

此代码假定group_id始终作为用户所处角色的复数形式出现。因此,艺术家意味着“艺术家”角色。

当然,这假设所有角色都是有效的group_id。如果不是这种情况,您可以针对def role内可能值的白名单检查group_id:

white_list = ['artist', 'fan']
role = s.singularize.downcase
if white_list.include? role
  User.having_role(role)
else
  # possibly throw an exception
end

答案 1 :(得分:2)

好的代码应该讲故事。

WHITELIST = %w(artist fan)

def announcement_send
  return unless send_now
  users_list_for_announcement.each { |user| notify(user) }
end

def users_list_for_announcement
  in_whitelist? = ->(group) { WHITELIST.include?(group) }
  case formatted_group_name
  when "both"        then User.not_having_role("admin")
  when in_whitelist? then User.having_role(formatted_group_name)
  else []
  end
end

def formatted_group_name
  group_id.to_s.singularize.downcase
end

def notify(user)
  ArtistMailer.announcement_user(self, user).deliver
end

实际上,我甚至会在用户处理逻辑中创建一个专用范围,以便根据组获得适当的用户。

答案 2 :(得分:0)

我认为以下是您要求的答案:


if self.send_now
if self.group_id.to_s == "Both" 
    User.not_having_role("admin").each do |user|
        ArtistMailer.announcement_user(self, user).deliver
    end
elsif self.group_id.to_s
    User.having_role(self.group_id.to_s.singularize.downcase).each do |user|
        ArtistMailer.announcement_user(self, user).deliver
    end     
end
end

答案 3 :(得分:0)

将角色作为参数传递

def announcement_send(role)
  if self.send_now == true 
    User.having_role(role).each do |user|
      ArtistMailer.announcement_user(self, user).deliver
    end  
end

def after_save(role)
   announcement_send(role)
end

这将在两个第一个条件

中起作用

在最后我猜测用户既有“粉丝”又有“艺术家”角色。然后传递user.role.first