Move related list partial to a component

This way the code is easier to follow; the code checking whether the
list has contents is in the partial rendering the list.

We also remove some duplication setting up related content in the
controllers.

For some reason, we have to manually ignore i18n keys which were
automatically ignored when the code was in the view.
This commit is contained in:
Javi Martín
2021-08-03 22:32:15 +02:00
parent 89436b528f
commit c8d8fae98d
12 changed files with 62 additions and 51 deletions

View File

@@ -0,0 +1,21 @@
<ul class="related-content-list" id="related-content-list">
<% related_contents.compact.each do |related| %>
<% related_content = related.find_related_content(relationable) %>
<li id="related-content-<%= related_content.id %>">
<div>
<span class="related-content-title"><%= t("related_content.content_title.#{related.model_name.singular}") %></span><br>
<h3 class="inline-block">
<%= link_to related.title, related.url %>
</h3>
</div>
<% if current_user && related_content.author != current_user && !related_content.scored_by_user?(current_user) %>
<span id="<%= dom_id(related_content) %>" class="js-score-actions score-actions">
<%= render "relationable/score", related: related_content %>
</span>
<% end %>
</li>
<% end %>
</ul>
<%= paginate related_contents %>

View File

@@ -0,0 +1,18 @@
class Relationable::RelatedListComponent < ApplicationComponent
attr_reader :relationable
delegate :current_user, to: :helpers
def initialize(relationable)
@relationable = relationable
end
def render?
related_contents.present?
end
private
def related_contents
@related_contents ||= Kaminari.paginate_array(relationable.relationed_contents).page(params[:page]).per(5)
end
end