sortable-for-rails 1.0.1 → 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: 89f9a133e5799c6aa1921f1587d24920f6c0ffd2b4f6915d2d5d0202fc09ddb4
4
- data.tar.gz: 0f98be3337e381b2577aa4e74aca84b3f2391f407b586ab3ce35808f4250ca5e
3
+ metadata.gz: 9fc07dc30d79c3ea98dd0c316b4a45ce4c46b300c0fbf3a210b03608f1be9254
4
+ data.tar.gz: fc4365bcaf59dbf9064fd885a4d9a1f657987ed853a3986205bf764ec3898120
5
5
  SHA512:
6
- metadata.gz: 0d45df6b107ff1a036f15b525a6a349d82e51928cacfa2762994607d35536748bb64f5e1f4fa0513f2df0656ef634025afe8ce2a78dc3378b22a5e6114291856
7
- data.tar.gz: f2efbcb192059a175a0bd4848b676db2d2d845345bf8ba5d3ebb623d6eed8c28ccfee152d1729619e08209aaede60fa717881545fa7d2518c9406209324d5bd0
6
+ metadata.gz: d0917c1b69bad99a640bef23faa1cf4ae9b50c594dc309178861e0e253fc8b1125154fef88bcfa4d09e956c62c90fb359bf6924cd3c7654103847f5d59fefbd5
7
+ data.tar.gz: e055b6049d8fb8243e61381395168013702a05acf6eaefacef27c3a2def06b743f2ea7adc6b1bcde5e31b88be184f6cab4a522951bc3c73acccb26f97c6b7de3
data/README.md CHANGED
@@ -27,6 +27,15 @@ class User < ApplicationRecord
27
27
 
28
28
  sortable :id, :updated_at, :email
29
29
  sortable :profile_name, -> { joins(:profile) }, column: "profiles.name"
30
+ sortable :status, method: :sortable_status
31
+
32
+ # we can sort in memory also if needed (for data not stored directly in DB)
33
+ # just remember on performance impact
34
+ def self.sortable_status(direction)
35
+ results = all.sort_by { |user| user.status }
36
+ results.reverse! if direction == :desc
37
+ results
38
+ end
30
39
 
31
40
  end
32
41
  ```
data/lib/sortable.rb CHANGED
@@ -12,28 +12,30 @@ module Sortable
12
12
  @sortable
13
13
  end
14
14
 
15
- def sorting(column, direction = :asc)
16
- Sortable::Sort.new(self, column, direction).all
15
+ def sorting(column, direction = :asc, *args)
16
+ Sortable::Sort.new(self, column, direction, *args).all
17
17
  end
18
18
  end
19
19
  end
20
20
 
21
21
  class Column
22
- attr_reader :name, :column, :scope
23
- def initialize(name, scope = nil, column: name)
22
+ attr_reader :name, :column, :scope, :method
23
+ def initialize(name, scope = nil, column: name, method: nil)
24
24
  @name = name
25
25
  @column = column
26
+ @method = method
26
27
  @scope = scope
27
28
  end
28
29
  end
29
30
 
30
31
  class Sort
31
- attr_reader :scope
32
+ attr_reader :scope, :args
32
33
 
33
- def initialize(scope, column, direction)
34
+ def initialize(scope, column, direction, *args)
34
35
  @scope = scope
35
36
  @column = column
36
37
  @direction = direction
38
+ @args = args
37
39
  end
38
40
 
39
41
  def columns
@@ -45,7 +47,12 @@ module Sortable
45
47
 
46
48
  if column
47
49
  all = all.merge(column.scope) if column.scope.present?
48
- all = all.order("#{column.column} #{direction}")
50
+
51
+ if column.method
52
+ all = all.public_send(column.method, direction.to_sym, *args)
53
+ else
54
+ all = all.order("#{column.column} #{direction}")
55
+ end
49
56
  end
50
57
 
51
58
  all
@@ -3,7 +3,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
3
 
4
4
  Gem::Specification.new do |spec|
5
5
  spec.name = "sortable-for-rails"
6
- spec.version = "1.0.1"
6
+ spec.version = "1.1.0"
7
7
  spec.authors = ["Grzegorz Derebecki"]
8
8
  spec.email = ["grzegorz.derebecki@fdb.pl"]
9
9
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sortable-for-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Grzegorz Derebecki
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-08-16 00:00:00.000000000 Z
11
+ date: 2020-01-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails