sort_n_params 1.0.1 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/sort_n_params/concerns/scopes.rb +20 -0
- data/lib/sort_n_params/sort_helpers.rb +8 -0
- data/lib/sort_n_params/sortable.rb +51 -0
- data/spec/sortable_spec.rb +94 -0
- data/spec/spec_helper.rb +7 -0
- metadata +6 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bf597fdee3ef14cd303a2cc400fef9aa8a85c43ac1aaee3154f97781553db993
|
4
|
+
data.tar.gz: af249d92dd15af32406513f1f090ea51a5fdc57332a86313f5544e0c195ca1dd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4dbfd4f0db84fd233cf2e081b25939b82dfec4648a33f7e1f3eee566c782caeac9dd4e109ce89d0f87484c18dacf626abf5263d1ee102e4dd777fe6b605dff8d
|
7
|
+
data.tar.gz: 3ef307e2e4ffd4b9da726854c45b23adf25dbb9b507a9ecd6d392f7dd6fa9ebeb3295e6c99aff056b49955d47ab97d0fb64207cfe803fb5e4cff561b0fa71c8b
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
|
3
|
+
module SortNParams
|
4
|
+
module Scopes
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
class_methods do
|
7
|
+
def sorting_order(ordering_array)
|
8
|
+
return all if ordering_array.blank?
|
9
|
+
|
10
|
+
all.order(parse_array(ordering_array))
|
11
|
+
end
|
12
|
+
|
13
|
+
def parse_array(ordering_array)
|
14
|
+
ordering_array.each_slice(2).map do |order, direction|
|
15
|
+
"#{name.tableize}.#{order} #{direction}"
|
16
|
+
end.join(', ')
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
class Sortable
|
2
|
+
DEFAULT_ORDER = 'asc'.freeze
|
3
|
+
|
4
|
+
def initialize(column, title, params)
|
5
|
+
@column = column
|
6
|
+
@title = title || column.titleize
|
7
|
+
@params = params
|
8
|
+
@sort_params = { order: [], filter: @params[:filter] }
|
9
|
+
end
|
10
|
+
|
11
|
+
def call
|
12
|
+
add_previous_order if @params[:order].present?
|
13
|
+
@sort_params[:order].include?(@column) ? revert_order : add_order
|
14
|
+
build_data
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def add_previous_order
|
20
|
+
@sort_params[:order] << @params[:order]
|
21
|
+
@sort_params[:order].flatten!
|
22
|
+
end
|
23
|
+
|
24
|
+
def build_data
|
25
|
+
OpenStruct.new(css: set_css, icon: set_icon, title: @title, sort_params: @sort_params)
|
26
|
+
end
|
27
|
+
|
28
|
+
def revert_order
|
29
|
+
column_index = @sort_params[:order].find_index(@column)
|
30
|
+
direction = @column == @sort_params[:order].detect { |e| e == @column } &&
|
31
|
+
@sort_params[:order][column_index + 1] == 'asc' ? 'desc' : 'asc'
|
32
|
+
@sort_params[:order][column_index + 1] = direction
|
33
|
+
end
|
34
|
+
|
35
|
+
def add_order
|
36
|
+
@sort_params[:order] << @column
|
37
|
+
@sort_params[:order] << DEFAULT_ORDER
|
38
|
+
end
|
39
|
+
|
40
|
+
def set_css
|
41
|
+
return nil unless @params[:order]
|
42
|
+
|
43
|
+
@column == @params[:order].detect { |e| e == @column } ? 'current' : nil
|
44
|
+
end
|
45
|
+
|
46
|
+
def set_icon
|
47
|
+
if @params[:order] && @params[:order].detect { |e| e == @column } == @column && @params[:order][ @params[:order].find_index(@column) + 1 ]
|
48
|
+
@column == @params[:order].detect { |e| e == @column } && @params[:order][ @params[:order].find_index(@column) + 1 ] == 'asc' ? 'fa fa-sort-up' : 'fa fa-sort-desc'
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,94 @@
|
|
1
|
+
require 'pry'
|
2
|
+
RSpec.describe Sortable do
|
3
|
+
subject(:data) { Sortable.new('id', 'code', {}).call }
|
4
|
+
|
5
|
+
context 'when builds data for first time' do
|
6
|
+
it 'returns nil class for css' do
|
7
|
+
expect(data.css).to be_nil
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'returns nil class for icon' do
|
11
|
+
expect(data.icon).to be_nil
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'returns title of passed param' do
|
15
|
+
expect(data.title).to eq 'code'
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'returns hash with default order for passed param' do
|
19
|
+
expect(data.sort_params).to be_instance_of Hash
|
20
|
+
expect(data.sort_params[:order]).to eq %w[id asc]
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'returns nil filter params' do
|
25
|
+
expect(data.sort_params[:filter]).to be_nil
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'titleizes column name if title is nil' do
|
29
|
+
new_data = Sortable.new('id', nil, {}).call
|
30
|
+
expect(new_data.title).to eq 'Id'
|
31
|
+
end
|
32
|
+
|
33
|
+
context 'when order data for first time' do
|
34
|
+
subject(:new_data) do
|
35
|
+
Sortable.new('description',
|
36
|
+
'description',
|
37
|
+
order: data.sort_params[:order],
|
38
|
+
filter: nil).call
|
39
|
+
end
|
40
|
+
subject(:already_ordered) do
|
41
|
+
Sortable.new('description',
|
42
|
+
'description',
|
43
|
+
order: new_data.sort_params[:order],
|
44
|
+
filter: nil).call
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'adds order' do
|
48
|
+
expect(already_ordered.sort_params).to be_instance_of Hash
|
49
|
+
expect(new_data.sort_params[:order]).to eq %w[id asc description asc]
|
50
|
+
end
|
51
|
+
|
52
|
+
it "returns 'current' class for css" do
|
53
|
+
expect(already_ordered.css).to eq 'current'
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'returns fa-sort-up class for icon' do
|
57
|
+
expect(already_ordered.icon).to eq 'fa fa-sort-up'
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'returns title of passed param' do
|
61
|
+
expect(already_ordered.title).to eq 'description'
|
62
|
+
end
|
63
|
+
|
64
|
+
context 'when order already exists' do
|
65
|
+
subject(:twice_ordered) do
|
66
|
+
Sortable.new('description',
|
67
|
+
'description',
|
68
|
+
order: already_ordered.sort_params[:order],
|
69
|
+
filter: nil).call
|
70
|
+
end
|
71
|
+
|
72
|
+
it 'reverts order for first time' do
|
73
|
+
expect(already_ordered.sort_params[:order]).to eq %w[id asc description desc]
|
74
|
+
end
|
75
|
+
|
76
|
+
it 'reverts order for second time' do
|
77
|
+
expect(twice_ordered.sort_params[:order]).to eq %w[id asc description asc]
|
78
|
+
end
|
79
|
+
|
80
|
+
it 'adds new order' do
|
81
|
+
new_order = Sortable.new('name',
|
82
|
+
'name',
|
83
|
+
order: already_ordered.sort_params[:order],
|
84
|
+
filter: nil).call
|
85
|
+
expect(already_ordered.sort_params).to be_instance_of Hash
|
86
|
+
expect(new_order.sort_params[:order]).to eq %w[id asc description desc name asc]
|
87
|
+
end
|
88
|
+
|
89
|
+
it 'returns fa-sort-down class for icon' do
|
90
|
+
expect(twice_ordered.icon).to eq 'fa fa-sort-desc'
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sort_n_params
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Juan Francisco Ferrari
|
@@ -129,6 +129,11 @@ extensions: []
|
|
129
129
|
extra_rdoc_files: []
|
130
130
|
files:
|
131
131
|
- lib/sort_n_params.rb
|
132
|
+
- lib/sort_n_params/concerns/scopes.rb
|
133
|
+
- lib/sort_n_params/sort_helpers.rb
|
134
|
+
- lib/sort_n_params/sortable.rb
|
135
|
+
- spec/sortable_spec.rb
|
136
|
+
- spec/spec_helper.rb
|
132
137
|
homepage: https://github.com/JuannFerrari/sort_n_params
|
133
138
|
licenses:
|
134
139
|
- MIT
|