sort_n_params 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d3f307d6ddad0f5a1e8e040e906a73710961f643471c0f0bf3bb460aa31350cb
4
- data.tar.gz: 248dde93d3390424fc37c005d3abb5fc10c7f68874a256481d077043cd1fb91c
3
+ metadata.gz: 65438859688c9289931f4447f73b1939a03058d98360878cb297b62d5c7c2ae4
4
+ data.tar.gz: 6bf07b3f4bac11774805bb5e2ebc010211b8d0a5b793af7d0d93cf8a7044617f
5
5
  SHA512:
6
- metadata.gz: 1d6a9e82cda9abdcada1d1e6a00ce4fc70f0282ce4805d0a1494c79193d5143e4bb80ecab214e87695ce98440f9799aa03327d346fb7a8ee32429e2c9c64db41
7
- data.tar.gz: 8a332365f8017aadbc47a25d1e2ebf4f5e8c42018b4484dd81b9128b0a114d0fe0f71aed7c7fb43ac57bfa0ac38dd7deda3c09f7ca1d8b61f9ce317a31ad2273
6
+ metadata.gz: ba8e96b6e58098196af8d86789e9b90c43212c5e2727dcd97aa72dc31d90a9231de96de39cf29aab6ab2c50b6c12074ba633120f139a4f45302b8a7efd493bf0
7
+ data.tar.gz: '026306599ab142c755abb914c495fb57df5f01bf76a18a503c400c11c8b806186fb54d1b11028bf8ab5316151ddb740b2f22b25787f365800ccba1bbf2bf7911'
@@ -0,0 +1,9 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.6.1
4
+ before_install:
5
+ - gem update --system
6
+ - gem install bundler
7
+ script:
8
+ - bundle install
9
+ - bundle exec rake spec
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in invoice_client.gemspec
6
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,17 @@
1
+ Copyright 2019 Juan Francisco Ferrari
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
4
+ this software and associated documentation files (the "Software"), to deal in
5
+ the Software without restriction, including without limitation the rights to use,
6
+ copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
7
+ Software, and to permit persons to whom the Software is furnished to do so,
8
+ subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included
11
+ in all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
14
+ INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
15
+ PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
16
+ BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
17
+ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -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
 
@@ -0,0 +1,31 @@
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, table_name = nil)
8
+ return all if ordering_array.blank?
9
+
10
+ all.order(parse_array(ordering_array, table_name))
11
+ end
12
+
13
+ def parse_array(ordering_array, table_name = nil)
14
+ table = table_name || name.tableize
15
+
16
+ ordering_array.each_slice(2).map do |order, direction|
17
+ table_custom, order_custom = get_field_order(table, order)
18
+ "#{table_custom}.#{order_custom} #{direction}"
19
+ end.join(', ')
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
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,22 @@
1
+ module SortNParams
2
+ module SortHelpers
3
+ def sortable(column, title = nil)
4
+ data = Sortable.new(column, title, params).call
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
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,76 @@
1
+ require 'ostruct'
2
+ require 'sort_n_params'
3
+
4
+ class Sortable
5
+ DEFAULT_ORDER = 'asc'.freeze
6
+
7
+ def initialize(column, title, params)
8
+ @column = column
9
+ @title = title || column.titleize
10
+ @params = params
11
+ @sort_params = { order: [], filter: @params[:filter] }
12
+ end
13
+
14
+ def call
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
+
25
+ build_data
26
+ end
27
+
28
+ private
29
+
30
+ def add_previous_order
31
+ @sort_params[:order] << @params[:order]
32
+ @sort_params[:order].flatten!
33
+ @clear_params = Marshal.load(Marshal.dump(@sort_params))
34
+ end
35
+
36
+ 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)
38
+ end
39
+
40
+ def revert_order
41
+ 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'
44
+ @sort_params[:order][column_index + 1] = direction
45
+ end
46
+
47
+ def add_order
48
+ @sort_params[:order] << @column
49
+ @sort_params[:order] << DEFAULT_ORDER
50
+ end
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
+
65
+ def set_css
66
+ return nil unless @params[:order]
67
+
68
+ @column == @params[:order].detect { |e| e == @column } ? 'current' : nil
69
+ end
70
+
71
+ 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
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,37 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'sort_n_params'
3
+ s.version = '1.1.0'
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']
8
+ s.email = 'juannferrari@gmail.com'
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
+ 'https://github.com/JuannFerrari/sort_n_params'
26
+ s.license = 'MIT'
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'
33
+
34
+ s.add_dependency('rails'.freeze, ['>= 5.0'])
35
+ s.add_dependency('nokogiri'.freeze, ['>= 0'])
36
+ s.add_dependency('rake'.freeze, ['>= 12.3.3'])
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
@@ -0,0 +1,95 @@
1
+ require 'sort_n_params'
2
+
3
+ RSpec.describe Sortable do
4
+ subject(:data) { Sortable.new('id', 'code', {}).call }
5
+
6
+ context 'when builds data for first time' do
7
+ it 'returns nil class for css' do
8
+ expect(data.css).to be_nil
9
+ end
10
+
11
+ it 'returns nil class for icon' do
12
+ expect(data.icon).to be_nil
13
+ end
14
+
15
+ it 'returns title of passed param' do
16
+ expect(data.title).to eq 'code'
17
+ end
18
+
19
+ it 'returns hash with default order for passed param' do
20
+ expect(data.sort_params).to be_instance_of Hash
21
+ expect(data.sort_params[:order]).to eq %w[id asc]
22
+ end
23
+ end
24
+
25
+ it 'returns nil filter params' do
26
+ expect(data.sort_params[:filter]).to be_nil
27
+ end
28
+
29
+ it 'titleizes column name if title is nil' do
30
+ new_data = Sortable.new('id', nil, {}).call
31
+ expect(new_data.title).to eq 'Id'
32
+ end
33
+
34
+ context 'when order data for first time' do
35
+ subject(:new_data) do
36
+ Sortable.new('description',
37
+ 'description',
38
+ order: data.sort_params[:order],
39
+ filter: nil).call
40
+ end
41
+ subject(:already_ordered) do
42
+ Sortable.new('description',
43
+ 'description',
44
+ order: new_data.sort_params[:order],
45
+ filter: nil).call
46
+ end
47
+
48
+ it 'adds order' do
49
+ expect(already_ordered.sort_params).to be_instance_of Hash
50
+ expect(new_data.sort_params[:order]).to eq %w[id asc description asc]
51
+ end
52
+
53
+ it "returns 'current' class for css" do
54
+ expect(already_ordered.css).to eq 'current'
55
+ end
56
+
57
+ it 'returns fa-sort-up class for icon' do
58
+ expect(already_ordered.icon).to eq SortNParams.sort_asc_class
59
+ end
60
+
61
+ it 'returns title of passed param' do
62
+ expect(already_ordered.title).to eq 'description'
63
+ end
64
+
65
+ context 'when order already exists' do
66
+ subject(:twice_ordered) do
67
+ Sortable.new('description',
68
+ 'description',
69
+ order: already_ordered.sort_params[:order],
70
+ filter: nil).call
71
+ end
72
+
73
+ it 'reverts order for first time' do
74
+ expect(already_ordered.sort_params[:order]).to eq %w[id asc description desc]
75
+ end
76
+
77
+ it 'reverts order for second time' do
78
+ expect(twice_ordered.sort_params[:order]).to eq %w[id asc description asc]
79
+ end
80
+
81
+ it 'adds new order' do
82
+ new_order = Sortable.new('name',
83
+ 'name',
84
+ order: already_ordered.sort_params[:order],
85
+ filter: nil).call
86
+ expect(already_ordered.sort_params).to be_instance_of Hash
87
+ expect(new_order.sort_params[:order]).to eq %w[id asc description desc name asc]
88
+ end
89
+
90
+ it 'returns fa-sort-down class for icon' do
91
+ expect(twice_ordered.icon).to eq SortNParams.sort_desc_class
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,7 @@
1
+ require 'factory_bot'
2
+ require 'sort_n_params/sortable'
3
+
4
+ RSpec.configure do |config|
5
+ config.include FactoryBot::Syntax::Methods
6
+ FactoryBot.find_definitions
7
+ end
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.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Juan Francisco Ferrari
@@ -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,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
@@ -112,24 +112,35 @@ 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: []
128
128
  extensions: []
129
129
  extra_rdoc_files: []
130
130
  files:
131
+ - ".travis.yml"
132
+ - Gemfile
133
+ - LICENSE
134
+ - Rakefile
131
135
  - lib/sort_n_params.rb
132
- homepage: https://rubygems.org/gems/sort_n_params
136
+ - lib/sort_n_params/concerns/scopes.rb
137
+ - lib/sort_n_params/sort_helpers.rb
138
+ - lib/sort_n_params/sortable.rb
139
+ - sort_n_params.gemspec
140
+ - spec/concerns/scopes_spec.rb
141
+ - spec/sortable_spec.rb
142
+ - spec/spec_helper.rb
143
+ homepage: https://github.com/JuannFerrari/sort_n_params
133
144
  licenses:
134
145
  - MIT
135
146
  metadata: {}
@@ -148,7 +159,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
148
159
  - !ruby/object:Gem::Version
149
160
  version: '0'
150
161
  requirements: []
151
- rubygems_version: 3.0.1
162
+ rubygems_version: 3.0.3
152
163
  signing_key:
153
164
  specification_version: 4
154
165
  summary: Sort N Params