sortable-for-rails 1.0.1 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +9 -0
- data/lib/sortable.rb +14 -7
- data/sortable-for-rails.gemspec +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9fc07dc30d79c3ea98dd0c316b4a45ce4c46b300c0fbf3a210b03608f1be9254
|
4
|
+
data.tar.gz: fc4365bcaf59dbf9064fd885a4d9a1f657987ed853a3986205bf764ec3898120
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
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
|
data/sortable-for-rails.gemspec
CHANGED
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
|
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:
|
11
|
+
date: 2020-01-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|