sortabl 0.3.1 → 0.4.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e01cbbb9f3c94c956178c6621ba641bd5553b0fe
4
- data.tar.gz: 6b24e29afa7582fad977af32e080a2935abd8e04
3
+ metadata.gz: 3163242a1ee21d16b4f2d609b1182db106d3e9cf
4
+ data.tar.gz: 7999c7735d164568cc38ef9232910ce1b06e7d00
5
5
  SHA512:
6
- metadata.gz: 06d49735d1368a0a14ed9c6b67551c696974078fa176a8aca779116f2a11b70991e7a416824277eb58ffcaf2f1407199787225caa7c634119b1d254f9543b411
7
- data.tar.gz: d84f32c0b91703ad7aece129cfbc1a73ff8496cb284d1015200062ef3ce254345bcc5fda39b56247e4838829bdff6183305248a0813902ecd70bcda44a2f291d
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: 'author asc, created_at desc')
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
- <thead>
56
- <th><%= sortabl_link 'Author', :author, id: 'author-column', class: 'author-column' %></th>
57
- </thead>
58
- <tbody>
59
- ...
60
- </tbody>
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
- <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>
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
- <p>Author</p>
82
+ <p>Author</p>
83
83
  <% end %>
84
84
  ```
85
85
 
@@ -1,60 +1,48 @@
1
1
  module Sortabl
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].to_s if args
13
- only = args[0][:only]
14
- except = args[0][:except]
15
- sort_params = {}
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
- case true
25
- # List only attributes
26
- # only:
27
- #
28
- when only.present?
29
- (only.map(&:to_s)).each do |attribute|
30
- sort_params[attribute + '_asc'] = "#{attribute} asc"
31
- sort_params[attribute + '_desc'] = "#{attribute} desc"
32
- end
33
-
34
- # List class attributes without excepts
35
- # except:
36
- #
37
- when except.present?
38
- (self.column_names - except.map(&:to_s)).each do |attribute|
39
- sort_params[attribute + '_asc'] = "#{attribute} asc"
40
- sort_params[attribute + '_desc'] = "#{attribute} desc"
41
- end
42
-
43
- # List class attribtues
44
- #
45
- else
46
- self.column_names.each do |attribute|
47
- sort_params[attribute + '_asc'] = "#{attribute} asc"
48
- sort_params[attribute + '_desc'] = "#{attribute} desc"
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
- module ActionViewExtensions
3
- module SortablHelper
2
+ module ActionViewExtensions
3
+ module SortablHelper
4
4
 
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 || {}
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
- # 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)
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
- # 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"
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
- # 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$).)*/, '')
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
- sortabl_params[sort_param] = "#{attribute}_desc" if sort_direction == 'asc'
26
- sortabl_params[sort_param] = nil if sort_direction == 'desc'
27
- end
28
- end
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
- # Add sortabl html class to html_options
31
- html_options[:class] = 'sortabl' + (sort_direction.present? ? " #{sort_direction}" : "") + (html_options[:class].present? ? " #{html_options[:class]}" : "")
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
- # Support remote true
34
- html_options.except!(:remote).merge!({'data-remote': 'true'}) if html_options[:remote]
33
+ # Support remote true
34
+ html_options.except!(:remote).merge!({'data-remote': 'true'}) if html_options[:remote]
35
35
 
36
- # Generate url from params
37
- url = url_for(sortabl_params)
38
- html_options["href".freeze] ||= url
36
+ # Generate url from params
37
+ url = url_for(sortabl_params)
38
+ html_options["href".freeze] ||= url
39
39
 
40
- content_tag("a".freeze, name || url, html_options, &block)
41
- end
40
+ content_tag("a".freeze, name || url, html_options, &block)
41
+ end
42
42
 
43
- end
44
- end
43
+ end
44
+ end
45
45
  end
@@ -1,3 +1,3 @@
1
1
  module Sortabl
2
- VERSION = "0.3.1"
2
+ VERSION = "0.4.2"
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.3.1
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-03-19 00:00:00.000000000 Z
11
+ date: 2016-04-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord