sortable-table 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE +21 -0
- data/README.md +162 -0
- data/Rakefile +1 -0
- data/lib/sortable-table.rb +8 -0
- data/lib/sortable_table/helpers/action_view_extension.rb +16 -0
- data/lib/sortable_table/models/sort_column.rb +12 -0
- data/lib/sortable_table/models/sort_column_custom_definition.rb +14 -0
- data/lib/sortable_table/models/sort_column_definition.rb +13 -0
- data/lib/sortable_table/models/sort_table.rb +19 -0
- data/lib/sortable_table/railtie.rb +9 -0
- data/lib/sortable_table/version.rb +3 -0
- data/sortable-table.gemspec +31 -0
- data/spec/fake_app/log/development.log +0 -0
- data/spec/fake_app/rails_app.rb +26 -0
- data/spec/helpers/action_view_extension_spec.rb +50 -0
- data/spec/models/sort_column_custom_definition_spec.rb +44 -0
- data/spec/models/sort_column_definition_spec.rb +28 -0
- data/spec/models/sort_column_spec.rb +19 -0
- data/spec/models/sort_table_spec.rb +66 -0
- data/spec/spec_helper.rb +2 -0
- metadata +174 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a955921e2db9b9ed641b8747366ba2e28f0a3fca
|
4
|
+
data.tar.gz: 68cc86e153706bf32411c61536197b5d6fcd913e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a7fbebbb2210f86fc87eaa3a5fb55240a3b0f6be5293f33e06fa98f2f754a5f29ff471f59b3fc92280af68e9daa84f1bb960e63d5fba98d094cee517de6b1cfc
|
7
|
+
data.tar.gz: 30a48be0ec2089ccbbadf651f61184152fddd46d6ff6c8054e4a30cf734b0e6caaff4a8eed752f6358d2bc7f633472c148871b77ac17761f7b01dc45e8f74b6f
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2014 Caselle
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,162 @@
|
|
1
|
+
# Sortable Table by [Caselle](http://www.caselle.com/)
|
2
|
+
|
3
|
+
The sortable-table gem allows you to sort table columns similar to [RailsCasts #228 Sortable Table Columns](http://railscasts.com/episodes/228-sortable-table-columns).
|
4
|
+
|
5
|
+
Rather than adding helper methods to a controller, sortable-table creates a sort column object. This approach provides the following benefits:
|
6
|
+
|
7
|
+
1. More complex sort criteria can be defined for a column. The RailsCasts approach only allows a single column to be sorted. This approach allows additional columns to be involved in the column sort.
|
8
|
+
2. The sorting logic is not duplicated across controllers, and controllers don't have to define helper methods.
|
9
|
+
3. It makes it easier for multiple tables on a page to have sortable columns.
|
10
|
+
|
11
|
+
## Installing
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'sortable-table', github: 'caselle/sortable-table'
|
15
|
+
```
|
16
|
+
|
17
|
+
## Configuration
|
18
|
+
|
19
|
+
Using sortable table involves the following steps:
|
20
|
+
|
21
|
+
1. Define which columns in your table can be sorted, and how they are sorted.
|
22
|
+
2. Using the parameters passed into your controller action, get the current sort column.
|
23
|
+
3. Use the current sort column to order your data.
|
24
|
+
4. Use the ```sort_by``` helper, to add links to the table header row which allow the table to be re-sorted by those columns.
|
25
|
+
|
26
|
+
### Controller
|
27
|
+
|
28
|
+
Create sort column definitions which define how each column can be sorted. Create a sort table that holds those sort column definitions as well as optional default sort column information. Then use the sort table to get the current sort column.
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
def sort_column
|
32
|
+
date_sort = SortableTable::SortColumnCustomDefinition.new('date',
|
33
|
+
asc: 'date asc, number asc',
|
34
|
+
desc: 'date desc, number desc')
|
35
|
+
number_sort = SortableTable::SortColumnDefinition.new('number')
|
36
|
+
sort_table = SortableTable::SortTable.new([date_sort, number_sort])
|
37
|
+
sort_table.sort_column(params[:sort], params[:direction])
|
38
|
+
end
|
39
|
+
```
|
40
|
+
Use the current sort column to order the records you pass to the view.
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
def index
|
44
|
+
@sort_column = sort_column
|
45
|
+
@items = Model.order(@sort_column.order)
|
46
|
+
end
|
47
|
+
```
|
48
|
+
|
49
|
+
Make the sort column available to the view, it will be used by the ```sort_by``` helper.
|
50
|
+
|
51
|
+
### View
|
52
|
+
|
53
|
+
Use the ```sort_by``` helper to add links to columns which you want to sort.
|
54
|
+
|
55
|
+
```haml
|
56
|
+
%table
|
57
|
+
%tbody
|
58
|
+
%tr
|
59
|
+
%th= sort_by 'number', title: 'Number', current_column: @sort_column
|
60
|
+
%th= sort_by 'date', title: 'Date', current_column: @sort_column
|
61
|
+
%th Description
|
62
|
+
%tbody
|
63
|
+
= render @items
|
64
|
+
```
|
65
|
+
|
66
|
+
If the column passed to the ```sort_by``` is the current sort column, the sort link it creates will switch the direction that the column is sorted by.
|
67
|
+
|
68
|
+
## Sorting Multiple Tables
|
69
|
+
|
70
|
+
If you have multipe tables to sort on a view, then you'll need to add a prefix to the sorting params for each table. E.g., if you have a foo table and a bar table, instead of just having :sort and :direction params, you would want ```:foo_sort```, ```:foo_direction```, ```:bar_sort```, and ```:bar_direction``` params. In other words, you'll want a table-specific prefix for each param.
|
71
|
+
|
72
|
+
To add a table-specific prefix, do the following:
|
73
|
+
|
74
|
+
Assign a sort column for each table. Use the param prefix when generating the sort column.
|
75
|
+
|
76
|
+
```ruby
|
77
|
+
sort_table.sort_column(params[:foo_sort], params[:foo_direction])
|
78
|
+
```
|
79
|
+
|
80
|
+
Pass ```:prefix``` to the ```sort_by helper```.
|
81
|
+
|
82
|
+
```haml
|
83
|
+
%th= sort_by 'name', title: 'Name', prefix: 'foo_', current_column: @foo_sort_column
|
84
|
+
```
|
85
|
+
|
86
|
+
## <a name="sort_column_definitions"></a>Sort Column Definitions
|
87
|
+
|
88
|
+
There are two types of sort column definitions: ```SortColumnDefinition``` and ```SortColumnCustomDefinition```.
|
89
|
+
|
90
|
+
### Sort Column Definition
|
91
|
+
|
92
|
+
```SortColumnDefinition``` simply allows you to pass the column used to sort. Depending on the sort direction the order will simply be "<column> asc" or "<column> desc".
|
93
|
+
|
94
|
+
The column should be have the same casing as the database. E.g., if the column is "Timmy", create ```SortableTable::SortColumnDefinition.new('Timmy')``` not ```SortableTable::SortColumnDefinition.new('timmy')```.
|
95
|
+
|
96
|
+
The column name is the key used by the [SortTable](#sort_table).
|
97
|
+
|
98
|
+
### Sort Column Custom Definition
|
99
|
+
|
100
|
+
```SortColumnCustomDefinition``` allows you to specify any custom order for ascending and descending directions. The first parameter passed when creating a ```SortColumnCustomDefinition``` is the "key" used by the [SortTable](#sort_table).
|
101
|
+
|
102
|
+
```ruby
|
103
|
+
date_sort = SortableTable::SortColumnCustomDefinition.new('date',
|
104
|
+
asc: 'date asc, number asc',
|
105
|
+
desc: 'date desc, number desc')
|
106
|
+
```
|
107
|
+
|
108
|
+
## <a name="sort_table"></a>Sort Table
|
109
|
+
|
110
|
+
A ```SortTable``` is used to create the current sort column based on the params passed to the controller action.
|
111
|
+
|
112
|
+
### Creation
|
113
|
+
|
114
|
+
To create a ```SortTable```, pass in the [sort column definitions](#sort_column_definitions) for the table.
|
115
|
+
|
116
|
+
Additional options that can be passed as well are:
|
117
|
+
|
118
|
+
- ```default_column```: The column to use for sorting if no sort column param is passed to the controller action. If this option is not passed, the first sort column definition is used. The value for this is the "key" of a [sort column definition](#sort_column_definitions).
|
119
|
+
- ```default_direction```: The direction to use for sorting the default column if no sort direction param is passed to the controller action. If this option is not passed, :asc is used. :asc or :desc
|
120
|
+
|
121
|
+
In the following example, date and number sort column definitions are created. If no sort params are passed to the controller action, the current sort column will be ordered by date ascending.
|
122
|
+
|
123
|
+
```ruby
|
124
|
+
date_sort = SortableTable::SortColumnDefinition.new('date')
|
125
|
+
number_sort = SortableTable::SortColumnDefinition.new('number')
|
126
|
+
sort_table = SortableTable::SortTable.new([date_sort, number_sort])
|
127
|
+
```
|
128
|
+
|
129
|
+
In this example, date and number sort column definitions are created, and the default order will be number descending.
|
130
|
+
|
131
|
+
```ruby
|
132
|
+
date_sort = SortableTable::SortColumnDefinition.new('date')
|
133
|
+
number_sort = SortableTable::SortColumnDefinition.new('number')
|
134
|
+
sort_table = SortableTable::SortTable.new([date_sort, number_sort],
|
135
|
+
default_column: 'number', default_direction: :desc)
|
136
|
+
```
|
137
|
+
|
138
|
+
### Current Sort Column
|
139
|
+
|
140
|
+
Call the ```sort_column``` SortTable method to get the current sort column.
|
141
|
+
|
142
|
+
```ruby
|
143
|
+
sort_table.sort_column(params[:sort], params[:direction])
|
144
|
+
```
|
145
|
+
|
146
|
+
If ```params[:sort]``` is missing, the sort table default column will be used. If ```params[:sort]``` is not a recognized sort column definition key, the sort table default column will be used.
|
147
|
+
|
148
|
+
If ```params[:direction]``` is missing, the sort table default direction will be used.
|
149
|
+
|
150
|
+
## Sort Column
|
151
|
+
|
152
|
+
A ```SortColumn``` represents the current sort column. It has the following attributes:
|
153
|
+
|
154
|
+
- ```order```: The order that can be passed to an ActiveRecord order method. ```YourModel.order(sort_column.order)```
|
155
|
+
- ```column```: The sort column. Corresponds to a [sort column definition](#sort_column_definitions) key.
|
156
|
+
- ```direction```: The sort direction, 'asc' or 'desc'.
|
157
|
+
|
158
|
+
Generally, a user of ```SortColumn``` will only use the ```order``` attribute. The other attributes are used by helper methods.
|
159
|
+
|
160
|
+
## License
|
161
|
+
|
162
|
+
sortable-table is available under the MIT license. See the LICENSE file for more info.
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
@@ -0,0 +1,8 @@
|
|
1
|
+
require 'sortable_table/version'
|
2
|
+
require 'sortable_table/models/sort_column'
|
3
|
+
require 'sortable_table/models/sort_column_custom_definition'
|
4
|
+
require 'sortable_table/models/sort_column_definition'
|
5
|
+
require 'sortable_table/models/sort_table'
|
6
|
+
require 'sortable_table/helpers/action_view_extension'
|
7
|
+
|
8
|
+
require 'sortable_table/railtie' if defined?(Rails)
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module SortableTable
|
2
|
+
module ActionViewExtension
|
3
|
+
def sort_by(column, options = {})
|
4
|
+
current_column = options[:current_column] || SortableTable::SortColumn.new(nil, nil)
|
5
|
+
title = options[:title] || column.titleize
|
6
|
+
prefix = options[:prefix]
|
7
|
+
is_current_column = column == current_column.column
|
8
|
+
css_class = is_current_column ? "current #{current_column.direction}" : nil
|
9
|
+
direction = is_current_column && current_column.direction == 'asc' ? 'desc' : 'asc'
|
10
|
+
link_to title, params.merge("#{prefix}sort" => column,
|
11
|
+
"#{prefix}direction" => direction,
|
12
|
+
"#{prefix}page" => nil),
|
13
|
+
class: css_class
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module SortableTable
|
2
|
+
class SortColumn
|
3
|
+
SORT_DIRECTIONS = %w(asc desc)
|
4
|
+
attr_reader :column, :direction, :order
|
5
|
+
|
6
|
+
def initialize(sort_column, sort_direction, options = {})
|
7
|
+
@column = sort_column
|
8
|
+
@direction = SORT_DIRECTIONS.include?(sort_direction) ? sort_direction : 'asc'
|
9
|
+
@order = options[:order] || "#{column} #{direction}"
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module SortableTable
|
2
|
+
class SortColumnCustomDefinition
|
3
|
+
attr_reader :column, :ordering
|
4
|
+
|
5
|
+
def initialize(column, options = {})
|
6
|
+
@column = column
|
7
|
+
@ordering = { 'asc' => options[:asc], 'desc' => options[:desc] }
|
8
|
+
end
|
9
|
+
|
10
|
+
def create_sort_column(direction)
|
11
|
+
SortColumn.new(column, direction, order: ordering[direction] || ordering['asc'])
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module SortableTable
|
2
|
+
class SortTable
|
3
|
+
attr_reader :column_definitions, :default_column, :default_direction
|
4
|
+
|
5
|
+
def initialize(column_definitions, options = {})
|
6
|
+
@default_column = options[:default_column] || column_definitions.first.column
|
7
|
+
@default_direction = options[:default_direction] || :asc
|
8
|
+
@column_definitions = column_definitions.each_with_object({}) do |column_definition, acc|
|
9
|
+
acc[column_definition.column] = column_definition
|
10
|
+
acc
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def sort_column(column, direction)
|
15
|
+
column_definition = column_definitions[column] || column_definitions[default_column]
|
16
|
+
column_definition.create_sort_column(direction || default_direction.to_s)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'sortable_table/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = 'sortable-table'
|
8
|
+
s.version = SortableTable::VERSION
|
9
|
+
s.platform = Gem::Platform::RUBY
|
10
|
+
s.authors = ['Jay Mitchell']
|
11
|
+
s.email = ['jaybmitchell@gmail.com']
|
12
|
+
s.homepage = 'https://github.com/caselle/sortable-table'
|
13
|
+
s.summary = 'Sort HTML table columns'
|
14
|
+
s.description = 'Sort HTML table columns in Rails 4 applications.'
|
15
|
+
|
16
|
+
s.licenses = ['MIT']
|
17
|
+
|
18
|
+
s.files = `git ls-files -z`.split("\x0")
|
19
|
+
s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
20
|
+
s.test_files = s.files.grep(%r{^(test|spec|features)/})
|
21
|
+
s.require_paths = ["lib"]
|
22
|
+
|
23
|
+
s.add_dependency 'activesupport', '>= 3.0.0'
|
24
|
+
s.add_dependency 'actionpack', '>= 3.0.0'
|
25
|
+
|
26
|
+
s.add_development_dependency 'bundler', '~> 1.5'
|
27
|
+
s.add_development_dependency 'rake'
|
28
|
+
s.add_development_dependency 'rspec-rails'
|
29
|
+
s.add_development_dependency 'tzinfo-data'
|
30
|
+
s.add_development_dependency 'rails'
|
31
|
+
end
|
File without changes
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# require 'rails/all'
|
2
|
+
require 'action_controller/railtie'
|
3
|
+
require 'action_view/railtie'
|
4
|
+
|
5
|
+
# config
|
6
|
+
app = Class.new(Rails::Application)
|
7
|
+
app.config.secret_token = 'e6c3eab00071ba648d96fc1880264d03546f3e02333b6411ce941fecf9360d0c4449f810e5483562c15a579f4a1cf11d710d33783ec48d07854e42ffc7a59232'
|
8
|
+
app.config.session_store :cookie_store, :key => '_myapp_session'
|
9
|
+
app.config.active_support.deprecation = :log
|
10
|
+
app.config.eager_load = false
|
11
|
+
# Rais.root
|
12
|
+
app.config.root = File.dirname(__FILE__)
|
13
|
+
Rails.backtrace_cleaner.remove_silencers!
|
14
|
+
app.initialize!
|
15
|
+
|
16
|
+
# routes
|
17
|
+
app.routes.draw do
|
18
|
+
resources :items
|
19
|
+
end
|
20
|
+
|
21
|
+
# controllers
|
22
|
+
class ApplicationController < ActionController::Base; end
|
23
|
+
class ItemsController < ApplicationController; end
|
24
|
+
|
25
|
+
# helpers
|
26
|
+
Object.const_set(:ApplicationHelper, Module.new)
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'fake_app/rails_app'
|
2
|
+
require 'rspec/rails'
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe 'SortableTable::ActionViewExtension', type: :helper do
|
6
|
+
describe '#sort_by' do
|
7
|
+
it 'add the column as the sort column and the title as the link text' do
|
8
|
+
allow(helper).to receive(:params).and_return(controller: :items, action: :index)
|
9
|
+
|
10
|
+
link = helper.sort_by('date', title: 'Title')
|
11
|
+
|
12
|
+
expect(link).to eq('<a href="/items?direction=asc&sort=date">Title</a>')
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'titleize the column and use it as the link text if title is missing' do
|
16
|
+
allow(helper).to receive(:params).and_return(controller: :items, action: :index)
|
17
|
+
|
18
|
+
link = helper.sort_by('description')
|
19
|
+
|
20
|
+
expect(link).to eq('<a href="/items?direction=asc&sort=description">Description</a>')
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'sorting the same column should turn asc to desc order and remove page parameter' do
|
24
|
+
current_column = SortableTable::SortColumn.new('number', 'asc')
|
25
|
+
allow(helper).to receive(:params).and_return(controller: :items, action: :index, page: 1)
|
26
|
+
|
27
|
+
link = helper.sort_by('number', title: 'Title', current_column: current_column)
|
28
|
+
|
29
|
+
expect(link).to eq('<a class="current asc" href="/items?direction=desc&sort=number">Title</a>')
|
30
|
+
end
|
31
|
+
|
32
|
+
it "clicking the same column should turn desc to asc order and remove page parameter" do
|
33
|
+
current_column = SortableTable::SortColumn.new('number', 'desc')
|
34
|
+
allow(helper).to receive(:params).and_return(controller: :items, action: :index, page: 1)
|
35
|
+
|
36
|
+
link = helper.sort_by('number', title: 'Title', current_column: current_column)
|
37
|
+
|
38
|
+
expect(link).to eq('<a class="current desc" href="/items?direction=asc&sort=number">Title</a>')
|
39
|
+
end
|
40
|
+
|
41
|
+
it "Adding prefix to parameters" do
|
42
|
+
current_column = SortableTable::SortColumn.new('number', 'desc')
|
43
|
+
allow(helper).to receive(:params).and_return(controller: :items, action: :index, invoice_page: 1)
|
44
|
+
|
45
|
+
link = helper.sort_by('number', title: 'Title', current_column: current_column, prefix: 'invoice_')
|
46
|
+
|
47
|
+
expect(link).to eq('<a class="current desc" href="/items?invoice_direction=asc&invoice_sort=number">Title</a>')
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module SortableTable
|
2
|
+
describe SortColumnCustomDefinition do
|
3
|
+
it 'has a column' do
|
4
|
+
definition = SortColumnCustomDefinition.new('Name')
|
5
|
+
expect(definition.column).to eq('Name')
|
6
|
+
end
|
7
|
+
|
8
|
+
it 'can create an ascending sort column' do
|
9
|
+
definition = SortColumnCustomDefinition.new('Jimmy',
|
10
|
+
asc: 'Jimmy asc, Timmy desc',
|
11
|
+
desc: 'Jimmy desc, Timmy asc')
|
12
|
+
|
13
|
+
sort_column = definition.create_sort_column('asc')
|
14
|
+
|
15
|
+
expect(sort_column.column).to eq('Jimmy')
|
16
|
+
expect(sort_column.direction).to eq('asc')
|
17
|
+
expect(sort_column.order).to eq('Jimmy asc, Timmy desc')
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'creates an ascending sort column when no sort direction is passed' do
|
21
|
+
definition = SortColumnCustomDefinition.new('Jimmy',
|
22
|
+
asc: 'Jimmy asc, Timmy desc',
|
23
|
+
desc: 'Jimmy desc, Timmy asc')
|
24
|
+
|
25
|
+
sort_column = definition.create_sort_column(nil)
|
26
|
+
|
27
|
+
expect(sort_column.column).to eq('Jimmy')
|
28
|
+
expect(sort_column.direction).to eq('asc')
|
29
|
+
expect(sort_column.order).to eq('Jimmy asc, Timmy desc')
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'can create a descending sort column' do
|
33
|
+
definition = SortColumnCustomDefinition.new('Jimmy',
|
34
|
+
asc: 'Jimmy asc, Timmy desc',
|
35
|
+
desc: 'Jimmy desc, Timmy asc')
|
36
|
+
|
37
|
+
sort_column = definition.create_sort_column('desc')
|
38
|
+
|
39
|
+
expect(sort_column.column).to eq('Jimmy')
|
40
|
+
expect(sort_column.direction).to eq('desc')
|
41
|
+
expect(sort_column.order).to eq('Jimmy desc, Timmy asc')
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module SortableTable
|
2
|
+
describe SortColumnDefinition do
|
3
|
+
it 'has column' do
|
4
|
+
definition = SortColumnDefinition.new('InvoiceNumber')
|
5
|
+
expect(definition.column).to eq('InvoiceNumber')
|
6
|
+
end
|
7
|
+
|
8
|
+
it 'can create an ascending sort column' do
|
9
|
+
definition = SortColumnDefinition.new('Timmy')
|
10
|
+
|
11
|
+
sort_column = definition.create_sort_column('asc')
|
12
|
+
|
13
|
+
expect(sort_column.column).to eq('Timmy')
|
14
|
+
expect(sort_column.direction).to eq('asc')
|
15
|
+
expect(sort_column.order).to eq('Timmy asc')
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'can create a descending sort column' do
|
19
|
+
definition = SortColumnDefinition.new('Jimmy')
|
20
|
+
|
21
|
+
sort_column = definition.create_sort_column('desc')
|
22
|
+
|
23
|
+
expect(sort_column.column).to eq('Jimmy')
|
24
|
+
expect(sort_column.direction).to eq('desc')
|
25
|
+
expect(sort_column.order).to eq('Jimmy desc')
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module SortableTable
|
2
|
+
describe SortColumn do
|
3
|
+
it 'direction only allows asc and desc' do
|
4
|
+
expect(SortColumn.new('Timmy', 'asc').direction).to eq('asc')
|
5
|
+
expect(SortColumn.new('Timmy', 'desc').direction).to eq('desc')
|
6
|
+
expect(SortColumn.new('Timmy', 'Jimmy').direction).to eq('asc')
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'create order based on column and direction' do
|
10
|
+
expect(SortColumn.new('Timmy', 'asc').order).to eq('Timmy asc')
|
11
|
+
expect(SortColumn.new('Timmy', 'desc').order).to eq('Timmy desc')
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'allows a custom order' do
|
15
|
+
column = SortColumn.new('Name', 'asc', order: 'Name asc, Number desc')
|
16
|
+
expect(column.order).to eq('Name asc, Number desc')
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
module SortableTable
|
2
|
+
describe SortTable do
|
3
|
+
describe "sort_column" do
|
4
|
+
it 'use the first column ascending as the default sort when not specified' do
|
5
|
+
number_sort_definition = SortColumnDefinition.new('Number')
|
6
|
+
name_sort_definition = SortColumnDefinition.new('Name')
|
7
|
+
sort_table = SortTable.new([number_sort_definition, name_sort_definition])
|
8
|
+
|
9
|
+
sort_column = sort_table.sort_column(nil, nil)
|
10
|
+
|
11
|
+
expect(sort_column.order).to eq('Number asc')
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'return default ordering information if sort column is not passed' do
|
15
|
+
number_sort_definition = SortColumnDefinition.new('Number')
|
16
|
+
name_sort_definition = SortColumnDefinition.new('Name')
|
17
|
+
sort_table = SortTable.new([number_sort_definition, name_sort_definition],
|
18
|
+
default_column: 'Name', default_direction: :desc)
|
19
|
+
|
20
|
+
sort_column = sort_table.sort_column(nil, nil)
|
21
|
+
|
22
|
+
expect(sort_column.order).to eq('Name desc')
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'return default ordering information for a nil column, passed sort direction' do
|
26
|
+
number_definition = SortColumnDefinition.new('InvoiceNumber')
|
27
|
+
date_definition = SortColumnDefinition.new('InvoiceDate')
|
28
|
+
sort_table = SortTable.new([number_definition, date_definition], default_column: 'InvoiceNumber')
|
29
|
+
|
30
|
+
sort_column = sort_table.sort_column(nil, 'asc')
|
31
|
+
|
32
|
+
expect(sort_column.column).to eq('InvoiceNumber')
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'return default ordering information for a column that is not sortable' do
|
36
|
+
number_definition = SortColumnDefinition.new('InvoiceNumber')
|
37
|
+
date_definition = SortColumnDefinition.new('InvoiceDate')
|
38
|
+
sort_table = SortTable.new([number_definition, date_definition], default_column: 'InvoiceNumber')
|
39
|
+
|
40
|
+
sort_column = sort_table.sort_column('Invoice', 'asc')
|
41
|
+
|
42
|
+
expect(sort_column.column).to eq('InvoiceNumber')
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'return asc ordering information for a column that is sortable' do
|
46
|
+
number_definition = SortColumnDefinition.new('InvoiceNumber')
|
47
|
+
date_definition = SortColumnDefinition.new('InvoiceDate')
|
48
|
+
sort_table = SortTable.new([number_definition, date_definition], default_column: 'InvoiceNumber')
|
49
|
+
|
50
|
+
sort_column = sort_table.sort_column('InvoiceDate', 'asc')
|
51
|
+
|
52
|
+
expect(sort_column.order).to eq('InvoiceDate asc')
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'return desc ordering information for a column that is sortable' do
|
56
|
+
number_definition = SortColumnDefinition.new('InvoiceNumber')
|
57
|
+
date_definition = SortColumnDefinition.new('InvoiceDate')
|
58
|
+
sort_table = SortTable.new([number_definition, date_definition], default_column: 'InvoiceNumber')
|
59
|
+
|
60
|
+
sort_column = sort_table.sort_column('InvoiceDate', 'desc')
|
61
|
+
|
62
|
+
expect(sort_column.order).to eq('InvoiceDate desc')
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,174 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sortable-table
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jay Mitchell
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-08-11 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 3.0.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 3.0.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: actionpack
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 3.0.0
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 3.0.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.5'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.5'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec-rails
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: tzinfo-data
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - '>='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rails
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - '>='
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - '>='
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
description: Sort HTML table columns in Rails 4 applications.
|
112
|
+
email:
|
113
|
+
- jaybmitchell@gmail.com
|
114
|
+
executables: []
|
115
|
+
extensions: []
|
116
|
+
extra_rdoc_files: []
|
117
|
+
files:
|
118
|
+
- .gitignore
|
119
|
+
- .rspec
|
120
|
+
- Gemfile
|
121
|
+
- LICENSE
|
122
|
+
- README.md
|
123
|
+
- Rakefile
|
124
|
+
- lib/sortable-table.rb
|
125
|
+
- lib/sortable_table/helpers/action_view_extension.rb
|
126
|
+
- lib/sortable_table/models/sort_column.rb
|
127
|
+
- lib/sortable_table/models/sort_column_custom_definition.rb
|
128
|
+
- lib/sortable_table/models/sort_column_definition.rb
|
129
|
+
- lib/sortable_table/models/sort_table.rb
|
130
|
+
- lib/sortable_table/railtie.rb
|
131
|
+
- lib/sortable_table/version.rb
|
132
|
+
- sortable-table.gemspec
|
133
|
+
- spec/fake_app/log/development.log
|
134
|
+
- spec/fake_app/rails_app.rb
|
135
|
+
- spec/helpers/action_view_extension_spec.rb
|
136
|
+
- spec/models/sort_column_custom_definition_spec.rb
|
137
|
+
- spec/models/sort_column_definition_spec.rb
|
138
|
+
- spec/models/sort_column_spec.rb
|
139
|
+
- spec/models/sort_table_spec.rb
|
140
|
+
- spec/spec_helper.rb
|
141
|
+
homepage: https://github.com/caselle/sortable-table
|
142
|
+
licenses:
|
143
|
+
- MIT
|
144
|
+
metadata: {}
|
145
|
+
post_install_message:
|
146
|
+
rdoc_options: []
|
147
|
+
require_paths:
|
148
|
+
- lib
|
149
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
150
|
+
requirements:
|
151
|
+
- - '>='
|
152
|
+
- !ruby/object:Gem::Version
|
153
|
+
version: '0'
|
154
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
155
|
+
requirements:
|
156
|
+
- - '>='
|
157
|
+
- !ruby/object:Gem::Version
|
158
|
+
version: '0'
|
159
|
+
requirements: []
|
160
|
+
rubyforge_project:
|
161
|
+
rubygems_version: 2.0.14
|
162
|
+
signing_key:
|
163
|
+
specification_version: 4
|
164
|
+
summary: Sort HTML table columns
|
165
|
+
test_files:
|
166
|
+
- spec/fake_app/log/development.log
|
167
|
+
- spec/fake_app/rails_app.rb
|
168
|
+
- spec/helpers/action_view_extension_spec.rb
|
169
|
+
- spec/models/sort_column_custom_definition_spec.rb
|
170
|
+
- spec/models/sort_column_definition_spec.rb
|
171
|
+
- spec/models/sort_column_spec.rb
|
172
|
+
- spec/models/sort_table_spec.rb
|
173
|
+
- spec/spec_helper.rb
|
174
|
+
has_rdoc:
|