sort_n_params 1.2.2 → 1.2.3

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: cea1f96ef8bba32a0577ca8e86bf1325aae552427f5403a2c03d00d536a5f2b0
4
- data.tar.gz: ea209f082dc417900a8e873d6a4bddc0901e25f6958b811743f168d0ff46a779
3
+ metadata.gz: 5546d17da19b3cfb15a26cb4cd14fc684545af3f743ea175d28947b0511f9758
4
+ data.tar.gz: 449f77325f1afb0e71ba03276bb07d095e9d00348882d50c69788e4c4902685d
5
5
  SHA512:
6
- metadata.gz: 998f053ac4c19a0e14fe46064568a9260d1c3f5ac1f0dbcb2ad8db67b76284379ab709313bf85411515803a5b8d97bdff25d24dbd596bb07069a68fc3decfb2f
7
- data.tar.gz: f5c313061ea1c2c365092ba5a91239aa38d7e5ee9616104cbc6bfd9fcbbfe8d0516f74f60cc22d6a749f36b3e450910f7cbd0ab3d399c4e5d7e19942d9972ab0
6
+ metadata.gz: 350e519cb57c72ba5b7541f8802bba8436a57905b8adb2c293c09d1892c50e23c646a5280f1469a0c5b56e34929bc9c745d5c08a348224c95ff588d526691afc
7
+ data.tar.gz: 0a314e2fcdc8998dfe14fa42ceead8114474f5dccf101112bc7243d265ba56d3f22dd2f8b6e6f00d0111fa90b8d3281e3eeb8e6dd7d653abec71f9976d596ea3
data/Gemfile CHANGED
@@ -1,6 +1,8 @@
1
- source "https://rubygems.org"
1
+ # frozen_string_literal: true
2
2
 
3
- git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
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,6 +1,8 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
3
5
 
4
6
  RSpec::Core::RakeTask.new(:spec)
5
7
 
6
- task :default => :spec
8
+ task default: :spec
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'active_support/concern'
2
4
 
3
5
  module SortNParams
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SortNParams
2
4
  module SortHelpers
3
5
  def sortable(column, title = nil)
@@ -6,17 +8,29 @@ module SortNParams
6
8
  concat(
7
9
  link_to(data.title, data.sort_params, class: data.css)
8
10
  )
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?
11
+
12
+ concat_position(data) if data.position.present?
13
+
14
+ concat_clear_params(data) if data.clear_params.present?
19
15
  end
20
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
+ )
34
+ end
21
35
  end
22
36
  end
@@ -1,8 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'ostruct'
2
4
  require 'sort_n_params'
3
5
 
4
6
  class Sortable
5
- DEFAULT_ORDER = 'asc'.freeze
7
+ DEFAULT_ORDER = 'asc'
6
8
 
7
9
  def initialize(column, title, params)
8
10
  @column = column
@@ -34,13 +36,18 @@ class Sortable
34
36
  end
35
37
 
36
38
  def build_data
37
- OpenStruct.new(css: set_css, icon: set_icon, title: @title, position: @column_position, sort_params: @sort_params, clear_params: @clear_params)
39
+ OpenStruct.new(css: set_css, icon: set_icon, title: @title, position: @column_position, sort_params: @sort_params,
40
+ clear_params: @clear_params)
38
41
  end
39
42
 
40
43
  def revert_order
41
44
  column_index = @sort_params[:order].find_index(@column)
42
- direction = @column == @sort_params[:order].detect { |e| e == @column } &&
43
- @sort_params[:order][column_index + 1] == 'asc' ? 'desc' : 'asc'
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
44
51
  @sort_params[:order][column_index + 1] = direction
45
52
  end
46
53
 
@@ -51,7 +58,7 @@ class Sortable
51
58
 
52
59
  def set_clear_params
53
60
  column_index = @clear_params[:order].find_index(@column)
54
- 2.times{|x| @clear_params[:order].delete_at(column_index) }
61
+ 2.times { |_x| @clear_params[:order].delete_at(column_index) }
55
62
  end
56
63
 
57
64
  def reset_clear_params
@@ -69,8 +76,20 @@ class Sortable
69
76
  end
70
77
 
71
78
  def set_icon
72
- if @params[:order] && @params[:order].detect { |e| e == @column } == @column && @params[:order][ @params[:order].find_index(@column) + 1 ]
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
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
74
85
  end
75
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
76
95
  end
data/lib/sort_n_params.rb CHANGED
@@ -1,3 +1,5 @@
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'
@@ -16,7 +18,7 @@ module SortNParams
16
18
 
17
19
  initializer 'sort_n_params.initialize' do |_app|
18
20
  ActiveSupport.on_load :action_view do
19
- ActionView::Base.send :include, SortNParams::SortHelpers
21
+ ActionView::Base.include SortNParams::SortHelpers
20
22
  end
21
23
  end
22
24
  end
@@ -1,7 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  Gem::Specification.new do |s|
2
4
  s.name = 'sort_n_params'
3
- s.version = '1.2.2'
4
- s.date = '2022-06-17'
5
+ s.version = '1.2.3'
6
+ s.date = '2022-06-22'
5
7
  s.summary = 'Sort N Params'
6
8
  s.description = 'Make your html <tables> sortables with N <th> parameters this gem.'
7
9
  s.authors = ['Juan Francisco Ferrari']
@@ -23,15 +25,18 @@ Gem::Specification.new do |s|
23
25
  s.homepage =
24
26
  'https://github.com/JuannFerrari/sort_n_params'
25
27
  s.license = 'MIT'
26
- s.required_ruby_version = Gem::Requirement.new(">= 2.6.0")
28
+ s.required_ruby_version = Gem::Requirement.new('>= 2.6.0')
27
29
 
28
30
  s.add_development_dependency 'bundler'
29
31
  s.add_development_dependency 'factory_bot', '~> 4.0'
32
+ s.add_development_dependency 'pry', '~> 0.13.1'
30
33
  s.add_development_dependency 'pry-byebug'
31
34
  s.add_development_dependency 'rake', '>= 12.3.3'
32
35
  s.add_development_dependency 'rspec', '~> 3.0'
36
+ s.add_development_dependency 'rubocop', '~> 1.30'
37
+ s.add_development_dependency 'rubocop-rails'
33
38
 
34
- s.add_dependency('rails'.freeze, ['>= 7.0'])
35
- s.add_dependency('nokogiri'.freeze, ['>= 0'])
36
- s.add_dependency('rake'.freeze, ['>= 12.3.3'])
39
+ s.add_dependency('nokogiri', ['>= 0'])
40
+ s.add_dependency('rails', ['>= 7.0'])
41
+ s.add_dependency('rake', ['>= 12.3.3'])
37
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 = Struct.new(:custom_field) do
6
- include SortNParams::Scopes
7
- end
11
+ CustomTable
8
12
  end
9
13
 
10
- context 'when you pass only an ordering array' do
11
- subject { test_class.parse_array(["test_field","asc"]) }
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
- it "builds the array correctly without the table_name param passed" do
14
- expect(subject).to eq("custom_tables.test_field asc")
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
- context 'when you pass both ordering array and table name' do
19
- subject { test_class.parse_array(["test_field","asc"], "another_table_name" ) }
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
- it "builds the array correctly with the table_name param passed" do
22
- expect(subject).to eq("another_table_name.test_field asc")
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
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'sort_n_params'
2
4
 
3
5
  RSpec.describe Sortable do
data/spec/spec_helper.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'factory_bot'
2
4
  require 'sort_n_params/sortable'
3
5
 
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.2.2
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: 2022-06-17 00:00:00.000000000 Z
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: rails
98
+ name: rubocop
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '1.30'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '1.30'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rubocop-rails
85
113
  requirement: !ruby/object:Gem::Requirement
86
114
  requirements:
87
115
  - - ">="
88
116
  - !ruby/object:Gem::Version
89
- version: '7.0'
90
- type: :runtime
117
+ version: '0'
118
+ type: :development
91
119
  prerelease: false
92
120
  version_requirements: !ruby/object:Gem::Requirement
93
121
  requirements:
94
122
  - - ">="
95
123
  - !ruby/object:Gem::Version
96
- version: '7.0'
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