sortabl 0.4.2 → 0.5.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 +4 -4
- data/README.md +6 -6
- data/lib/sortabl/sortabl_core.rb +11 -14
- data/lib/sortabl/version.rb +1 -1
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a6c4af3f0e56418608ff656d0f7b6ab202a36ef5
|
4
|
+
data.tar.gz: a33bedac5421960f53ee0f27b8dc75bac744592d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 67ce18ed70f06f3b3469245e7e080439005d667dbc5fbeea7358e46fb8341ae0570252203761646734622662d7ecd39d3a10cbf5522dd0a40f7cd3c56bd72d12
|
7
|
+
data.tar.gz: 4d8334f0ce2a0ac8bd694a6c5bfaf9dcef4d60e3f241eef3087f53cf64f82254f68ae1bad46b7993417bd57c9795a73eca5ef4e119b7166607a43a495027888e
|
data/README.md
CHANGED
@@ -17,19 +17,19 @@ gem 'sortabl'
|
|
17
17
|
#### Default
|
18
18
|
|
19
19
|
```ruby
|
20
|
-
@posts = Post.sortabl(
|
20
|
+
@posts = Post.sortabl(params[:sortabl])
|
21
21
|
```
|
22
22
|
|
23
23
|
And that's it! Records will be sorted by permitted parameter `:sortabl`. If parameter `:sortabl` isn't permitted, records will be sorted by primary key. If you don't want to sort by primary key as default, you can set another one by:
|
24
24
|
|
25
25
|
```ruby
|
26
|
-
@posts = Post.sortabl(
|
26
|
+
@posts = Post.sortabl(params[:sortabl], default: [author: :asc])
|
27
27
|
```
|
28
28
|
|
29
29
|
Even default multiple columns sort is possible:
|
30
30
|
|
31
31
|
```ruby
|
32
|
-
@posts = Post.sortabl(
|
32
|
+
@posts = Post.sortabl(params[:sortabl], default: [author: :asc, created_at: :desc])
|
33
33
|
```
|
34
34
|
|
35
35
|
#### Limeted Attributes
|
@@ -38,9 +38,9 @@ Permitted values can be an attribute of model class, followed by `_asc` or `_des
|
|
38
38
|
If there's an attribute permitted which doesn't exist in model, it falls back to sort by `default` key. Attributes can be limited with `only` and `except`. For example:
|
39
39
|
|
40
40
|
```ruby
|
41
|
-
@posts = Post.sortabl(
|
41
|
+
@posts = Post.sortabl(params[:sortabl], only: [:title, :author])
|
42
42
|
# or
|
43
|
-
@posts = Post.sortabl(
|
43
|
+
@posts = Post.sortabl(params[:sortabl], except: [:text])
|
44
44
|
```
|
45
45
|
|
46
46
|
|
@@ -92,7 +92,7 @@ By default, `sortabl_link` will generate `:sortabl` as parameter into the url. I
|
|
92
92
|
<%= sortabl_link 'Author', :author, sort_param: :my_custom_sort_param %>
|
93
93
|
|
94
94
|
# In controller
|
95
|
-
@posts = Post.sortabl(
|
95
|
+
@posts = Post.sortabl(params[:my_custom_sort_param], only: [:title, :author])
|
96
96
|
```
|
97
97
|
|
98
98
|
Which hyperlink will be rendered, depends on permitted values.
|
data/lib/sortabl/sortabl_core.rb
CHANGED
@@ -4,37 +4,34 @@ module Sortabl
|
|
4
4
|
|
5
5
|
extend ActiveSupport::Concern
|
6
6
|
|
7
|
-
# class Base
|
8
7
|
module ClassMethods
|
9
8
|
|
10
|
-
def sortabl *args
|
9
|
+
def sortabl parameter, *args
|
10
|
+
|
11
11
|
# Init
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
12
|
+
unless args.empty?
|
13
|
+
default = args[0][:default]
|
14
|
+
only = args[0][:only]
|
15
|
+
except = args[0][:except]
|
16
|
+
end
|
16
17
|
|
17
|
-
raise "Invalid Parameters: Do not use 'only' and 'except' together!" if only.present? and except.present?
|
18
|
+
raise ArgumentError.new("Invalid Parameters: Do not use 'only' and 'except' together!") if only.present? and except.present?
|
18
19
|
|
19
20
|
# Set default order attribute
|
20
21
|
# default:
|
21
|
-
#
|
22
22
|
order_by_default = default.present? ? default : self.primary_key
|
23
23
|
|
24
24
|
# Extract column name and direction from parameter
|
25
|
-
#
|
26
25
|
if parameter.present?
|
27
|
-
column_name = parameter.gsub(/(_asc$|_desc$)/, '')
|
28
|
-
direction = parameter.gsub(/^((?!desc$|asc$).)*/, '')
|
26
|
+
column_name = parameter.to_s.gsub(/(_asc$|_desc$)/, '')
|
27
|
+
direction = parameter.to_s.gsub(/^((?!desc$|asc$).)*/, '')
|
29
28
|
|
30
29
|
# Sort by default if column_name is not included in only or column_name is included in except
|
31
|
-
#
|
32
30
|
return order((order_by_default)) if only.present? and !only.include? column_name.to_sym
|
33
31
|
return order((order_by_default)) if except.present? and except.include? column_name.to_sym
|
34
32
|
end
|
35
33
|
|
36
34
|
# Convert param_value to symbol
|
37
|
-
#
|
38
35
|
sort_column = { column_name.to_sym => direction.to_sym } if column_name.present? and direction.present?
|
39
36
|
|
40
37
|
# Order class object
|
@@ -45,4 +42,4 @@ module Sortabl
|
|
45
42
|
|
46
43
|
end
|
47
44
|
end
|
48
|
-
end
|
45
|
+
end
|
data/lib/sortabl/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sortabl
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Frederic Walch
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-07-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -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: actionview
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '4.0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '4.0'
|
41
55
|
- !ruby/object:Gem::Dependency
|
42
56
|
name: bundler
|
43
57
|
requirement: !ruby/object:Gem::Requirement
|