indentlabs/notebook

View on GitHub
app/views/content/list/_list.html.erb

Summary

Maintainability
Test Coverage
<%# Usage: render partial: 'content/list/list', locals: { content_list: @content, content_type: @content_type_class, show_add_another_form: true } %>

<%
  category_ids_for_this_content_type = AttributeCategory.where(entity_type: content_type.name.downcase, user_id: current_user).pluck(:id)
  name_field = AttributeField.find_by(field_type: 'name', attribute_category_id: category_ids_for_this_content_type)
  # todo description field

  content_ids = content_list.pluck(:id)
  if name_field
    list_name_lookup_cache = Hash[
      name_field.attribute_values.where(
        entity_type: content_type.name
      ).pluck(:entity_type, :entity_id, :value).map { |page_type, page_id, value| ["#{page_type}_#{page_id}", value]}
    ]
  else
    list_name_lookup_cache = {}
  end
%>

<ul class="collection <%= 'with-header' if title.present? %>" style='border: 0'>
  <% if title.present? %>
    <li class="collection-header"><h5><%= title %></h5></li>
  <% end %>

  <% content_list.each do |content| %>
    <li class="collection-item avatar">
      <%= link_to content do %>
        <i class="material-icons circle <%= content.class.color %>"><%= content.class.icon %></i>
      <% end %>
      <span class="title">
        <%= link_to (content.respond_to?(:label) ? content.label : list_name_lookup_cache["#{content.page_type}_#{content.id}"].presence || content.name.presence || "Untitled #{content.page_type}"), content %>
        <small class="grey-text">
          <% universe_field_value = content.universe_field_value %>
          <% if universe_field_value.present? %>
            <i class="material-icons"><%= Universe.icon %></i>
            <%# todo remove this hack %>
            <%
              if universe_field_value.is_a?(Integer)
                universe_field_value = Universe.find_by(id: universe_field_value, user: content.user)
              end
            %>
            <% if universe_field_value %>
              <%= link_to universe_field_value.try(:name), universe_field_value %>
            <% end %>
          <% end %>
        </small>
        <% if user_signed_in? && (
            (content.universe_field_value.present? && universe_field_value.try(:user) != current_user) ||
            (content.is_a?(Universe) && content.user != current_user)
          )
        %>
          <span class="new badge grey white-text tooltipped" style="float: inherit; margin: 0;" data-badge-caption="contributing"
            data-position="bottom" data-delay="100" data-tooltip="You have been added as a contributor to this universe and its content."></span>
        <% end %>
      </span>
      <p class="tags-container">
        <% if content.respond_to?(:page_tags) %>
          <% content.page_tags.each do |tag| %>
            <% if user_signed_in? && content.user == current_user %>
              <%= link_to params.permit(:tag).merge({ tag: PageTagService.slug_for(tag.tag) }) do %>
                <span class="new badge <%= params[:tag] == tag.slug ? content.class.color : 'grey' %> left" data-badge-caption="<%= tag.tag %>"></span>
              <% end %>
            <% else %>
              <span class="new badge <%= params[:tag] == tag.slug ? content.class.color : 'grey' %> left" data-badge-caption="<%= tag.tag %>"></span>
            <% end %>
          <% end %>
        <% end %>
      </p>
      <p class='grey-text' style="clear: both">
        <% if user_signed_in? %>
          <span class="timestamp tooltipped" data-position="bottom" data-delay="500" data-tooltip="<%= content.updated_at.strftime("%m/%d/%Y %H:%M") %>" style="font-size: 80%">
            <i class="material-icons tiny left">mode_edit</i>

            <%
              if content.updated_at == content.created_at
                action = 'created'
              else
                action = 'updated'
              end
            %>

            <%= action %> <%= time_ago_in_words content.updated_at %> ago
          </span>
        <% end %>
      </p>
      <span class="secondary-content hide-on-med-and-up">
        <% if user_signed_in? && content.updatable_by?(current_user) %>
          <%# todo also show if you're a contributor %>
          <%= link_to edit_polymorphic_path(content.page_type.downcase, id: content.id), class: 'js-edit-hover' do %>
            <i class="material-icons">edit</i>
          <% end %>
        <% end %>
      </span>
    </li>
  <% end %>

  <% if local_assigns[:show_add_another_form] && content_list.any? && current_user.can_create?(content_type) %>
    <%# todo real permissions here %>
    <% if content_type == Character || content_type == Location || content_type == Item || current_user.on_premium_plan? %>
      <% unless content_type == Universe %>
        <%= render partial: 'content/list/quick_add_form', locals: { content_type: content_type } %>
      <% end %>
    <% end %>
  <% end %>

</ul>