Skip to content

Commit

Permalink
Initial sortable tables
Browse files Browse the repository at this point in the history
This adds server-side sorting for the
columns on the `zizia_csv_import_details` table.

Sorting the additional headers will require more
complex queries or a different solution.

Connected to curationexperts/in-house#420
  • Loading branch information
little9 committed Oct 31, 2019
1 parent c436d29 commit 2263230
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 10 deletions.
6 changes: 6 additions & 0 deletions app/assets/stylesheets/zizia/_import_table.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.desc::after {
content: ''
}
.asc::after {
content: ''
}
1 change: 1 addition & 0 deletions app/assets/stylesheets/zizia/zizia.scss
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
@import 'file_upload';
@import 'field_guide';
@import 'new';
@import 'import_table';

.btn {
white-space:normal !important;
Expand Down
15 changes: 12 additions & 3 deletions app/controllers/zizia/csv_import_details_controller.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
# frozen_string_literal: true
module Zizia
class CsvImportDetailsController < ApplicationController
helper_method :sort_column, :sort_direction
load_and_authorize_resource
with_themed_layout 'dashboard'

def index
@csv_import_details = Zizia::CsvImportDetail.order(:id).page csv_import_detail_params[:page]
@csv_import_details = Zizia::CsvImportDetail.order(sort_column + ' ' + sort_direction).page csv_import_detail_params[:page]
end

def show
@csv_import_detail = Zizia::CsvImportDetail.find(csv_import_detail_params["id"])
@csv_import_detail = Zizia::CsvImportDetail.find(csv_import_detail_params['id'])
end

private

def sort_column
Zizia::CsvImportDetail.column_names.include?(params[:sort]) ? params[:sort] : 'created_at'
end

def sort_direction
%w[asc desc].include?(params[:direction]) ? params[:direction] : 'desc'
end

def csv_import_detail_params
params.permit(:id, :page)
params.permit(:id, :page, :sort, :direction)
end
end
end
7 changes: 7 additions & 0 deletions app/helpers/zizia/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
# frozen_string_literal: true
module Zizia
module ApplicationHelper
def sortable(column, title = nil)
title ||= column.titleize
css_class = column == sort_column ? "current #{sort_direction}" : nil
direction = column == sort_column && sort_direction == 'asc' ? 'desc' : 'asc'
link_to title, { sort: column, direction: direction }, class: css_class
end

def collections_for_select
ActiveFedora::SolrService.query('has_model_ssim:Collection').map do |c|
[c['title_tesim'][0], c['id']]
Expand Down
12 changes: 6 additions & 6 deletions app/views/zizia/csv_import_details/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<h3>CSV Imports</h3>
<table class="table table-striped">
<tr>
<th>ID</th>
<th><%= sortable 'id', 'ID' %></th>
<th>Associated User</th>
<th>Date</th>
<th><%= sortable 'created_at', 'Date' %></th>
<th>CSV File</th>
<th>Number of Works</th>
<th>Number of Files</th>
<th>Total Size</th>
<th>Status</th>
<th>Overwrite Behavior Type</th>
<th><%= sortable 'status' %></th>
<th><%= sortable 'update_actor_stack', 'Overwrite Behavior Type' %></th>
</tr>
<% @csv_import_details.each do |csv_import_detail| %>
<tr>
Expand All @@ -23,7 +23,7 @@
<%= csv_import_detail.created_at.strftime("%B %-d, %Y %H:%M") %>
</td>
<td>
<%= csv_import_detail.csv_import.manifest %>
<%= File.basename(csv_import_detail.csv_import.manifest.to_s) %>
</td>
<td>
<%= csv_import_detail.pre_ingest_works.count %>
Expand All @@ -32,7 +32,7 @@
<%= csv_import_detail.pre_ingest_files.count %>
</td>
<td>
<%= number_to_human_size(csv_import_detail.total_size) %>
<%= number_to_human_size(csv_import_detail.total_size ||= 0) %>
</td>
<td>
<%= csv_import_detail.status %>
Expand Down
28 changes: 28 additions & 0 deletions spec/dummy/spec/system/csv_import_details_page_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
RSpec.describe 'viewing the csv import detail page' do
let(:csv_import) { FactoryBot.create(:csv_import) }
let(:csv_import_detail) { FactoryBot.create_list(:csv_import_detail, 12, created_at: Time.parse('Tue, 29 Oct 2019 14:20:02 UTC +00:00').utc) }
let(:csv_import_detail_second) { FactoryBot.create(:csv_import_detail, created_at: Time.parse('Thur, 31 Oct 2019 14:20:02 UTC +00:00').utc, status: 'zippy', update_actor_stack: 'ZiziaTesting') }
let(:user) { FactoryBot.create(:admin) }

before do
user.save
csv_import.user_id = user.id
csv_import.save
csv_import_detail.each(&:save)
csv_import_detail_second.save
login_as user
end

Expand All @@ -23,6 +25,32 @@
expect(page).to have_content('Total Size')
end

it 'has links to sort' do
visit ('/csv_import_details/index')
expect(page).to have_content ('ZiziaTesting')
click_on('Overwrite Behavior Type')
expect(page.current_url).to match(/index\?direction\=asc\&locale\=en\&sort\=update_actor_stack/)
expect(page).not_to have_content('ZiziaTesting')
visit('/csv_import_details/index?direction=desc&locale=en&sort=update_actor_stack')
expect(page).to have_content('ZiziaTesting')
end

it 'has a sortable id' do
visit('/csv_import_details/index?direction=desc&locale=en&sort=id')
expect(page).to have_link '13'
end

it 'has a sortable status' do
pending 'status is always undetermined currently'
visit('/csv_import_details/index?direction=asc&locale=en&sort=status')
expect(page).to have_content 'zippy'
end

it 'has a sortable date' do
visit('/csv_import_details/index?direction=desc&locale=en&sort=created_at')
expect(page).to have_content 'October 31'
end

it 'displays the metadata when you visit the page' do
visit ('/csv_import_details/index')
expect(page).to have_content('CSV Imports ID')
Expand Down
2 changes: 1 addition & 1 deletion spec/dummy/spec/system/import_from_csv_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@
# Viewing additional details after an import
visit "/csv_import_details/index"
expect(page).to have_content('Total Size')
find(:xpath, '//*[@id="content-wrapper"]/div[2]/table/tbody/tr[2]/td[1]/a').click
find(:xpath, '//*[@id="content-wrapper"]/div[2]/table/tbody/tr[3]/td[1]/a').click
expect(page).to have_content('dog.jpg')
expect(page).to have_content('5.74 MB')
end
Expand Down

0 comments on commit 2263230

Please sign in to comment.