0

我想优化我的代码我该怎么做?

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

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

4 回答 4

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 始终以用户所在角色的复数形式出现。因此 Artists 表示“艺术家”角色。

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

white_list = ['artist', 'fan']
role = s.singularize.downcase
if white_list.include? role
  User.having_role(role)
else
  # possibly throw an exception
end
于 2012-12-04T11:30:12.897 回答
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

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

于 2012-12-04T11:33:25.633 回答
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
于 2012-12-04T11:37:19.463 回答
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

于 2012-12-04T11:41:34.117 回答