sort_n_params 1.0.4 → 1.2.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +4 -2
- data/Rakefile +5 -3
- data/lib/sort_n_params/concerns/scopes.rb +12 -1
- data/lib/sort_n_params/sort_helpers.rb +29 -1
- data/lib/sort_n_params/sortable.rb +51 -7
- data/lib/sort_n_params.rb +11 -1
- data/sort_n_params.gemspec +35 -31
- data/spec/concerns/scopes_spec.rb +19 -13
- data/spec/sortable_spec.rb +6 -3
- data/spec/spec_helper.rb +2 -1
- metadata +50 -9
- data/.travis.yml +0 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5546d17da19b3cfb15a26cb4cd14fc684545af3f743ea175d28947b0511f9758
|
4
|
+
data.tar.gz: 449f77325f1afb0e71ba03276bb07d095e9d00348882d50c69788e4c4902685d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 350e519cb57c72ba5b7541f8802bba8436a57905b8adb2c293c09d1892c50e23c646a5280f1469a0c5b56e34929bc9c745d5c08a348224c95ff588d526691afc
|
7
|
+
data.tar.gz: 0a314e2fcdc8998dfe14fa42ceead8114474f5dccf101112bc7243d265ba56d3f22dd2f8b6e6f00d0111fa90b8d3281e3eeb8e6dd7d653abec71f9976d596ea3
|
data/Gemfile
CHANGED
@@ -1,6 +1,8 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
3
|
+
source 'https://rubygems.org'
|
4
|
+
|
5
|
+
git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
|
4
6
|
|
5
7
|
# Specify your gem's dependencies in invoice_client.gemspec
|
6
8
|
gemspec
|
data/Rakefile
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'active_support/concern'
|
2
4
|
|
3
5
|
module SortNParams
|
@@ -14,9 +16,18 @@ module SortNParams
|
|
14
16
|
table = table_name || name.tableize
|
15
17
|
|
16
18
|
ordering_array.each_slice(2).map do |order, direction|
|
17
|
-
|
19
|
+
table_custom, order_custom = get_field_order(table, order)
|
20
|
+
"#{table_custom}.#{order_custom} #{direction}"
|
18
21
|
end.join(', ')
|
19
22
|
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def get_field_order(table, order)
|
27
|
+
return [table, order] if order.split('.').one?
|
28
|
+
|
29
|
+
order.split('.')
|
30
|
+
end
|
20
31
|
end
|
21
32
|
end
|
22
33
|
end
|
@@ -1,8 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module SortNParams
|
2
4
|
module SortHelpers
|
3
5
|
def sortable(column, title = nil)
|
4
6
|
data = Sortable.new(column, title, params).call
|
5
|
-
|
7
|
+
capture do
|
8
|
+
concat(
|
9
|
+
link_to(data.title, data.sort_params, class: data.css)
|
10
|
+
)
|
11
|
+
|
12
|
+
concat_position(data) if data.position.present?
|
13
|
+
|
14
|
+
concat_clear_params(data) if data.clear_params.present?
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def concat_position(data)
|
21
|
+
concat(
|
22
|
+
link_to(data.sort_params, class: SortNParams.badge_main_class) do
|
23
|
+
"<b>#{data.position}</b><i class='#{data.icon}'></i>".html_safe
|
24
|
+
end
|
25
|
+
)
|
26
|
+
end
|
27
|
+
|
28
|
+
def concat_clear_params(data)
|
29
|
+
concat(
|
30
|
+
link_to(data.clear_params, class: SortNParams.badge_secondary_class) do
|
31
|
+
"<i class='#{SortNParams.sort_clear_class}'></i>".html_safe
|
32
|
+
end
|
33
|
+
)
|
6
34
|
end
|
7
35
|
end
|
8
36
|
end
|
@@ -1,5 +1,10 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'ostruct'
|
4
|
+
require 'sort_n_params'
|
5
|
+
|
1
6
|
class Sortable
|
2
|
-
DEFAULT_ORDER = 'asc'
|
7
|
+
DEFAULT_ORDER = 'asc'
|
3
8
|
|
4
9
|
def initialize(column, title, params)
|
5
10
|
@column = column
|
@@ -10,7 +15,15 @@ class Sortable
|
|
10
15
|
|
11
16
|
def call
|
12
17
|
add_previous_order unless @params[:order].nil?
|
13
|
-
@sort_params[:order].include?(@column)
|
18
|
+
if @sort_params[:order].include?(@column)
|
19
|
+
revert_order
|
20
|
+
set_clear_params
|
21
|
+
set_position
|
22
|
+
else
|
23
|
+
add_order
|
24
|
+
reset_clear_params
|
25
|
+
end
|
26
|
+
|
14
27
|
build_data
|
15
28
|
end
|
16
29
|
|
@@ -19,16 +32,22 @@ class Sortable
|
|
19
32
|
def add_previous_order
|
20
33
|
@sort_params[:order] << @params[:order]
|
21
34
|
@sort_params[:order].flatten!
|
35
|
+
@clear_params = Marshal.load(Marshal.dump(@sort_params))
|
22
36
|
end
|
23
37
|
|
24
38
|
def build_data
|
25
|
-
OpenStruct.new(css: set_css, icon: set_icon, title: @title, sort_params: @sort_params
|
39
|
+
OpenStruct.new(css: set_css, icon: set_icon, title: @title, position: @column_position, sort_params: @sort_params,
|
40
|
+
clear_params: @clear_params)
|
26
41
|
end
|
27
42
|
|
28
43
|
def revert_order
|
29
44
|
column_index = @sort_params[:order].find_index(@column)
|
30
|
-
direction = @column == @sort_params[:order].detect { |e| e == @column } &&
|
31
|
-
|
45
|
+
direction = if @column == @sort_params[:order].detect { |e| e == @column } &&
|
46
|
+
@sort_params[:order][column_index + 1] == 'asc'
|
47
|
+
'desc'
|
48
|
+
else
|
49
|
+
'asc'
|
50
|
+
end
|
32
51
|
@sort_params[:order][column_index + 1] = direction
|
33
52
|
end
|
34
53
|
|
@@ -37,6 +56,19 @@ class Sortable
|
|
37
56
|
@sort_params[:order] << DEFAULT_ORDER
|
38
57
|
end
|
39
58
|
|
59
|
+
def set_clear_params
|
60
|
+
column_index = @clear_params[:order].find_index(@column)
|
61
|
+
2.times { |_x| @clear_params[:order].delete_at(column_index) }
|
62
|
+
end
|
63
|
+
|
64
|
+
def reset_clear_params
|
65
|
+
@clear_params = nil
|
66
|
+
end
|
67
|
+
|
68
|
+
def set_position
|
69
|
+
@column_position = @sort_params[:order].each_slice(2).map(&:first).find_index(@column) + 1
|
70
|
+
end
|
71
|
+
|
40
72
|
def set_css
|
41
73
|
return nil unless @params[:order]
|
42
74
|
|
@@ -44,8 +76,20 @@ class Sortable
|
|
44
76
|
end
|
45
77
|
|
46
78
|
def set_icon
|
47
|
-
|
48
|
-
|
79
|
+
return unless order_param_exists?
|
80
|
+
|
81
|
+
if @column == order_column && @params[:order][ @params[:order].find_index(@column) + 1 ] == 'asc'
|
82
|
+
SortNParams.sort_asc_class
|
83
|
+
else
|
84
|
+
SortNParams.sort_desc_class
|
49
85
|
end
|
50
86
|
end
|
87
|
+
|
88
|
+
def order_param_exists?
|
89
|
+
@params[:order] && @column == order_column && @params[:order][ @params[:order].find_index(@column) + 1 ]
|
90
|
+
end
|
91
|
+
|
92
|
+
def order_column
|
93
|
+
@params[:order].detect { |e| e == @column }
|
94
|
+
end
|
51
95
|
end
|
data/lib/sort_n_params.rb
CHANGED
@@ -1,14 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'sort_n_params/sort_helpers'
|
2
4
|
require 'sort_n_params/sortable'
|
3
5
|
require 'rails/engine'
|
4
6
|
|
5
7
|
module SortNParams
|
8
|
+
class << self
|
9
|
+
attr_accessor :badge_main_class,
|
10
|
+
:badge_secondary_class,
|
11
|
+
:sort_asc_class,
|
12
|
+
:sort_desc_class,
|
13
|
+
:sort_clear_class
|
14
|
+
end
|
15
|
+
|
6
16
|
class Engine < ::Rails::Engine
|
7
17
|
require 'sort_n_params/concerns/scopes'
|
8
18
|
|
9
19
|
initializer 'sort_n_params.initialize' do |_app|
|
10
20
|
ActiveSupport.on_load :action_view do
|
11
|
-
ActionView::Base.
|
21
|
+
ActionView::Base.include SortNParams::SortHelpers
|
12
22
|
end
|
13
23
|
end
|
14
24
|
end
|
data/sort_n_params.gemspec
CHANGED
@@ -1,38 +1,42 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
Gem::Specification.new do |s|
|
2
4
|
s.name = 'sort_n_params'
|
3
|
-
s.version = '1.
|
4
|
-
s.date = '
|
5
|
-
s.summary =
|
6
|
-
s.description =
|
7
|
-
s.authors = [
|
5
|
+
s.version = '1.2.3'
|
6
|
+
s.date = '2022-06-22'
|
7
|
+
s.summary = 'Sort N Params'
|
8
|
+
s.description = 'Make your html <tables> sortables with N <th> parameters this gem.'
|
9
|
+
s.authors = ['Juan Francisco Ferrari']
|
8
10
|
s.email = 'juannferrari@gmail.com'
|
9
|
-
s.require_paths = [
|
10
|
-
s.files
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
s.homepage =
|
11
|
+
s.require_paths = ['lib']
|
12
|
+
s.files = [
|
13
|
+
'Gemfile',
|
14
|
+
'LICENSE',
|
15
|
+
'Rakefile',
|
16
|
+
'sort_n_params.gemspec',
|
17
|
+
'lib/sort_n_params.rb',
|
18
|
+
'lib/sort_n_params/sortable.rb',
|
19
|
+
'lib/sort_n_params/sort_helpers.rb',
|
20
|
+
'lib/sort_n_params/concerns/scopes.rb',
|
21
|
+
'spec/concerns/scopes_spec.rb',
|
22
|
+
'spec/spec_helper.rb',
|
23
|
+
'spec/sortable_spec.rb'
|
24
|
+
]
|
25
|
+
s.homepage =
|
25
26
|
'https://github.com/JuannFerrari/sort_n_params'
|
26
|
-
s.license
|
27
|
-
|
27
|
+
s.license = 'MIT'
|
28
|
+
s.required_ruby_version = Gem::Requirement.new('>= 2.6.0')
|
28
29
|
|
29
|
-
s.add_development_dependency
|
30
|
-
s.add_development_dependency
|
31
|
-
s.add_development_dependency
|
32
|
-
s.add_development_dependency
|
33
|
-
s.add_development_dependency
|
30
|
+
s.add_development_dependency 'bundler'
|
31
|
+
s.add_development_dependency 'factory_bot', '~> 4.0'
|
32
|
+
s.add_development_dependency 'pry', '~> 0.13.1'
|
33
|
+
s.add_development_dependency 'pry-byebug'
|
34
|
+
s.add_development_dependency 'rake', '>= 12.3.3'
|
35
|
+
s.add_development_dependency 'rspec', '~> 3.0'
|
36
|
+
s.add_development_dependency 'rubocop', '~> 1.30'
|
37
|
+
s.add_development_dependency 'rubocop-rails'
|
34
38
|
|
35
|
-
s.add_dependency(
|
36
|
-
s.add_dependency(
|
37
|
-
s.add_dependency(
|
39
|
+
s.add_dependency('nokogiri', ['>= 0'])
|
40
|
+
s.add_dependency('rails', ['>= 7.0'])
|
41
|
+
s.add_dependency('rake', ['>= 12.3.3'])
|
38
42
|
end
|
@@ -1,25 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'sort_n_params/concerns/scopes'
|
2
4
|
|
5
|
+
CustomTable = Struct.new(:custom_field) do
|
6
|
+
include SortNParams::Scopes
|
7
|
+
end
|
8
|
+
|
3
9
|
RSpec.describe SortNParams::Scopes do
|
4
10
|
let(:test_class) do
|
5
|
-
CustomTable
|
6
|
-
include SortNParams::Scopes
|
7
|
-
end
|
11
|
+
CustomTable
|
8
12
|
end
|
9
13
|
|
10
|
-
|
11
|
-
|
14
|
+
describe '#parse_array' do
|
15
|
+
context 'when you pass only an ordering array' do
|
16
|
+
subject { test_class.parse_array(%w[test_field asc]) }
|
12
17
|
|
13
|
-
|
14
|
-
|
18
|
+
it 'builds the array correctly without the table_name param passed' do
|
19
|
+
expect(subject).to eq('custom_tables.test_field asc')
|
20
|
+
end
|
15
21
|
end
|
16
|
-
end
|
17
22
|
|
18
|
-
|
19
|
-
|
23
|
+
context 'when you pass both ordering array and table name' do
|
24
|
+
subject { test_class.parse_array(%w[test_field asc], 'another_table_name') }
|
20
25
|
|
21
|
-
|
22
|
-
|
26
|
+
it 'builds the array correctly with the table_name param passed' do
|
27
|
+
expect(subject).to eq('another_table_name.test_field asc')
|
28
|
+
end
|
23
29
|
end
|
24
30
|
end
|
25
|
-
end
|
31
|
+
end
|
data/spec/sortable_spec.rb
CHANGED
@@ -1,4 +1,7 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'sort_n_params'
|
4
|
+
|
2
5
|
RSpec.describe Sortable do
|
3
6
|
subject(:data) { Sortable.new('id', 'code', {}).call }
|
4
7
|
|
@@ -54,7 +57,7 @@ RSpec.describe Sortable do
|
|
54
57
|
end
|
55
58
|
|
56
59
|
it 'returns fa-sort-up class for icon' do
|
57
|
-
expect(already_ordered.icon).to eq
|
60
|
+
expect(already_ordered.icon).to eq SortNParams.sort_asc_class
|
58
61
|
end
|
59
62
|
|
60
63
|
it 'returns title of passed param' do
|
@@ -87,7 +90,7 @@ RSpec.describe Sortable do
|
|
87
90
|
end
|
88
91
|
|
89
92
|
it 'returns fa-sort-down class for icon' do
|
90
|
-
expect(twice_ordered.icon).to eq
|
93
|
+
expect(twice_ordered.icon).to eq SortNParams.sort_desc_class
|
91
94
|
end
|
92
95
|
end
|
93
96
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sort_n_params
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Juan Francisco Ferrari
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-06-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -38,6 +38,20 @@ dependencies:
|
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '4.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: pry
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 0.13.1
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 0.13.1
|
41
55
|
- !ruby/object:Gem::Dependency
|
42
56
|
name: pry-byebug
|
43
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -81,19 +95,33 @@ dependencies:
|
|
81
95
|
- !ruby/object:Gem::Version
|
82
96
|
version: '3.0'
|
83
97
|
- !ruby/object:Gem::Dependency
|
84
|
-
name:
|
98
|
+
name: rubocop
|
85
99
|
requirement: !ruby/object:Gem::Requirement
|
86
100
|
requirements:
|
87
101
|
- - "~>"
|
88
102
|
- !ruby/object:Gem::Version
|
89
|
-
version: '
|
90
|
-
type: :
|
103
|
+
version: '1.30'
|
104
|
+
type: :development
|
91
105
|
prerelease: false
|
92
106
|
version_requirements: !ruby/object:Gem::Requirement
|
93
107
|
requirements:
|
94
108
|
- - "~>"
|
95
109
|
- !ruby/object:Gem::Version
|
96
|
-
version: '
|
110
|
+
version: '1.30'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: rubocop-rails
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
97
125
|
- !ruby/object:Gem::Dependency
|
98
126
|
name: nokogiri
|
99
127
|
requirement: !ruby/object:Gem::Requirement
|
@@ -108,6 +136,20 @@ dependencies:
|
|
108
136
|
- - ">="
|
109
137
|
- !ruby/object:Gem::Version
|
110
138
|
version: '0'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: rails
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '7.0'
|
146
|
+
type: :runtime
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - ">="
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '7.0'
|
111
153
|
- !ruby/object:Gem::Dependency
|
112
154
|
name: rake
|
113
155
|
requirement: !ruby/object:Gem::Requirement
|
@@ -128,7 +170,6 @@ executables: []
|
|
128
170
|
extensions: []
|
129
171
|
extra_rdoc_files: []
|
130
172
|
files:
|
131
|
-
- ".travis.yml"
|
132
173
|
- Gemfile
|
133
174
|
- LICENSE
|
134
175
|
- Rakefile
|
@@ -152,14 +193,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
152
193
|
requirements:
|
153
194
|
- - ">="
|
154
195
|
- !ruby/object:Gem::Version
|
155
|
-
version:
|
196
|
+
version: 2.6.0
|
156
197
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
157
198
|
requirements:
|
158
199
|
- - ">="
|
159
200
|
- !ruby/object:Gem::Version
|
160
201
|
version: '0'
|
161
202
|
requirements: []
|
162
|
-
rubygems_version: 3.
|
203
|
+
rubygems_version: 3.3.7
|
163
204
|
signing_key:
|
164
205
|
specification_version: 4
|
165
206
|
summary: Sort N Params
|