sortabl 0.3.1 → 0.4.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +18 -18
- data/lib/sortabl/sortabl_core.rb +47 -59
- data/lib/sortabl/sortabl_helper.rb +33 -33
- data/lib/sortabl/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3163242a1ee21d16b4f2d609b1182db106d3e9cf
|
4
|
+
data.tar.gz: 7999c7735d164568cc38ef9232910ce1b06e7d00
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 687800544182c04f1ec55eb4d0f8e5049b93948b567abaaf2e63baf5279862624b0c5e55cae3df7a479b19fc1c3466460550583267fc3e9575d7b1e0247df96c
|
7
|
+
data.tar.gz: 1518453eda7a21079422cac19d96246e172bf61405a91d46a787a1ef0890c3385b59b46960b2ecc8b3e492897de251e45c4b35829c418d1ee511f1802972d671
|
data/README.md
CHANGED
@@ -29,7 +29,7 @@ And that's it! Records will be sorted by permitted parameter `:sortabl`. If para
|
|
29
29
|
Even default multiple columns sort is possible:
|
30
30
|
|
31
31
|
```ruby
|
32
|
-
@posts = Post.sortabl(sort_by: params[:sortabl], default:
|
32
|
+
@posts = Post.sortabl(sort_by: params[:sortabl], default: [author: :asc, created_at: :desc])
|
33
33
|
```
|
34
34
|
|
35
35
|
#### Limeted Attributes
|
@@ -52,26 +52,26 @@ There's also a link helper, which generates needed urls. It works just like you
|
|
52
52
|
|
53
53
|
```erb
|
54
54
|
<table>
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
55
|
+
<thead>
|
56
|
+
<th><%= sortabl_link 'Author', :author, id: 'author-column', class: 'author-column' %></th>
|
57
|
+
</thead>
|
58
|
+
<tbody>
|
59
|
+
...
|
60
|
+
</tbody>
|
61
61
|
</table>
|
62
62
|
|
63
63
|
# Will be rendered to:
|
64
64
|
<table>
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
65
|
+
<thead>
|
66
|
+
<th><a id="author-column" class="sortabl author-column" href="/posts?sortabl=author_asc">Author</a></th>
|
67
|
+
# or
|
68
|
+
<th><a id="author-column" class="sortabl asc author-column" href="/posts?sortabl=author_desc">Author</a></th>
|
69
|
+
# or
|
70
|
+
<th><a id="author-column" class="sortabl desc author-column" href="/posts">Author</a></th>
|
71
|
+
</thead>
|
72
|
+
<tbody>
|
73
|
+
...
|
74
|
+
</tbody>
|
75
75
|
</table>
|
76
76
|
```
|
77
77
|
|
@@ -79,7 +79,7 @@ This link helper can be placed anywhere in the view. There is no need, that it h
|
|
79
79
|
|
80
80
|
```erb
|
81
81
|
<%= sortabl_link :author, id: 'author-column', class: 'author-column' do %>
|
82
|
-
|
82
|
+
<p>Author</p>
|
83
83
|
<% end %>
|
84
84
|
```
|
85
85
|
|
data/lib/sortabl/sortabl_core.rb
CHANGED
@@ -1,60 +1,48 @@
|
|
1
1
|
module Sortabl
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
# Order class object
|
53
|
-
order((sort_params[args[0][:sort_by]] or order_by_default))
|
54
|
-
end
|
55
|
-
|
56
|
-
end
|
57
|
-
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end
|
2
|
+
module ActiveRecordExtensions
|
3
|
+
module Sortabl
|
4
|
+
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
# class Base
|
8
|
+
module ClassMethods
|
9
|
+
|
10
|
+
def sortabl *args
|
11
|
+
# Init
|
12
|
+
default = args[0][:default]
|
13
|
+
only = args[0][:only]
|
14
|
+
except = args[0][:except]
|
15
|
+
parameter = args[0][:sort_by]
|
16
|
+
|
17
|
+
raise "Invalid Parameters: Do not use 'only' and 'except' together!" if only.present? and except.present?
|
18
|
+
|
19
|
+
# Set default order attribute
|
20
|
+
# default:
|
21
|
+
#
|
22
|
+
order_by_default = default.present? ? default : self.primary_key
|
23
|
+
|
24
|
+
# Extract column name and direction from parameter
|
25
|
+
#
|
26
|
+
if parameter.present?
|
27
|
+
column_name = parameter.gsub(/(_asc$|_desc$)/, '')
|
28
|
+
direction = parameter.gsub(/^((?!desc$|asc$).)*/, '')
|
29
|
+
|
30
|
+
# Sort by default if column_name is not included in only or column_name is included in except
|
31
|
+
#
|
32
|
+
return order((order_by_default)) if only.present? and !only.include? column_name.to_sym
|
33
|
+
return order((order_by_default)) if except.present? and except.include? column_name.to_sym
|
34
|
+
end
|
35
|
+
|
36
|
+
# Convert param_value to symbol
|
37
|
+
#
|
38
|
+
sort_column = { column_name.to_sym => direction.to_sym } if column_name.present? and direction.present?
|
39
|
+
|
40
|
+
# Order class object
|
41
|
+
order((sort_column or order_by_default))
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -1,45 +1,45 @@
|
|
1
1
|
module Sortabl
|
2
|
-
|
3
|
-
|
2
|
+
module ActionViewExtensions
|
3
|
+
module SortablHelper
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
5
|
+
# Renders sortabl link
|
6
|
+
#
|
7
|
+
def sortabl_link(name = nil, attribute = nil, html_options = nil, &block)
|
8
|
+
html_options, attribute, name = attribute, name, block if block_given?
|
9
|
+
html_options = html_options || {}
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
11
|
+
# Support custom sort_param
|
12
|
+
# If sort_param isn't given, fall back to :sortabl as param
|
13
|
+
sort_param = html_options[:sort_param] || :sortabl
|
14
|
+
html_options.except!(:sort_param)
|
15
15
|
|
16
|
-
|
17
|
-
|
18
|
-
|
16
|
+
# Remove current sortabl param from url and add default sortabl param
|
17
|
+
sortabl_params = params.except(sort_param)
|
18
|
+
sortabl_params[sort_param] = "#{attribute}_asc"
|
19
19
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
20
|
+
# If there was already a sortabl param, invert direction or remove sortabl param
|
21
|
+
if params[sort_param].present?
|
22
|
+
if attribute.to_s == params[sort_param].gsub(/(_asc$|_desc$)/, '')
|
23
|
+
sort_direction = params[sort_param].gsub(/^((?!desc$|asc$).)*/, '')
|
24
24
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
25
|
+
sortabl_params[sort_param] = "#{attribute}_desc" if sort_direction == 'asc'
|
26
|
+
sortabl_params[sort_param] = nil if sort_direction == 'desc'
|
27
|
+
end
|
28
|
+
end
|
29
29
|
|
30
|
-
|
31
|
-
|
30
|
+
# Add sortabl html class to html_options
|
31
|
+
html_options[:class] = 'sortabl' + (sort_direction.present? ? " #{sort_direction}" : "") + (html_options[:class].present? ? " #{html_options[:class]}" : "")
|
32
32
|
|
33
|
-
|
34
|
-
|
33
|
+
# Support remote true
|
34
|
+
html_options.except!(:remote).merge!({'data-remote': 'true'}) if html_options[:remote]
|
35
35
|
|
36
|
-
|
37
|
-
|
38
|
-
|
36
|
+
# Generate url from params
|
37
|
+
url = url_for(sortabl_params)
|
38
|
+
html_options["href".freeze] ||= url
|
39
39
|
|
40
|
-
|
41
|
-
|
40
|
+
content_tag("a".freeze, name || url, html_options, &block)
|
41
|
+
end
|
42
42
|
|
43
|
-
|
44
|
-
|
43
|
+
end
|
44
|
+
end
|
45
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.4.2
|
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-04-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|