sort_n_params 1.0.3 → 1.2.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f8d7d660d21516f59a4a914359a26d53a20fdc3699793e0928957546baee823e
4
- data.tar.gz: 928b68de9c0da24d7d454f445dbfe2b2cdfcd1a55762b9fd226edf86a85ec25f
3
+ metadata.gz: cea1f96ef8bba32a0577ca8e86bf1325aae552427f5403a2c03d00d536a5f2b0
4
+ data.tar.gz: ea209f082dc417900a8e873d6a4bddc0901e25f6958b811743f168d0ff46a779
5
5
  SHA512:
6
- metadata.gz: '093b2206de1545750e2da682723fb6c725d6ca907156ccd7068edd827d87923f46ca100887d7a9f8d1f47923c1230214db6aca59a312a8da949291810637861a'
7
- data.tar.gz: 431f0adadd6e5fc19f105afa7eac5a2ed3ea7d0471363473af5ba896e2b6c35f3d3ea4b2963cdb9f72955312c36cc8df1cfe59a01437ab005db6737f2e66ff32
6
+ metadata.gz: 998f053ac4c19a0e14fe46064568a9260d1c3f5ac1f0dbcb2ad8db67b76284379ab709313bf85411515803a5b8d97bdff25d24dbd596bb07069a68fc3decfb2f
7
+ data.tar.gz: f5c313061ea1c2c365092ba5a91239aa38d7e5ee9616104cbc6bfd9fcbbfe8d0516f74f60cc22d6a749f36b3e450910f7cbd0ab3d399c4e5d7e19942d9972ab0
File without changes
@@ -4,17 +4,28 @@ module SortNParams
4
4
  module Scopes
5
5
  extend ActiveSupport::Concern
6
6
  class_methods do
7
- def sorting_order(ordering_array)
7
+ def sorting_order(ordering_array, table_name = nil)
8
8
  return all if ordering_array.blank?
9
9
 
10
- all.order(parse_array(ordering_array))
10
+ all.order(parse_array(ordering_array, table_name))
11
11
  end
12
12
 
13
- def parse_array(ordering_array)
13
+ def parse_array(ordering_array, table_name = nil)
14
+ table = table_name || name.tableize
15
+
14
16
  ordering_array.each_slice(2).map do |order, direction|
15
- "#{name.tableize}.#{order} #{direction}"
17
+ table_custom, order_custom = get_field_order(table, order)
18
+ "#{table_custom}.#{order_custom} #{direction}"
16
19
  end.join(', ')
17
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
18
29
  end
19
30
  end
20
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
- link_to "#{data.title} <i class='#{data.icon}'".html_safe, data.sort_params, class: data.css
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
 
@@ -9,8 +12,16 @@ class Sortable
9
12
  end
10
13
 
11
14
  def call
12
- add_previous_order if @params[:order].present?
13
- @sort_params[:order].include?(@column) ? revert_order : add_order
15
+ add_previous_order unless @params[:order].nil?
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' ? 'fa fa-sort-up' : 'fa fa-sort-desc'
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/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
 
@@ -1,36 +1,37 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'sort_n_params'
3
- s.version = '1.0.3'
4
- s.date = '2019-05-07'
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"]
3
+ s.version = '1.2.2'
4
+ s.date = '2022-06-17'
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 = ["lib"]
10
- s.files = [
11
- "Gemfile",
12
- "MIT-LICENSE",
13
- "Rakefile",
14
- "sort_n_params.gemspec",
15
- "lib/sort_n_params.rb",
16
- "lib/sort_n_params/sortable.rb",
17
- "lib/sort_n_params/sort_helpers.rb",
18
- "lib/sort_n_params/concerns/scopes.rb",
19
- "spec/spec_helper.rb",
20
- "spec/sortable_spec.rb"
21
- ]
22
- s.homepage =
9
+ s.require_paths = ['lib']
10
+ s.files = [
11
+ 'Gemfile',
12
+ 'LICENSE',
13
+ 'Rakefile',
14
+ 'sort_n_params.gemspec',
15
+ 'lib/sort_n_params.rb',
16
+ 'lib/sort_n_params/sortable.rb',
17
+ 'lib/sort_n_params/sort_helpers.rb',
18
+ 'lib/sort_n_params/concerns/scopes.rb',
19
+ 'spec/concerns/scopes_spec.rb',
20
+ 'spec/spec_helper.rb',
21
+ 'spec/sortable_spec.rb'
22
+ ]
23
+ s.homepage =
23
24
  'https://github.com/JuannFerrari/sort_n_params'
24
- s.license = 'MIT'
25
+ s.license = 'MIT'
26
+ s.required_ruby_version = Gem::Requirement.new(">= 2.6.0")
25
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'
26
33
 
27
- s.add_development_dependency "bundler"
28
- s.add_development_dependency "factory_bot", "~> 4.0"
29
- s.add_development_dependency "pry-byebug"
30
- s.add_development_dependency "rake", "~> 10.0"
31
- s.add_development_dependency "rspec", "~> 3.0"
32
-
33
- s.add_dependency(%q<rails>.freeze, ["~> 5.0"])
34
- s.add_dependency(%q<nokogiri>.freeze, [">= 0"])
35
- s.add_dependency(%q<rake>.freeze, ["~> 10.1"])
34
+ s.add_dependency('rails'.freeze, ['>= 7.0'])
35
+ s.add_dependency('nokogiri'.freeze, ['>= 0'])
36
+ s.add_dependency('rake'.freeze, ['>= 12.3.3'])
36
37
  end
@@ -0,0 +1,25 @@
1
+ require 'sort_n_params/concerns/scopes'
2
+
3
+ RSpec.describe SortNParams::Scopes do
4
+ let(:test_class) do
5
+ CustomTable = Struct.new(:custom_field) do
6
+ include SortNParams::Scopes
7
+ end
8
+ end
9
+
10
+ context 'when you pass only an ordering array' do
11
+ subject { test_class.parse_array(["test_field","asc"]) }
12
+
13
+ it "builds the array correctly without the table_name param passed" do
14
+ expect(subject).to eq("custom_tables.test_field asc")
15
+ end
16
+ end
17
+
18
+ context 'when you pass both ordering array and table name' do
19
+ subject { test_class.parse_array(["test_field","asc"], "another_table_name" ) }
20
+
21
+ it "builds the array correctly with the table_name param passed" do
22
+ expect(subject).to eq("another_table_name.test_field asc")
23
+ end
24
+ end
25
+ end
@@ -1,4 +1,5 @@
1
- require 'pry'
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 'fa fa-sort-up'
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 'fa fa-sort-desc'
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
@@ -1,7 +1,7 @@
1
1
  require 'factory_bot'
2
+ require 'sort_n_params/sortable'
2
3
 
3
4
  RSpec.configure do |config|
4
5
  config.include FactoryBot::Syntax::Methods
5
6
  FactoryBot.find_definitions
6
-
7
7
  end
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.0.3
4
+ version: 1.2.2
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: 2019-05-07 00:00:00.000000000 Z
11
+ date: 2022-06-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -56,16 +56,16 @@ dependencies:
56
56
  name: rake
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - "~>"
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
- version: '10.0'
61
+ version: 12.3.3
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - "~>"
66
+ - - ">="
67
67
  - !ruby/object:Gem::Version
68
- version: '10.0'
68
+ version: 12.3.3
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rspec
71
71
  requirement: !ruby/object:Gem::Requirement
@@ -84,16 +84,16 @@ dependencies:
84
84
  name: rails
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - "~>"
87
+ - - ">="
88
88
  - !ruby/object:Gem::Version
89
- version: '5.0'
89
+ version: '7.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
- version: '5.0'
96
+ version: '7.0'
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: nokogiri
99
99
  requirement: !ruby/object:Gem::Requirement
@@ -112,16 +112,16 @@ dependencies:
112
112
  name: rake
113
113
  requirement: !ruby/object:Gem::Requirement
114
114
  requirements:
115
- - - "~>"
115
+ - - ">="
116
116
  - !ruby/object:Gem::Version
117
- version: '10.1'
117
+ version: 12.3.3
118
118
  type: :runtime
119
119
  prerelease: false
120
120
  version_requirements: !ruby/object:Gem::Requirement
121
121
  requirements:
122
- - - "~>"
122
+ - - ">="
123
123
  - !ruby/object:Gem::Version
124
- version: '10.1'
124
+ version: 12.3.3
125
125
  description: Make your html <tables> sortables with N <th> parameters this gem.
126
126
  email: juannferrari@gmail.com
127
127
  executables: []
@@ -129,13 +129,14 @@ extensions: []
129
129
  extra_rdoc_files: []
130
130
  files:
131
131
  - Gemfile
132
- - MIT-LICENSE
132
+ - LICENSE
133
133
  - Rakefile
134
134
  - lib/sort_n_params.rb
135
135
  - lib/sort_n_params/concerns/scopes.rb
136
136
  - lib/sort_n_params/sort_helpers.rb
137
137
  - lib/sort_n_params/sortable.rb
138
138
  - sort_n_params.gemspec
139
+ - spec/concerns/scopes_spec.rb
139
140
  - spec/sortable_spec.rb
140
141
  - spec/spec_helper.rb
141
142
  homepage: https://github.com/JuannFerrari/sort_n_params
@@ -150,14 +151,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
150
151
  requirements:
151
152
  - - ">="
152
153
  - !ruby/object:Gem::Version
153
- version: '0'
154
+ version: 2.6.0
154
155
  required_rubygems_version: !ruby/object:Gem::Requirement
155
156
  requirements:
156
157
  - - ">="
157
158
  - !ruby/object:Gem::Version
158
159
  version: '0'
159
160
  requirements: []
160
- rubygems_version: 3.0.1
161
+ rubygems_version: 3.3.7
161
162
  signing_key:
162
163
  specification_version: 4
163
164
  summary: Sort N Params