smart_listing 0.9.1 → 0.9.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE +1 -1
- data/README.md +4 -0
- data/app/assets/javascripts/smart_listing/smart_listing.coffee +247 -247
- data/app/helpers/smart_listing/application_helper.rb +3 -3
- data/app/helpers/smart_listing/helper.rb +257 -257
- data/lib/smart_listing.rb +117 -112
- data/lib/smart_listing/version.rb +1 -1
- metadata +3 -3
data/lib/smart_listing.rb
CHANGED
@@ -1,116 +1,121 @@
|
|
1
1
|
require "smart_listing/engine"
|
2
2
|
require "kaminari"
|
3
3
|
module SmartListing
|
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
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
4
|
+
class Base
|
5
|
+
if Rails.env.development?
|
6
|
+
DEFAULT_PAGE_SIZES = [3, 10, 20, 50, 100]
|
7
|
+
else
|
8
|
+
DEFAULT_PAGE_SIZES = [10, 20, 50, 100]
|
9
|
+
end
|
10
|
+
|
11
|
+
attr_reader :name, :collection, :options, :per_page, :page, :sort_attr, :sort_order, :sort_extra, :partial, :count
|
12
|
+
|
13
|
+
def initialize name, collection, options = {}
|
14
|
+
@name = name
|
15
|
+
|
16
|
+
@options = {
|
17
|
+
:param_names => { # param names
|
18
|
+
:page => "#{@name}_page".to_sym,
|
19
|
+
:per_page => "#{@name}_per_page".to_sym,
|
20
|
+
:sort_attr => "#{@name}_sort_attr".to_sym,
|
21
|
+
:sort_order => "#{@name}_sort_order".to_sym,
|
22
|
+
:sort_extra => "#{@name}_sort_extra".to_sym,
|
23
|
+
},
|
24
|
+
:partial => @name, # smart list partial name
|
25
|
+
:array => false, # controls whether smart list should be using arrays or AR collections
|
26
|
+
:max_count => nil, # limit number of rows
|
27
|
+
:unlimited_per_page => false, # allow infinite page size
|
28
|
+
:sort => true, # allow sorting
|
29
|
+
:paginate => true, # allow pagination
|
30
|
+
:href => nil, # set smart list target url (in case when different than current url)
|
31
|
+
:default_sort_attr => nil, # default sort by
|
32
|
+
:memorize_per_page => false,
|
33
|
+
:page_sizes => DEFAULT_PAGE_SIZES, # set available page sizes array
|
34
|
+
}.merge!(options)
|
35
|
+
|
36
|
+
if @options[:array]
|
37
|
+
@collection = collection.to_a
|
38
|
+
else
|
39
|
+
@collection = collection
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def setup params, cookies
|
44
|
+
@page = params[param_names[:page]]
|
45
|
+
@per_page = !params[param_names[:per_page]] || params[param_names[:per_page]].empty? ? (@options[:memorize_per_page] && cookies[param_names[:per_page]].to_i > 0 ? cookies[param_names[:per_page]].to_i : page_sizes.first) : params[param_names[:per_page]].to_i
|
46
|
+
@sort_attr = params[param_names[:sort_attr]] || @options[:default_sort_attr]
|
47
|
+
@sort_order = ["asc", "desc"].include?(params[param_names[:sort_order]]) ? params[param_names[:sort_order]] : "desc"
|
48
|
+
@sort_extra = params[param_names[:sort_extra]]
|
49
|
+
|
50
|
+
cookies[param_names[:per_page]] = @per_page if @options[:memorize_per_page]
|
51
|
+
|
52
|
+
@count = @collection.size
|
53
|
+
|
54
|
+
if @options[:array]
|
55
|
+
@collection = @collection.sort do |x, y|
|
56
|
+
xval = x
|
57
|
+
yval = y
|
58
|
+
@sort_attr.split(".").each do |m|
|
59
|
+
xval = xval.try(m)
|
60
|
+
yval = yval.try(m)
|
61
|
+
end
|
62
|
+
xval = xval.upcase if xval.is_a?(String)
|
63
|
+
yval = yval.upcase if yval.is_a?(String)
|
64
|
+
|
65
|
+
if xval.nil? || yval.nil?
|
66
|
+
xval.nil? ? 1 : -1
|
67
|
+
else
|
68
|
+
if @sort_order == "asc"
|
69
|
+
(xval <=> yval) || (xval && !yval ? 1 : -1)
|
70
|
+
else
|
71
|
+
(yval <=> xval) || (yval && !xval ? 1 : -1)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end if @options[:sort] && @sort_attr && !@sort_attr.empty?
|
75
|
+
if @options[:paginate] && @per_page > 0
|
76
|
+
@collection = ::Kaminari.paginate_array(@collection).page(@page).per(@per_page)
|
77
|
+
if @collection.length == 0
|
78
|
+
@collection = @collection.page(@collection.num_pages)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
else
|
82
|
+
@collection = @collection.order("#{@sort_attr} #{@sort_order}") if @options[:sort] && @sort_attr && !@sort_attr.empty? && @sort_order
|
83
|
+
if @options[:paginate] && @per_page > 0
|
84
|
+
@collection = @collection.page(@page).per(@per_page)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def partial
|
90
|
+
@options[:partial]
|
91
|
+
end
|
92
|
+
|
93
|
+
def param_names
|
94
|
+
@options[:param_names]
|
95
|
+
end
|
96
|
+
|
97
|
+
def unlimited_per_page?
|
98
|
+
!!@options[:unlimited_per_page]
|
99
|
+
end
|
100
|
+
|
101
|
+
def max_count
|
102
|
+
@options[:max_count]
|
103
|
+
end
|
104
|
+
|
105
|
+
def href
|
106
|
+
@options[:href]
|
107
|
+
end
|
108
|
+
|
109
|
+
def page_sizes
|
110
|
+
@options[:page_sizes]
|
111
|
+
end
|
112
|
+
|
113
|
+
def all_params
|
114
|
+
ap = {}
|
115
|
+
@options[:param_names].each do |k, v|
|
116
|
+
ap[v] = self.send(k)
|
117
|
+
end
|
118
|
+
ap
|
119
|
+
end
|
120
|
+
end
|
116
121
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: smart_listing
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sology
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-12-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -107,7 +107,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
107
107
|
version: '0'
|
108
108
|
requirements: []
|
109
109
|
rubyforge_project:
|
110
|
-
rubygems_version: 2.
|
110
|
+
rubygems_version: 2.0.0
|
111
111
|
signing_key:
|
112
112
|
specification_version: 4
|
113
113
|
summary: SmartListing helps creating sortable lists of ActiveRecord collections with
|