tables can be sorted; optimized Agents load slightly

This commit is contained in:
Andrew Cantino 2014-09-10 00:23:14 -07:00
parent 5101cd5a8c
commit 77723a91d5
14 changed files with 180 additions and 26 deletions

View file

@ -36,15 +36,6 @@ body { padding-top: 60px; }
@extend .control-group.error;
}
table.events {
.payload {
color: #999;
font-size: 12px;
//text-align: center;
font-family: monospace;
}
}
.select2 {
float: none !important;
margin-left: 0 !important;

View file

@ -0,0 +1,31 @@
// Sortable table headers
.table th a.selected {
position: relative;
text-decoration: underline;
&.asc:after, &.desc:after {
text-decoration: none;
position: absolute;
top: -5px;
right: -12px;
font-size: 1.2em;
}
&.asc:after {
content: '\2193';
}
&.desc:after {
content: '\2191';
}
}
table.events {
.payload {
color: #999;
font-size: 12px;
//text-align: center;
font-family: monospace;
}
}

View file

@ -1,8 +1,11 @@
class AgentsController < ApplicationController
include DotHelper
include SortableTable
def index
@agents = current_user.agents.page(params[:page])
set_table_sort sorts: %w[name last_check_at last_event_at last_receive_at], default: { name: :asc }
@agents = current_user.agents.preload(:scenarios, :controllers).reorder(table_sort).page(params[:page])
respond_to do |format|
format.html

View file

@ -0,0 +1,53 @@
require 'active_support/concern'
module SortableTable
extend ActiveSupport::Concern
included do
helper SortableTableHelper
end
protected
def table_sort
raise("You must call set_table_sort in any action using table_sort.") unless @table_sort_info.present?
@table_sort_info[:order]
end
def set_table_sort(sort_options)
valid_sorts = sort_options[:sorts] || raise("You must specify :sorts as an array of valid sort attributes.")
default = sort_options[:default] || { valid_sorts.first.to_sym => :desc }
if params[:sort].present?
attribute, direction = params[:sort].downcase.split('.')
unless valid_sorts.include?(attribute)
attribute, direction = default.to_a.first
end
else
attribute, direction = default.to_a.first
end
direction = direction.to_s == 'desc' ? 'desc' : 'asc'
@table_sort_info = {
order: { attribute.to_sym => direction.to_sym },
attribute: attribute,
direction: direction
}
end
module SortableTableHelper
def sortable_column(attribute, name = attribute.humanize, default_direction = 'desc')
selected = @table_sort_info[:attribute].to_s == attribute
if selected
direction = @table_sort_info[:direction]
new_direction = direction.to_s == 'desc' ? 'asc' : 'desc'
classes = "selected #{direction}"
else
classes = ''
new_direction = default_direction
end
link_to(name, url_for(sort: "#{attribute}.#{new_direction}"), class: classes)
end
end
end

View file

@ -1,8 +1,11 @@
class ScenariosController < ApplicationController
include SortableTable
skip_before_filter :authenticate_user!, :only => :export
def index
@scenarios = current_user.scenarios.page(params[:page])
set_table_sort sorts: %w[name public], default: { name: :asc }
@scenarios = current_user.scenarios.reorder(table_sort).page(params[:page])
respond_to do |format|
format.html
@ -21,7 +24,9 @@ class ScenariosController < ApplicationController
def show
@scenario = current_user.scenarios.find(params[:id])
@agents = @scenario.agents.preload(:scenarios).page(params[:page])
set_table_sort sorts: %w[name last_check_at last_event_at last_receive_at], default: { name: :asc }
@agents = @scenario.agents.preload(:scenarios, :controllers).reorder(table_sort).page(params[:page])
respond_to do |format|
format.html

View file

@ -1,8 +1,12 @@
class ServicesController < ApplicationController
include SortableTable
before_filter :upgrade_warning, only: :index
def index
@services = current_user.services.page(params[:page])
set_table_sort sorts: %w[provider name global], default: { provider: :asc }
@services = current_user.services.reorder(table_sort).page(params[:page])
respond_to do |format|
format.html

View file

@ -1,6 +1,10 @@
class UserCredentialsController < ApplicationController
include SortableTable
def index
@user_credentials = current_user.user_credentials.page(params[:page])
set_table_sort sorts: %w[credential_name credential_value], default: { credential_name: :asc }
@user_credentials = current_user.user_credentials.reorder(table_sort).page(params[:page])
respond_to do |format|
format.html

View file

@ -31,7 +31,7 @@ module AgentHelper
end
def agent_controllers(agent, delimiter = ', ')
unless agent.controllers.empty?
if agent.controllers.present?
agent.controllers.map { |agent|
link_to(agent.name, agent_path(agent))
}.join(delimiter).html_safe

View file

@ -1,11 +1,11 @@
<div class='table-responsive'>
<table class='table table-striped'>
<tr>
<th>Name</th>
<th><%= sortable_column 'name', 'Name', 'asc' %></th>
<th>Schedule</th>
<th>Last Check</th>
<th>Last Event Out</th>
<th>Last Event In</th>
<th><%= sortable_column 'last_check_at', 'Last Check' %></th>
<th><%= sortable_column 'last_event_at', 'Last Event Out' %></th>
<th><%= sortable_column 'last_receive_at', 'Last Event In' %></th>
<th>Events Created</th>
<th>Working?</th>
<th></th>

View file

@ -12,9 +12,9 @@
<table class='table table-striped'>
<tr>
<th>Name</th>
<th><%= sortable_column 'name', 'Name', 'asc' %></th>
<th>Agents</th>
<th>Public</th>
<th><%= sortable_column 'public' %></th>
<th></th>
</tr>

View file

@ -25,9 +25,9 @@
<div class='table-responsive'>
<table class='table table-striped events'>
<tr>
<th>Provider</th>
<th>Username</th>
<th>Global?</th>
<th><%= sortable_column 'provider', 'Provider', 'asc' %></th>
<th><%= sortable_column 'name', 'Name', 'asc' %></th>
<th><%= sortable_column 'global', 'Global?' %></th>
<th></th>
</tr>

View file

@ -14,8 +14,8 @@
<table class='table table-striped'>
<tr>
<th>Name</th>
<th>Value</th>
<th><%= sortable_column 'credential_name', 'Name', 'asc' %></th>
<th><%= sortable_column 'credential_value', 'Value', 'asc' %></th>
</tr>
<% @user_credentials.each do |user_credential| %>

View file

@ -0,0 +1,61 @@
require 'spec_helper'
describe SortableTable do
class SortableTestController
attr_accessor :params
def self.helper(foo)
end
include SortableTable
public :set_table_sort
public :table_sort
end
describe "#set_table_sort" do
let(:controller) { SortableTestController.new }
let(:default) { { column2: :asc }}
let(:options) { { sorts: %w[column1 column2], default: default } }
it "uses a default when no sort is given" do
controller.params = {}
controller.set_table_sort options
controller.table_sort.should == default
end
it "applies the given sort when one is passed in" do
controller.params = { sort: "column1.desc" }
controller.set_table_sort options
controller.table_sort.should == { column1: :desc }
controller.params = { sort: "column1.asc" }
controller.set_table_sort options
controller.table_sort.should == { column1: :asc }
controller.params = { sort: "column2.desc" }
controller.set_table_sort options
controller.table_sort.should == { column2: :desc }
end
it "ignores unknown directions" do
controller.params = { sort: "column1.foo" }
controller.set_table_sort options
controller.table_sort.should == { column1: :asc }
controller.params = { sort: "column1.foo drop tables" }
controller.set_table_sort options
controller.table_sort.should == { column1: :asc }
end
it "ignores unknown columns" do
controller.params = { sort: "foo.asc" }
controller.set_table_sort options
controller.table_sort.should == default
controller.params = { sort: ";drop table;.asc" }
controller.set_table_sort options
controller.table_sort.should == default
end
end
end

View file

@ -55,6 +55,8 @@ RSpec.configure do |config|
config.global_fixtures = :all
config.treat_symbols_as_metadata_keys_with_true_values = true
config.render_views
config.include Devise::TestHelpers, :type => :controller
config.include SpecHelpers
config.include Delorean