sort_n_params 1.0.4 → 1.1.0
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.rb +8 -0
- data/lib/sort_n_params/concerns/scopes.rb +10 -1
- data/lib/sort_n_params/sort_helpers.rb +15 -1
- data/lib/sort_n_params/sortable.rb +28 -3
- data/sort_n_params.gemspec +29 -30
- data/spec/sortable_spec.rb +4 -3
- data/spec/spec_helper.rb +0 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 65438859688c9289931f4447f73b1939a03058d98360878cb297b62d5c7c2ae4
|
4
|
+
data.tar.gz: 6bf07b3f4bac11774805bb5e2ebc010211b8d0a5b793af7d0d93cf8a7044617f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ba8e96b6e58098196af8d86789e9b90c43212c5e2727dcd97aa72dc31d90a9231de96de39cf29aab6ab2c50b6c12074ba633120f139a4f45302b8a7efd493bf0
|
7
|
+
data.tar.gz: '026306599ab142c755abb914c495fb57df5f01bf76a18a503c400c11c8b806186fb54d1b11028bf8ab5316151ddb740b2f22b25787f365800ccba1bbf2bf7911'
|
data/lib/sort_n_params.rb
CHANGED
@@ -3,6 +3,14 @@ require 'sort_n_params/sortable'
|
|
3
3
|
require 'rails/engine'
|
4
4
|
|
5
5
|
module SortNParams
|
6
|
+
class << self
|
7
|
+
attr_accessor :badge_main_class,
|
8
|
+
:badge_secondary_class,
|
9
|
+
:sort_asc_class,
|
10
|
+
:sort_desc_class,
|
11
|
+
:sort_clear_class
|
12
|
+
end
|
13
|
+
|
6
14
|
class Engine < ::Rails::Engine
|
7
15
|
require 'sort_n_params/concerns/scopes'
|
8
16
|
|
@@ -14,9 +14,18 @@ module SortNParams
|
|
14
14
|
table = table_name || name.tableize
|
15
15
|
|
16
16
|
ordering_array.each_slice(2).map do |order, direction|
|
17
|
-
|
17
|
+
table_custom, order_custom = get_field_order(table, order)
|
18
|
+
"#{table_custom}.#{order_custom} #{direction}"
|
18
19
|
end.join(', ')
|
19
20
|
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def get_field_order(table, order)
|
25
|
+
return [table, order] if order.split('.').one?
|
26
|
+
|
27
|
+
order.split('.')
|
28
|
+
end
|
20
29
|
end
|
21
30
|
end
|
22
31
|
end
|
@@ -2,7 +2,21 @@ module SortNParams
|
|
2
2
|
module SortHelpers
|
3
3
|
def sortable(column, title = nil)
|
4
4
|
data = Sortable.new(column, title, params).call
|
5
|
-
|
5
|
+
capture do
|
6
|
+
concat(
|
7
|
+
link_to(data.title, data.sort_params, class: data.css)
|
8
|
+
)
|
9
|
+
concat(
|
10
|
+
link_to(data.sort_params, class: SortNParams.badge_main_class) do
|
11
|
+
"<b>#{data.position}</b><i class='#{data.icon}'></i>".html_safe
|
12
|
+
end
|
13
|
+
) if data.position.present?
|
14
|
+
concat(
|
15
|
+
link_to(data.clear_params, class: SortNParams.badge_secondary_class) do
|
16
|
+
"<i class='#{SortNParams.sort_clear_class}'></i>".html_safe
|
17
|
+
end
|
18
|
+
) if data.clear_params.present?
|
19
|
+
end
|
6
20
|
end
|
7
21
|
end
|
8
22
|
end
|
@@ -1,3 +1,6 @@
|
|
1
|
+
require 'ostruct'
|
2
|
+
require 'sort_n_params'
|
3
|
+
|
1
4
|
class Sortable
|
2
5
|
DEFAULT_ORDER = 'asc'.freeze
|
3
6
|
|
@@ -10,7 +13,15 @@ class Sortable
|
|
10
13
|
|
11
14
|
def call
|
12
15
|
add_previous_order unless @params[:order].nil?
|
13
|
-
@sort_params[:order].include?(@column)
|
16
|
+
if @sort_params[:order].include?(@column)
|
17
|
+
revert_order
|
18
|
+
set_clear_params
|
19
|
+
set_position
|
20
|
+
else
|
21
|
+
add_order
|
22
|
+
reset_clear_params
|
23
|
+
end
|
24
|
+
|
14
25
|
build_data
|
15
26
|
end
|
16
27
|
|
@@ -19,10 +30,11 @@ class Sortable
|
|
19
30
|
def add_previous_order
|
20
31
|
@sort_params[:order] << @params[:order]
|
21
32
|
@sort_params[:order].flatten!
|
33
|
+
@clear_params = Marshal.load(Marshal.dump(@sort_params))
|
22
34
|
end
|
23
35
|
|
24
36
|
def build_data
|
25
|
-
OpenStruct.new(css: set_css, icon: set_icon, title: @title, sort_params: @sort_params)
|
37
|
+
OpenStruct.new(css: set_css, icon: set_icon, title: @title, position: @column_position, sort_params: @sort_params, clear_params: @clear_params)
|
26
38
|
end
|
27
39
|
|
28
40
|
def revert_order
|
@@ -37,6 +49,19 @@ class Sortable
|
|
37
49
|
@sort_params[:order] << DEFAULT_ORDER
|
38
50
|
end
|
39
51
|
|
52
|
+
def set_clear_params
|
53
|
+
column_index = @clear_params[:order].find_index(@column)
|
54
|
+
2.times{|x| @clear_params[:order].delete_at(column_index) }
|
55
|
+
end
|
56
|
+
|
57
|
+
def reset_clear_params
|
58
|
+
@clear_params = nil
|
59
|
+
end
|
60
|
+
|
61
|
+
def set_position
|
62
|
+
@column_position = @sort_params[:order].each_slice(2).map(&:first).find_index(@column) + 1
|
63
|
+
end
|
64
|
+
|
40
65
|
def set_css
|
41
66
|
return nil unless @params[:order]
|
42
67
|
|
@@ -45,7 +70,7 @@ class Sortable
|
|
45
70
|
|
46
71
|
def set_icon
|
47
72
|
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' ?
|
73
|
+
@column == @params[:order].detect { |e| e == @column } && @params[:order][ @params[:order].find_index(@column) + 1 ] == 'asc' ? SortNParams.sort_asc_class : SortNParams.sort_desc_class
|
49
74
|
end
|
50
75
|
end
|
51
76
|
end
|
data/sort_n_params.gemspec
CHANGED
@@ -1,38 +1,37 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = 'sort_n_params'
|
3
|
-
s.version = '1.0
|
3
|
+
s.version = '1.1.0'
|
4
4
|
s.date = '2019-05-07'
|
5
|
-
s.summary =
|
6
|
-
s.description =
|
7
|
-
s.authors = [
|
5
|
+
s.summary = 'Sort N Params'
|
6
|
+
s.description = 'Make your html <tables> sortables with N <th> parameters this gem.'
|
7
|
+
s.authors = ['Juan Francisco Ferrari']
|
8
8
|
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
|
9
|
+
s.require_paths = ['lib']
|
10
|
+
s.files = [
|
11
|
+
'.travis.yml',
|
12
|
+
'Gemfile',
|
13
|
+
'LICENSE',
|
14
|
+
'Rakefile',
|
15
|
+
'sort_n_params.gemspec',
|
16
|
+
'lib/sort_n_params.rb',
|
17
|
+
'lib/sort_n_params/sortable.rb',
|
18
|
+
'lib/sort_n_params/sort_helpers.rb',
|
19
|
+
'lib/sort_n_params/concerns/scopes.rb',
|
20
|
+
'spec/concerns/scopes_spec.rb',
|
21
|
+
'spec/spec_helper.rb',
|
22
|
+
'spec/sortable_spec.rb'
|
23
|
+
]
|
24
|
+
s.homepage =
|
25
25
|
'https://github.com/JuannFerrari/sort_n_params'
|
26
|
-
s.license
|
26
|
+
s.license = 'MIT'
|
27
27
|
|
28
|
+
s.add_development_dependency 'bundler'
|
29
|
+
s.add_development_dependency 'factory_bot', '~> 4.0'
|
30
|
+
s.add_development_dependency 'pry-byebug'
|
31
|
+
s.add_development_dependency 'rake', '>= 12.3.3'
|
32
|
+
s.add_development_dependency 'rspec', '~> 3.0'
|
28
33
|
|
29
|
-
s.
|
30
|
-
s.
|
31
|
-
s.
|
32
|
-
s.add_development_dependency "rake", ">= 12.3.3"
|
33
|
-
s.add_development_dependency "rspec", "~> 3.0"
|
34
|
-
|
35
|
-
s.add_dependency(%q<rails>.freeze, ["~> 5.0"])
|
36
|
-
s.add_dependency(%q<nokogiri>.freeze, [">= 0"])
|
37
|
-
s.add_dependency(%q<rake>.freeze, [">= 12.3.3"])
|
34
|
+
s.add_dependency('rails'.freeze, ['>= 5.0'])
|
35
|
+
s.add_dependency('nokogiri'.freeze, ['>= 0'])
|
36
|
+
s.add_dependency('rake'.freeze, ['>= 12.3.3'])
|
38
37
|
end
|
data/spec/sortable_spec.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
|
-
require '
|
1
|
+
require 'sort_n_params'
|
2
|
+
|
2
3
|
RSpec.describe Sortable do
|
3
4
|
subject(:data) { Sortable.new('id', 'code', {}).call }
|
4
5
|
|
@@ -54,7 +55,7 @@ RSpec.describe Sortable do
|
|
54
55
|
end
|
55
56
|
|
56
57
|
it 'returns fa-sort-up class for icon' do
|
57
|
-
expect(already_ordered.icon).to eq
|
58
|
+
expect(already_ordered.icon).to eq SortNParams.sort_asc_class
|
58
59
|
end
|
59
60
|
|
60
61
|
it 'returns title of passed param' do
|
@@ -87,7 +88,7 @@ RSpec.describe Sortable do
|
|
87
88
|
end
|
88
89
|
|
89
90
|
it 'returns fa-sort-down class for icon' do
|
90
|
-
expect(twice_ordered.icon).to eq
|
91
|
+
expect(twice_ordered.icon).to eq SortNParams.sort_desc_class
|
91
92
|
end
|
92
93
|
end
|
93
94
|
end
|
data/spec/spec_helper.rb
CHANGED
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.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Juan Francisco Ferrari
|
@@ -84,14 +84,14 @@ dependencies:
|
|
84
84
|
name: rails
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
86
86
|
requirements:
|
87
|
-
- - "
|
87
|
+
- - ">="
|
88
88
|
- !ruby/object:Gem::Version
|
89
89
|
version: '5.0'
|
90
90
|
type: :runtime
|
91
91
|
prerelease: false
|
92
92
|
version_requirements: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
|
-
- - "
|
94
|
+
- - ">="
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '5.0'
|
97
97
|
- !ruby/object:Gem::Dependency
|