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 29, 2019
1 parent 03360bc commit 2b8fdbe
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 7 deletions.
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] : 'id'
end

def sort_direction
%w[asc desc].include?(params[:direction]) ? params[:direction] : 'asc'
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
8 changes: 4 additions & 4 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 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).not_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

0 comments on commit 2b8fdbe

Please sign in to comment.