0

我有一个application.html.erb我想将未读用户消息的计算移动到一些装饰器/帮助器的地方,基本上是因为它看起来像这样:

<% if current_user %>
  <%= link_to 'Messages', conversations_path %>
  <% counter = @conversations.map do |conversation| %>
    <% unless conversation.unread_message_count(current_user).zero? %>
      <% conversation.unread_message_count(current_user) %>
    <% end %>
  <% end %>
(<%= counter.sum %>)

我知道装饰器的基本概念,但我想知道我是否ConversationDecoratorapp/decorators/conversation_decorator.rb定义的counter方法和@conversations.map 块在那里,如何在里面使用这个装饰器application.html.erb

示例对话装饰器:

class ConversationDecorator < ApplicationDecorator
  def unread_counter
    @conversations.map do |conversation|
      conversation.unread_message_count(current_user) unless conversation.unread_message_count(current_user).zero?
    end.sum
  end
end
4

0 回答 0