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:
21
app/components/relationable/related_list_component.html.erb
Normal file
21
app/components/relationable/related_list_component.html.erb
Normal 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 %>
|
||||
18
app/components/relationable/related_list_component.rb
Normal file
18
app/components/relationable/related_list_component.rb
Normal 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
|
||||
Reference in New Issue
Block a user