Rails 6 how to decorate application.html.erb

152 Views Asked by At

I've got an application.html.erb where I want move calculation of unread user messages into some decorator/helper, basically because it looks like this:

<% 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 %>)

I know the basic concept of decorators but I'm wondering if I have ConversationDecorator in app/decorators/conversation_decorator.rb with defined counter method with @conversations.map block there, how to use this decorator inside of application.html.erb ?

Example ConversationDecorator:

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
0

There are 0 best solutions below