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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1699472fa73140a298c054ba1cf30a75c1f08a38cfa991d2b0d738bed8b707a0
4
- data.tar.gz: fa259494066c8118ce818a88169b771631550bd8b20dbf39c3966230abc24c54
3
+ metadata.gz: 65438859688c9289931f4447f73b1939a03058d98360878cb297b62d5c7c2ae4
4
+ data.tar.gz: 6bf07b3f4bac11774805bb5e2ebc010211b8d0a5b793af7d0d93cf8a7044617f
5
5
  SHA512:
6
- metadata.gz: 4b7d0e7c550936f4f85a2312935b138490dcce4f252240465b28eac3e4c7268fa87a089607824af3165934abdb1a8e59fdb2f5412e8684c45e6b8980fc666814
7
- data.tar.gz: 8f406482183c6f110e9eb13a30a0fbbfe85052c5c235b4e0d3366d879632de367c561b06b984184cef0ca01f737aa81a1f2288d2506663beb477d5e061b69eee
6
+ metadata.gz: ba8e96b6e58098196af8d86789e9b90c43212c5e2727dcd97aa72dc31d90a9231de96de39cf29aab6ab2c50b6c12074ba633120f139a4f45302b8a7efd493bf0
7
+ data.tar.gz: '026306599ab142c755abb914c495fb57df5f01bf76a18a503c400c11c8b806186fb54d1b11028bf8ab5316151ddb740b2f22b25787f365800ccba1bbf2bf7911'
@@ -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
- "#{table}.#{order} #{direction}"
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
- 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
 
@@ -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) ? revert_order : add_order
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
@@ -1,38 +1,37 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'sort_n_params'
3
- s.version = '1.0.4'
3
+ s.version = '1.1.0'
4
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"]
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
- ".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 =
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 = 'MIT'
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.add_development_dependency "bundler"
30
- s.add_development_dependency "factory_bot", "~> 4.0"
31
- s.add_development_dependency "pry-byebug"
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
@@ -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
@@ -4,5 +4,4 @@ require 'sort_n_params/sortable'
4
4
  RSpec.configure do |config|
5
5
  config.include FactoryBot::Syntax::Methods
6
6
  FactoryBot.find_definitions
7
-
8
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.4
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