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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2435ba6e0ee3cef2d9d22e2fd81bb2191ed60a70
4
- data.tar.gz: e75b0bc90d7932c4f0d91db505f5db7049eb33f1
3
+ metadata.gz: 6c8bb9d9cd7cbd2160b42329feecf0c6989069d2
4
+ data.tar.gz: f78351cc3a133a7ae0daaa3ac89849580e1ff32a
5
5
  SHA512:
6
- metadata.gz: e7fedc22f9251162e414449b262ca14920dbae3ea2c2f0aea0964cd2935d9083391cfe9db2ea5f805b12f05701aeb3fdb76de5b26fe541a05ec6a336fb24250a
7
- data.tar.gz: aea008d52329c6fd6088babe387c7551a8c1c95fda8ecfc3f0fd819ebb640d4cc52f0615a74223fedef752109ff7971935859383a8455da1f47846cdf9fdef6b
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 view helper for rendering table heads:
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
- <%= sortabl_th 'Author', :author, id: 'author-column', class: 'author-column' %>
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"><a href="/posts?sortabl=author_asc">Author<i class="fa fa-sort"></i></a></th>
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"><a href="/posts?sortabl=author_desc">Author<i class="fa fa-sort-asc"></i></a></th>
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"><a href="/posts">Author<i class="fa fa-sort-desc"></i></a></th>
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, the ViewHelper will generate `:sortabl` as parameter. If you need to change name of the parameter, you can do so by simply:
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
- <%= sortabl_th 'Author', :author, id: 'author-column', class: 'author-column', sort_param: :my_custom_sort_param %>
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 table head
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 sortabl_th name, attribute, *args
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
- sort_by = args[0][:sort_param] || :sortabl
12
+ sort_param = html_options[:sort_param] || :sortabl
13
+ html_options.except!(:sort_param)
14
14
 
15
- # To provide full path with all given params
16
- # make a copy of incoming params and override sort_param only
17
- sort_params = params.except(sort_by)
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
- # Set default sort path
20
- sort_params[sort_by] = "#{attribute}_asc"
21
-
22
- # if there is already a sort param set, invert or remove sort direction
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
- # Get element id and classes
37
- th_id = args[0][:id] if args.present? and args[0][:id].present?
38
- th_class = args[0][:class] if args.present? and args[0][:class].present?
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 HTML Code
41
- html = <<-HTML
42
- <th #{"id=#{th_id}" if th_id.present?} class="sortabl#{sort_direction.present? ? " #{sort_direction}" : ""}#{" #{th_class}" if th_class.present?}" #{args[0].map{ |k,v| "#{k}='#{v}'" unless (k.to_s =~ /^data-(.*)$/).nil? }.join(' ') if args.present?}>
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
- html.html_safe
39
+ content_tag("a".freeze, name || url, html_options, &block)
48
40
  end
49
41
 
50
42
  end
@@ -1,3 +1,3 @@
1
1
  module Sortabl
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
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.2.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-17 00:00:00.000000000 Z
11
+ date: 2016-03-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord