sortabl 0.2.0 → 0.3.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 +15 -8
- data/lib/sortabl/sortabl_helper.rb +23 -31
- 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: 6c8bb9d9cd7cbd2160b42329feecf0c6989069d2
|
4
|
+
data.tar.gz: f78351cc3a133a7ae0daaa3ac89849580e1ff32a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e2dddc0491636c11a14e9109aca73939ae216e69ad2c00ecb845c8d95b834878097bae7dad0a7107badb2fd273c77e887d1233887137fcdfa654ea76889d4d65
|
7
|
+
data.tar.gz: 71f5460fb2496b7f89a1dd3f461fddadb13ed553e342cab0fd6d92e104c24d488b36ee132f29a7cc3092363f9a6b16d7fa4e8f45e0d06c34ab1ba2d4612a4248
|
data/README.md
CHANGED
@@ -36,12 +36,12 @@ If there's an attribute permitted which doesn't exist in model, it falls back to
|
|
36
36
|
|
37
37
|
### View
|
38
38
|
|
39
|
-
There's also a
|
39
|
+
There's also a link helper, which generates needed urls. It works just like you would expect from rails default link_to helper. But instead of hand over a path, just place the desired column name. The helper will do the rest for you:
|
40
40
|
|
41
41
|
```erb
|
42
42
|
<table>
|
43
43
|
<thead>
|
44
|
-
|
44
|
+
<th><%= sortabl_link 'Author', :author, id: 'author-column', class: 'author-column' %></th>
|
45
45
|
</thead>
|
46
46
|
<tbody>
|
47
47
|
...
|
@@ -51,11 +51,11 @@ There's also a view helper for rendering table heads:
|
|
51
51
|
# Will be rendered to:
|
52
52
|
<table>
|
53
53
|
<thead>
|
54
|
-
<th id="author-column" class="sortabl author-column"
|
54
|
+
<th><a id="author-column" class="sortabl author-column" href="/posts?sortabl=author_asc">Author</a></th>
|
55
55
|
# or
|
56
|
-
<th id="author-column" class="sortabl asc author-column"
|
56
|
+
<th><a id="author-column" class="sortabl asc author-column" href="/posts?sortabl=author_desc">Author</a></th>
|
57
57
|
# or
|
58
|
-
<th id="author-column" class="sortabl desc author-column"
|
58
|
+
<th><a id="author-column" class="sortabl desc author-column" href="/posts">Author</a></th>
|
59
59
|
</thead>
|
60
60
|
<tbody>
|
61
61
|
...
|
@@ -63,20 +63,27 @@ There's also a view helper for rendering table heads:
|
|
63
63
|
</table>
|
64
64
|
```
|
65
65
|
|
66
|
+
This link helper can be placed anywhere in the view. There is no need, that it has to be placed in table head. Furthermore you can use `sortabl_link` as block too.
|
67
|
+
|
68
|
+
```erb
|
69
|
+
<%= sortabl_link :author, id: 'author-column', class: 'author-column' do %>
|
70
|
+
<p>Author</p>
|
71
|
+
<% end %>
|
72
|
+
```
|
73
|
+
|
66
74
|
#### Parameter
|
67
75
|
|
68
|
-
By default,
|
76
|
+
By default, `sortabl_link` will generate `:sortabl` as parameter into the url. If you need to change name of the parameter, you can do so by simply:
|
69
77
|
|
70
78
|
```ruby
|
71
79
|
# In view
|
72
|
-
<%=
|
80
|
+
<%= sortabl_link 'Author', :author, id: 'author-column', class: 'author-column', sort_param: :my_custom_sort_param %>
|
73
81
|
|
74
82
|
# In controller
|
75
83
|
@posts = Post.sortabl(sort_by: params[:my_custom_sort_param], only: [:title, :author])
|
76
84
|
```
|
77
85
|
|
78
86
|
Which hyperlink will be rendered, depends on permitted values.
|
79
|
-
For now, this gem uses [font-awesome-rails](https://github.com/bokmann/font-awesome-rails) for placing icons. So you may want to install font-awesome gem to your project as well. More icon libaries will be supported in future.
|
80
87
|
Gem works also fine with pagination like [will_paginate](https://github.com/mislav/will_paginate).
|
81
88
|
|
82
89
|
Happy sorting ;)
|
@@ -2,49 +2,41 @@ module Sortabl
|
|
2
2
|
module ActionViewExtensions
|
3
3
|
module SortablHelper
|
4
4
|
|
5
|
-
# Renders
|
6
|
-
# Uses fontawesome as default icons
|
7
|
-
# <th 'data-sortable': 'attribute_direction'>Name <i class='fa fa-...'></th>
|
5
|
+
# Renders sortabl link
|
8
6
|
#
|
9
|
-
def
|
7
|
+
def sortabl_link(name = nil, attribute = nil, html_options = nil, &block)
|
8
|
+
html_options, attribute, name = attribute, name, block if block_given?
|
10
9
|
|
11
10
|
# Support custom sort_param
|
12
11
|
# If sort_param isn't given, fall back to :sortabl as param
|
13
|
-
|
12
|
+
sort_param = html_options[:sort_param] || :sortabl
|
13
|
+
html_options.except!(:sort_param)
|
14
14
|
|
15
|
-
#
|
16
|
-
|
17
|
-
|
15
|
+
# Remove current sortabl param from url and add default sortabl param
|
16
|
+
sortabl_params = params.except(sort_param)
|
17
|
+
sortabl_params[sort_param] = "#{attribute}_asc"
|
18
18
|
|
19
|
-
#
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
if params[sort_by].present?
|
24
|
-
|
25
|
-
# if sort param match current attribute, change direction
|
26
|
-
if attribute.to_s == params[sort_by].gsub(/(_asc$|_desc$)/, '')
|
27
|
-
|
28
|
-
sort_direction = params[sort_by].gsub(/^((?!desc$|asc$).)*/, '')
|
29
|
-
|
30
|
-
sort_params[sort_by] = "#{attribute}_desc" if sort_direction == 'asc'
|
31
|
-
sort_params[sort_by] = nil if sort_direction == 'desc'
|
19
|
+
# If there was already a sortabl param, invert direction or remove sortabl param
|
20
|
+
if params[sort_param].present?
|
21
|
+
if attribute.to_s == params[sort_param].gsub(/(_asc$|_desc$)/, '')
|
22
|
+
sort_direction = params[sort_param].gsub(/^((?!desc$|asc$).)*/, '')
|
32
23
|
|
24
|
+
sortabl_params[sort_param] = "#{attribute}_desc" if sort_direction == 'asc'
|
25
|
+
sortabl_params[sort_param] = nil if sort_direction == 'desc'
|
33
26
|
end
|
34
27
|
end
|
35
28
|
|
36
|
-
#
|
37
|
-
|
38
|
-
|
29
|
+
# Add sortabl html class to html_options
|
30
|
+
html_options[:class] = 'sortabl' + (sort_direction.present? ? " #{sort_direction}" : "") + (html_options[:class].present? ? " #{html_options[:class]}" : "")
|
31
|
+
|
32
|
+
# Support remote true
|
33
|
+
html_options.except!(:remote).merge!({'data-remote': 'true'}) if html_options[:remote]
|
39
34
|
|
40
|
-
# Generate
|
41
|
-
|
42
|
-
|
43
|
-
<a href='#{url_for(sort_params)}'>#{name}<i class='fa fa-sort#{sort_direction.present? ? "-#{sort_direction}" : ""}'></i></a>
|
44
|
-
</td>
|
45
|
-
HTML
|
35
|
+
# Generate url from params
|
36
|
+
url = url_for(sortabl_params)
|
37
|
+
html_options["href".freeze] ||= url
|
46
38
|
|
47
|
-
|
39
|
+
content_tag("a".freeze, name || url, html_options, &block)
|
48
40
|
end
|
49
41
|
|
50
42
|
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.3.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-03-
|
11
|
+
date: 2016-03-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|